1. pom.xml中添加:
<dependencies>
<!-- mybatis.jar保证生成文件引入class不报错; 不引用不影响生成过程 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- mybatis-generator-core.jar引入可以校验generatorConfig.xml, 并引入dtd; 不引用不影响生成过程 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
</dependencies>
<build>
<finalName>generator</finalName>
<!-- idea默认是只读取resource下面的xml文件, 如果我们需要把mapper.xml文件放到java路径下, 需要在pom.xml中加入如下配置: -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<!-- 插件 -->
<pluginManagement>
<plugins>
<!-- 加入Mybatis的逆向工程jar -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!-- 本地MySQL版本为5.1.73, 所以该连接版本不能使用6.0.0以上, 否则会报错: Cannot create PoolableConnectionFactory (CLIENT_PLUGIN_AUTH is required) -->
<version>5.1.38</version>
</dependency>
</dependencies>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<!-- mybatis.jar保证生成文件引入class不报错; 不引用不影响生成过程 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- mybatis-generator-core.jar引入可以校验generatorConfig.xml, 并引入dtd; 不引用不影响生成过程 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
</dependencies>
<build>
<finalName>generator</finalName>
<!-- idea默认是只读取resource下面的xml文件, 如果我们需要把mapper.xml文件放到java路径下, 需要在pom.xml中加入如下配置: -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<!-- 插件 -->
<pluginManagement>
<plugins>
<!-- 加入Mybatis的逆向工程jar -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!-- 本地MySQL版本为5.1.73, 所以该连接版本不能使用6.0.0以上, 否则会报错: Cannot create PoolableConnectionFactory (CLIENT_PLUGIN_AUTH is required) -->
<version>5.1.38</version>
</dependency>
</dependencies>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
2. 编写generatorConfig.xml
3. Edit Configurations --> +(添加) --> maven --> Name: mybatis-generator(随意) --> mybatis-generator:generate -e --> run
4. 后期如果使用IDEA时, xml文件放入src/main/java下, 需解决IDEA默认只读取src/main/resources下的文件
介绍完毕, 提供参考附件, 如下:
a. 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>
<!-- 数据库驱动包位置 -->
<!--<classPathEntry location="E:\task\software\develop\repertory\workspaces\workspace-m2\demo\spring\example\Mybatis\generatorConfig\java\lib\mysql-connector-java-5.1.24-bin.jar" />-->
<context id="sqlGenerate" targetRuntime="MyBatis3">
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/demo?characterEncoding=utf8"
userId="root" password="root">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer; 为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成Pojo包名和位置 -->
<javaModelGenerator targetPackage="com.learn.demo.domain"
targetProject="java\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
<!-- 清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成Mapper映射XML文件位置 -->
<sqlMapGenerator targetPackage="com.learn.demo.mapper"
targetProject="java\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成Mapper接口文件位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.learn.demo.dao"
targetProject="java\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成哪些表(更改tableName和domainObjectName就可以) -->
<!-- tableName:要生成的表名 domainObjectName:生成后的实例名 enableCountByExample:Count语句中加入where条件查询,默认为true开启
enableUpdateByExample:Update语句中加入where条件查询,默认为true开启 enableDeleteByExample:Delete语句中加入where条件查询,默认为true开启
enableSelectByExample:Select多条语句中加入where条件查询,默认为true开启 selectByExampleQueryId:Select单个对象语句中加入where条件查询,默认为true开启 -->
<table tableName="test" domainObjectName="Test"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false" />
<table tableName="user_t" />
<!-- <table tableName="order" /> -->
<!-- <table tableName="product" /> -->
<!-- <table tableName="order_detail" /> -->
</context>
</generatorConfiguration>
b. 使用mybatis-generator插件方法