1. Add plugin coordinates under maven
<!--配置Generator插件-->
<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>
Second, add a configuration file
Add generatorConfig.xml file under resources
Configure the database connection information and mapper and entity class generation path.
<?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>
<!--生成PO类位置-->
<javaModelGenerator targetPackage="com.example.springbootmybatis.pojo"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--mapper映射文件生成位置-->
<sqlMapGenerator targetPackage="com.example.springbootmybatis.mapper" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--mapper接口生成位置-->
<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>
Three, add DTD file
** http: //mybatis.org/dtd/mybatis-generator-config_1_0.dtd ** Copy the link to the browser to download the dtd file
Open the settings in IDEA with ctrl + alt + s, add http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd to Languages & Frameworks | Schemas and DTDs, and select the dtd save path
Four, run the plug-in to generate code
Select Plugins–> mybatis-generator–> mybatis-generator in maven: generate double-click to run
5. Add annotations to the startup class
@SpringBootApplication
@MapperScan("com.example.springbootmybatis.mapper")//指定扫描接口与映射配置文件的包名
public class SpringbootmybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootmybatisApplication.class, args);
}
}