Mybatis-Generator是一个用于自动生成dao层接口、pojo以及mapper xml的一个Mybatis插件。 官网学习地址:http://www.mybatis.org/generator/
1、在工程中的pom.xml文件,配置如下依赖及插件
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
2、在main的resource目录下创建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>
<!--JDBC驱动jar包的位置-->
<classPathEntry location="D:/tools/myRepository/mysql/mysql-connector-java/8.0.13/mysql-connector-java-8.0.13.jar" />
<context id="default" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true" />
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--JDBC数据库连接-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mydb"
userId="root"
password="root">
</jdbcConnection>
<!--类型处理器,在数据库类型和java类型之间的转换控制-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- Model模型生成器,用来生成含有主键key的类 -->
<javaModelGenerator targetPackage="generator" targetProject="./src/main/java">
<!-- 是否把 schema 作为子包名 -->
<property name="enableSubPackages" value="true" />
<!-- 是否清理从数据库中查询出的字符串左右两边的空白字符 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator targetPackage="generator" targetProject="./src/main/resources">
<!-- 是否把 schema 作为子包名 -->
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="generator" targetProject="./src/main/resources">
<!-- 是否把 schema 作为子包名 -->
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="tb_user" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
3、使用maven运行插件,自动生成代码