在springboot项目中添加Mybatis代码自动生成
1.在springboot项目resource目录下添加generator目录
2.在generator目录下添加config.properties与generatorConfig.xml配置文件
config.properties
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/market_dbs?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
jdbc.user=用户名
jdbc.password=密码
modelName=UserMessage
tableName=t_vehicle_message
generatorConfig.xml
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<properties resource="generator/config.properties"/>
<context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
<property name="caseSensitive" value="true"/>
<!--使用lombok生成get和set-->
<property name="lombok" value="Getter,Setter,ToString,Accessors"/>
</plugin>
<jdbcConnection driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.url}"
userId="${jdbc.user}"
password="${jdbc.password}">
<!--与数据库自带同名表区分-->
<property name="nullCatalogMeansCurrent" value="true" ></property>
</jdbcConnection>
<!--实体类-->
<javaModelGenerator targetPackage="com.dh.marketplatform.user.domain.vo.dto.${modelName}"
targetProject="src/main/java"/>
<!--mapper.xml-->
<sqlMapGenerator targetPackage="mapping/${modelName}"
targetProject="src/main/resources"/>
<!--mapper接口-->
<javaClientGenerator targetPackage="com.dh.marketplatform.user.domain.mapper.${modelName}"
targetProject="src/main/java"
type="XMLMAPPER"/>
<!--为哪张表生成-->
<table tableName="${tableName}">
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
</context>
</generatorConfiguration>
3.在pom文件build>plugins标签下添加如下配置
<!--通用mapper实体类代码生成器-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>
${basedir}/src/main/resources/generator/generatorConfig.xml
</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
</plugin>
4.在pom文件中dependencies标签下添加如下依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>