- 在maven项目中添加pom配置信息
导入配置
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
添加插件:
<build>
<finalName>${name}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
<configuration>
<!--允许移动生成的文件 -->
<verbose>true</verbose>
<!-- 是否覆盖 -->
<overwrite>true</overwrite>
<!-- 自动生成的配置 -->
<configurationFile>
src/main/resources/mybatis-generator.xml
</configurationFile>
</configuration>
</plugin>
</plugins>
</build>
2.根据上面自动生成配置文件的路径信息,添加mybatis-generator.xml 生成文件
添加路径:src/main/resources/mybatis-generator.xml
mybatis-generator.xml内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="hr" targetRuntime="MyBatis3">
<property name="suppressAllComments" value="false" />
<property name="useActualColumnNames" value="false" />
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&useSSL=false&serverTimezone=UTC"
userId="root" password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="com.admin.org.dao.dataobject"
targetProject="C:\Users\admin\Desktop\dab\selfDemo\aa\src\main\java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
<property name="useGeneratedKeys" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="sqlMap"
targetProject="C:\Users\admin\Desktop\dab\selfDemo\aa\src\main\resources\">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.admin.org.dao.mapper"
targetProject="C:\Users\admin\Desktop\dab\selfDemo/aa\src\main\java">
<property name="enableSubPackages" value="true"/>
<property name="useGeneratedKeys" value="true"/>
</javaClientGenerator>
<table tableName="student" domainObjectName="StudentDo"
enableUpdateByExample="true" enableDeleteByExample="true">
<generatedKey column="ID" sqlStatement="mysql" identity="true" />
</table>
</context>
</generatorConfiguration>
注意:targetPackage 为项目目录名称
targetProject 为自动生成数据库表对应的实体文件名的路径地址
记得在项目启动位置添加 @MapperScan("com.xinzhe.org.dao.mapper")
完成上述操作后,记得在application.yml配置文件中添加 如下配置不然会出现如下错误
mybatis:
mapper-locations: classpath*:sqlMap/*.xml
参考示例:
@MapperScan("com.admin.org.dao.mapper")
@SpringBootApplication
public class SelfApplication {
public static void main(String[] args) {
SpringApplication.run(SelfApplication.class, args);
}
}