
====================================
pom.xml 文件
====================================
需要在 pom.xml 文件增加 mybatis-generator-maven 插件, mybatis-generator maven 插件默认会读到 src/main/resources目录下的 generatorConfig.xml 文件, 也可以自定义 generatorConfig.xml的路径.
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<!--配置文件的路径-->
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
</configuration>
</plugin>
====================================
generatorConfig.properties 文件
====================================
src/main/resources目录下的新建 generatorConfig.properties 文件, 该文件将被generatorConfig.xml引用, 在其中定义一些和环境相关的变量, 比如jar 驱动的路径.
mybatisDriverJarPath=C:\\Users\\Administrator\\.m2\\repository\\mysql\\mysql-connector-java\\5.1.\\mysql-connector-java-5.1..jar
mybatisDriverClassName=com.mysql.jdbc.Driver
mybatisJdbcUrl=jdbc:mysql://localhost/world?useUnicode=true&characterEncoding=utf-8
mybatisJdbcUser=root
mybatisJdbcPassword=toor
mybatisJavaModelPackage=com.springbootmybatis.mybatissample.entity
mybatisSqlMapPackage=com.springbootmybatis.mybatissample.mapper
mybatisDaoPackage=com.springbootmybatis.mybatissample.dao
====================================
generatorConfig.xml 文件
====================================
src/main/resources目录下的新建 generatorConfig.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>
<!--配置文件[可选项], 引入配置文件后, 在下面的Xml内容中可以使用${configItem}的方式来设定属性值, 比如jdbc用户名 -->
<properties resource="generatorConfig.properties" /> <!-- 必须配置驱动包的路径 -->
<classPathEntry location="${mybatisDriverJarPath}" /> <context id="MysqlTables" targetRuntime="MyBatis3">
<!-- 生成的pojo,将implements Serializable/Hashcode/ToString方法 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<plugin
type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin> <commentGenerator>
<!-- 是否去除自动生成的注释 -->
<property name="suppressAllComments" value="false" /> <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 -->
<!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true -->
<property name="suppressDate" value="true" />
</commentGenerator> <jdbcConnection driverClass="${mybatisDriverClassName}"
connectionURL="${mybatisJdbcUrl}" userId="${mybatisJdbcUser}"
password="${mybatisJdbcPassword}">
</jdbcConnection> <javaTypeResolver>
<!-- 使用Java的 Integer而不是java.math.BigDecimal来对应DB中的 DECIMAL 和 NUMERIC 类型 -->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver> <!-- 指定生成pojo的包和此包在项目中的地址; -->
<javaModelGenerator
targetPackage="${mybatisJavaModelPackage}" targetProject="src\main\java">
<!-- 是否在当前路径下新加一层, 新的一层以db schema作为目录名 -->
<property name="enableSubPackages" value="false" />
</javaModelGenerator> <!--对应的mapper.xml文件 -->
<sqlMapGenerator
targetPackage="${mybatisSqlMapPackage}" targetProject="src\main\java">
<!-- 是否在当前路径下新加一层, 新的一层以db schema作为目录名 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator> <!-- 对应的Mapper接口类文件 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="${mybatisDaoPackage}" targetProject="src\main\java">
<!-- 是否在当前路径下新加一层, 新的一层以db schema作为目录名 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator> <!-- 需要生成entity的数据表, 该table节点可以多个 -->
<table schema="world" tableName="city" domainObjectName="City"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"> <!-- 用于指定生成实体类时是否使用实际的列名作为实体类的属性名, 如果要用Camel Case风格, 需要设置为false -->
<property name="useActualColumnNames" value="false" /> <!-- 字段重命名 -->
<!--
<columnOverride column="create_time" property="createTime" />
<columnOverride column="float_test" property="floatTest" />
<columnOverride column="double_test" property="doubleTest" />
<columnOverride column="text_test" property="textTest" />
--> <!-- 忽略列,不生成bean 字段 -->
<!-- <ignoreColumn column="FRED" /> --> <!-- 指定列的java数据类型 -->
<!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
</table>
</context>
</generatorConfiguration>
====================================
mvn mybatis-generator:generate 命令
====================================
执行 mvn mybatis-generator:generate 命令, 可生成对应Java entity类和 mapper.xml 和 java dao 接口.
====================================
参考
====================================
https://www.cnblogs.com/GaiDynasty/p/4088531.html
https://www.cnblogs.com/JsonShare/p/5521901.html
https://segmentfault.com/a/1190000009058867
https://blog.csdn.net/wangxy799/article/details/60870361
https://www.cnblogs.com/linhp/p/5884151.html
https://www.cnblogs.com/hyyq/p/7087620.html