generatorConfog.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>
<properties resource="generator.properties"/>
<context id="MySqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<!-- 生成的Java文件的编码 -->
<property name="javaFileEncoding" value="UTF-8"/>
<!-- 使用自带序列化插件 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<!-- 指定生成 Mapper 的继承模板 -->
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
</plugin>
<!--注意context内的文件要按序放-->
<!--可以自定义生成model的代码注释-->
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
<property name="addRemarkComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.connectionURL}"
userId="${jdbc.userId}"
password="${jdbc.password}">
</jdbcConnection>
<!--指定生成model的路径-->
<javaModelGenerator targetPackage="${java.package}"
targetProject="${java.project}">
<property name="enableSubPackages" value="${java.enableSubPackages}"/>
</javaModelGenerator>
<!--指定生成mapper.xml的路径-->
<sqlMapGenerator targetPackage="${xml.package}"
targetProject="${xml.project}">
<property name="enableSubPackages" value="${xml.enableSubPackages}"/>
</sqlMapGenerator>
<!--指定生成mapper接口的的路径-->
<javaClientGenerator type="XMLMAPPER" targetPackage="${mapper.package}"
targetProject="${mapper.project}">
<property name="enableSubPackages" value="${mapper.enableSubPackages}"/>
</javaClientGenerator>
<!--生成全部表tableName设为%-->
<table tableName="user" schema="test">
<generatedKey column="id" sqlStatement="mysql" />
</table>
</context>
</generatorConfiguration>
generator.properties
# 数据库连接信息
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://127.0.0.1:3306/db
jdbc.userId=
jdbc.password=
# 指定生成model的路径
java.package=
java.project=src\\main\\java
java.enableSubPackages=true
# 指定生成mapper.xml的路径
xml.package=
xml.project=src\\main\\resources
xml.enableSubPackages=true
# 指定生成mapper接口的路径
mapper.package=
mapper.project=src\\main\\java
mapper.enableSubPackages=true
maven 插件
<build>
<plugins>
<!-- Mapper 代码生成插件 -->
<plugin>
<!-- mybaits 代码生成插件 -->
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!-- 代码自动生成配置文件路径 -->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<!-- 是否覆盖 -->
<overwrite>true</overwrite>
<!-- 允许移动生成的文件 -->
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<!-- mapper 依赖库 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.0.3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
点击maven插件生成代码