[06] 利用mybatis-generator自动生成代码
1、mybatis-generator 概述
MyBatis官方提供了逆向工程 mybatis-generator,可以针对数据库表自动生成MyBatis执行所需要的代码(如Mapper.java、Mapper.xml、POJO)。mybatis-generator 有三种用法:命令行、eclipse插件、maven插件。而maven插件的方式比较通用,本文也将概述maven插件的使用方式。
2、pom.xml中配置plugin
<build>
<plugins>
<!-- mybatis-generator -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!-- mybatis-generator的配置文件,根据情况调整位置 -->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
31
1
<build>
2
<plugins>
3
<!-- mybatis-generator -->
4
<plugin>
5
<groupId>org.mybatis.generator</groupId>
6
<artifactId>mybatis-generator-maven-plugin</artifactId>
7
<version>1.3.2</version>
8
<configuration>
9
<!-- mybatis-generator的配置文件,根据情况调整位置 -->
10
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
11
<verbose>true</verbose>
12
<overwrite>true</overwrite>
13
</configuration>
14
<executions>
15
<execution>
16
<id>Generate MyBatis Artifacts</id>
17
<goals>
18
<goal>generate</goal>
19
</goals>
20
</execution>
21
</executions>
22
<dependencies>
23
<dependency>
24
<groupId>org.mybatis.generator</groupId>
25
<artifactId>mybatis-generator-core</artifactId>
26
<version>1.3.2</version>
27
</dependency>
28
</dependencies>
29
</plugin>
30
</plugins>
31
</build>
3、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="C:/workspace/project/learning/mybatis/lib/mysql-connector-java-5.1.6.jar"/>
<context id="default" targetRuntime="MyBatis3">
<!--创建Java类时是否取消生成注释-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--JDBC数据库连接-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test"
userId="root"
password="dev">
</jdbcConnection>
<!--
Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径
-->
<javaModelGenerator targetPackage="dulk.learn.mybatis.generator.pojo"
targetProject="./src/main/java">
<!-- 是否允许子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="false"/>
<!-- 是否对model添加构造函数 -->
<property name="constructorBased" value="true"/>
<!-- 是否对类CHAR类型的列的数据进行trim操作 -->
<property name="trimStrings" value="true"/>
<!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 -->
<property name="immutable" value="false"/>
</javaModelGenerator>
<!--
mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件
-->
<sqlMapGenerator targetPackage="generator"
targetProject="./src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!--
客户端代码,生成易于使用的针对Model对象和XML配置文件的代码
type="ANNOTATEDMAPPER",生成Java Model和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="dulk.learn.mybatis.generator.mapper"
targetProject="./src/main/java">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!--tables-->
<table tableName="author" domainObjectName="Author"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="book" domainObjectName="Book"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
x
1
2
3
4
5
6
<generatorConfiguration>
7
8
<!--JDBC驱动jar包的位置-->
9
<classPathEntry location="C:/workspace/project/learning/mybatis/lib/mysql-connector-java-5.1.6.jar"/>
10
11
<context id="default" targetRuntime="MyBatis3">
12
13
<!--创建Java类时是否取消生成注释-->
14
<commentGenerator>
15
<property name="suppressDate" value="true"/>
16
<property name="suppressAllComments" value="true"/>
17
</commentGenerator>
18
19
<!--JDBC数据库连接-->
20
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
21
connectionURL="jdbc:mysql://localhost:3306/test"
22
userId="root"
23
password="dev">
24
</jdbcConnection>
25
26
<!--
27
Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
28
targetPackage 指定生成的model生成所在的包名
29
targetProject 指定在该项目下所在的路径
30
-->
31
<javaModelGenerator targetPackage="dulk.learn.mybatis.generator.pojo"
32
targetProject="./src/main/java">
33
<!-- 是否允许子包,即targetPackage.schemaName.tableName -->
34
<property name="enableSubPackages" value="false"/>
35
<!-- 是否对model添加构造函数 -->
36
<property name="constructorBased" value="true"/>
37
<!-- 是否对类CHAR类型的列的数据进行trim操作 -->
38
<property name="trimStrings" value="true"/>
39
<!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 -->
40
<property name="immutable" value="false"/>
41
</javaModelGenerator>
42
43
<!--
44
mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件
45
-->
46
<sqlMapGenerator targetPackage="generator"
47
targetProject="./src/main/resources">
48
<property name="enableSubPackages" value="false"/>
49
</sqlMapGenerator>
50
51
<!--
52
客户端代码,生成易于使用的针对Model对象和XML配置文件的代码
53
type="ANNOTATEDMAPPER",生成Java Model和基于注解的Mapper对象
54
type="MIXEDMAPPER",生成基于注解的Java Model和相应的Mapper对象
55
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
56
-->
57
<javaClientGenerator type="XMLMAPPER"
58
targetPackage="dulk.learn.mybatis.generator.mapper"
59
targetProject="./src/main/java">
60
<property name="enableSubPackages" value="false"/>
61
</javaClientGenerator>
62
63
64
<!--tables-->
65
<table tableName="author" domainObjectName="Author"
66
enableCountByExample="false" enableUpdateByExample="false"
67
enableDeleteByExample="false" enableSelectByExample="false"
68
selectByExampleQueryId="false">
69
</table>
70
<table tableName="book" domainObjectName="Book"
71
enableCountByExample="false" enableUpdateByExample="false"
72
enableDeleteByExample="false" enableSelectByExample="false"
73
selectByExampleQueryId="false">
74
</table>
75
76
</context>
77
</generatorConfiguration>
有些默认配置不必显性去配置,所以简化一下快速配置的话也可以这样:
(注意:配置中文件的生成地址等根据个人实际情况进行调整,另外,若有其他配置想要调整请参考官方文档)
<?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="C:/workspace/project/learning/mybatis/lib/mysql-connector-java-5.1.6.jar"/>
<context id="default" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test"
userId="root"
password="dev">
</jdbcConnection>
<javaModelGenerator targetPackage="dulk.learn.mybatis.generator.pojo"
targetProject="./src/main/java">
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="generator"
targetProject="./src/main/resources">
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="dulk.learn.mybatis.generator.mapper"
targetProject="./src/main/java">
</javaClientGenerator>
<table tableName="author" domainObjectName="Author"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="book" domainObjectName="Book"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
1
2
3
4
5
6
<generatorConfiguration>
7
8
<classPathEntry location="C:/workspace/project/learning/mybatis/lib/mysql-connector-java-5.1.6.jar"/>
9
10
<context id="default" targetRuntime="MyBatis3">
11
12
<commentGenerator>
13
<property name="suppressDate" value="true"/>
14
<property name="suppressAllComments" value="true"/>
15
</commentGenerator>
16
17
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
18
connectionURL="jdbc:mysql://localhost:3306/test"
19
userId="root"
20
password="dev">
21
</jdbcConnection>
22
23
<javaModelGenerator targetPackage="dulk.learn.mybatis.generator.pojo"
24
targetProject="./src/main/java">
25
<property name="trimStrings" value="true"/>
26
</javaModelGenerator>
27
28
<sqlMapGenerator targetPackage="generator"
29
targetProject="./src/main/resources">
30
</sqlMapGenerator>
31
32
<javaClientGenerator type="XMLMAPPER"
33
targetPackage="dulk.learn.mybatis.generator.mapper"
34
targetProject="./src/main/java">
35
</javaClientGenerator>
36
37
<table tableName="author" domainObjectName="Author"
38
enableCountByExample="false" enableUpdateByExample="false"
39
enableDeleteByExample="false" enableSelectByExample="false"
40
selectByExampleQueryId="false">
41
</table>
42
<table tableName="book" domainObjectName="Book"
43
enableCountByExample="false" enableUpdateByExample="false"
44
enableDeleteByExample="false" enableSelectByExample="false"
45
selectByExampleQueryId="false">
46
</table>
47
48
</context>
49
</generatorConfiguration>
另,配置文件中诸如 javaModelGenerator / sqlMapGenerator / javaClientGenerator 等配置的顺序不要轻易改变,可能会导致插件执行时失败。
4、使用方式
mvn mybatis-generator:generate
如果是在IDEA环境中,直接鼠标点击执行即可:
目录结构如下:
注:建表时,字段名称建议用"_"分隔多个单词,比如:author_id...,这样生成的model,属性名称就会变成的驼峰命名,即:authorId