使用 MyBatis Generator 生成代码

MyBatis Generator (MBG) 是一个Mybatis的代码生成器,可以根据数据库表结构自动生成 bean 实体类,mapper 接口和 mapper 映射文件,大大简化我们的开发流程。

这里使用基于 SpringBoot 项目,直接使用 Maven 来配置插件。

Maven 配置

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>pers.codingfanlt</groupId>
<artifactId>housekeeping</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Housekeeping</name>
<description>A small project back-end service</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>


······


</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-maven-plugin -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
</dependency>
</dependencies>
</plugin>

</plugins>
</build>

</project>

MyBatis Generator 配置

generatorConfig.xml , 放在 resource 目录下

这里主要配置了数据库信息以及生成的代码的存放位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?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">
<!-- mybatis-generator的核心配置文件 -->
<generatorConfiguration>

<context id="DB2Tables" targetRuntime="MyBatis3">

<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://xxx.xxx.xxx:3306/db_name"
userId="username"
password="password">
<!--产生selectByPrimaryKey-->
<property name="useInformationSchema" value="true"/>
</jdbcConnection>


<!--指定生成的类型为java类型,避免数据库中number等类型字段 -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>

<!--自动生成的实体的存放包路径 -->
<javaModelGenerator targetPackage="pers.codingfanlt.housekeeping.bean" targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>

<!--自动生成的*Mapper.xml文件存放路径 -->
<sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>

<!--自动生成的*Mapper.java存放路径 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="pers.codingfanlt.housekeeping.mapperZ"
targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>


<!-- 映射配置 -->
<table tableName="category" domainObjectName="Category"></table>
<table tableName="product" domainObjectName="Product"></table>
<table tableName="waiter" domainObjectName="Waiter"></table>
<table tableName="customer" domainObjectName="Customer"></table>
<table tableName="address" domainObjectName="Address"></table>
<table tableName="order" domainObjectName="Order"></table>
<table tableName="order_line" domainObjectName="OrderLine"></table>
<table tableName="comment" domainObjectName="Comment"></table>


<!--
mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate
-->


</context>
</generatorConfiguration>

生成代码

配置完成后,使用命令 mvn mybatis-generator:generate -e 进行代码生成,或双击 IDEA Maven 侧边栏中的
mybatis-generator:generate

image.png

结果

image.png
image.png
image.png

注意,vo 对象以及多表操作的代码还是要手动写的