一、在maven下添加插件坐标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <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> </dependencies> <configuration> <configurationFile>${project.basedir}/src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins>
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> </resource> </resources>
|
二、添加配置文件
resources 下添加 generatorConfig.xml 文件
配置数据库连接信息和mapper以及实体类生成路径。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| <?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="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true"></property> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql:///day17?serverTimezone=UTC" userId="root" password="root"> <property name="nullCatalogMeansCurrent" value="true"/> </jdbcConnection>
<javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetPackage="com.example.springbootmybatis.pojo" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.example.springbootmybatis.mapper" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.springbootmybatis.mapper" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table schema="" tableName="user" > </table>
</context> </generatorConfiguration>
|
三、添加DTD文件
*http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd *复制链接到浏览器下载 dtd 文件
在 IDEA 中 ctrl+alt+s 打开设置,在 Languages & Frameworks | Schemas and DTDs 中增加http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd,并选择 dtd 保存路径
四、运行插件生成代码
在 maven 中选择Plugins–>mybatis-generator–>mybatis-generator:generate双击运行
五、启动类中添加注解
1 2 3 4 5 6 7 8 9
| @SpringBootApplication @MapperScan("com.example.springbootmybatis.mapper") public class SpringbootmybatisApplication {
public static void main(String[] args) { SpringApplication.run(SpringbootmybatisApplication.class, args); }
}
|