Mybatis Generator【MBG】配置

MBG插件配置

Step1:使用MBG的前提是需要在项目中添加MBG插件。
在项目的pom文件中添加以下插件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!--=====================================================================================================-->
<!--mybatis-generator 逆向工程插件配置 | 如果不配置该插件,逆向工程构件时会报异常-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!--控制台打印执行过程中的日志-->
<verbose>true</verbose>
<!--重复生成时覆盖之前的文件-->
<overwrite>true</overwrite>
<!--指定xml配置文件-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
</configuration>
</plugin>
<!--=====================================================================================================-->

MBG的内容配置

Step2:在项目的resource目录下的新增MBG配置文件,如: generatorConfig.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?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>

<!-- 数据库驱动:选择本地硬盘上的数据库驱动包 | 可以在pom.xml中配置mbg时不用添加数据库连接驱动的配置-->
<classPathEntry
location="/Users/maple/Maple/Tools/Maven/repository/mysql/mysql-connector-java/8.0.21/mysql-connector-java-8.0.21.jar"/>

<!-- strategyContext 是逆向工程的主要配置信息 -->
<!-- id:起个名字 -->
<!-- targetRuntime:设置生成的文件适用的 mybatis 版本 -->
<!--<strategyContext id="DB2Tables" targetRuntime="MyBatis3">-->
<context id="DB2Tables" targetRuntime="MyBatis3">

<!-- 生成的Java文件的编码 -->
<property name="javaFileEncoding" value="UTF-8"/>
<!-- 格式化java代码 -->
<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
<!-- 格式化XML代码 -->
<property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
<!-- beginningDelimiter和endingDelimiter:指明数据库中用于标记数据库对象名的符号,比如O:MYSQL默认是反引号`;ORACLE就是双引号", -->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>

<!--在创建class时,对注释进行控制-->
<commentGenerator>
<!--注释生成的时间戳 true: 否 ; false: 是-->
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 ; false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>

<!-- 数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1/Mybatis"
userId="root"
password="nba106118">
</jdbcConnection>

<!--非必须,类型处理器,在数据库类型和java类型之间进行转换控制 -->
<!--默认情况下数据库中的 decimal,bigInt 在 Java 对应是 sql 下的 BigDecimal 类, 而不是 double 和 long 类型 -->
<!-- 使用常用的基本类型代替 sql 包下的引用类型 -->
<!--默认值为false-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>

<!-- 生成模型的包名和位置-->
<!-- targetPackage:生成的实体类所在的包 -->
<!-- targetProject:生成的实体类所在的位置 -->
<javaModelGenerator targetPackage="com.maple.shudong.modal" targetProject="src/main/java">
<!-- 是否允许子包 -->
<property name="enableSubPackages" value="false"/>
<!-- 是否清理从数据库中查询出的字符串左右两边的空白字符 -->
<property name="trimStrings" value="true"/>
<!-- 是否对modal添加全参构造函数 -->
<property name="constructorBased" value="false"/>
<!-- 建立modal对象是否不可改变 即生成的modal对象不会有setter方法,只有构造方法 -->
<property name="immutable" value="false"/>
</javaModelGenerator>

<!-- 生成映射文件的包名和位置-->
<!-- targetPackage:生成的实体类所在的包 -->
<!-- targetProject:生成的实体类所在的位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<!-- 针对数据库的一个配置,是否把 schema 作为子包名 -->
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>

<!-- 生成Mapper的包名和位置-->
<!-- targetPackage 和 targetProject:生成的 Mapper 文件的包和位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.maple.shudong.dao" targetProject="src/main/java">
<!-- 针对 oracle 数据库的一个配置,是否把 schema 作为字包名 -->
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>

<!-- tableName是数据库中的表名或视图名,domainObjectName是生成的JAVA模型名,后面的参数不用改,
要生成更多的表就在下面继续加table标签 -->
<!--
Mapper中的方法可以通过方法名进行设置: 默认所有配置为true;可按需配置为false;
enableCountByExample="true" : countByExample() 方法
enableUpdateByExample="true" : updateByExample() + updateByExampleSelective() 方法
enableDeleteByExample="true" : deleteByExample() 方法
enableSelectByExample="true" : selectByExample() 方法
enableDeleteByPrimaryKey="true" : deleteByPrimaryKey() 方法
enableInsert="true" : insert() + insertSelevtive() 方法
enableSelectByPrimaryKey="true" : selectByPrimaryKey() 方法
enableUpdateByPrimaryKey="true" : updateByPrimaryKeySelective() + updateByPrimaryKey() 方法
-->

<!--不做处理-->
<!--<table tableName="sd_user" domainObjectName="SdUser"-->
<!-- delimitIdentifiers="true">-->
<!-- <generatedKey column="id" sqlStatement="MYSQL" identity="true" type="post"/>-->
<!--</table>-->

<!--个性化处理-->
<table tableName="sd_click_record" domainObjectName="SdClickRecord" delimitIdentifiers="true"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
<!--主键回填-->
<generatedKey column="id" sqlStatement="MYSQL" identity="true" type="post"/>
<!--忽略列 | 生成modal时会忽略该字段值-->
<!--<ignoreColumn column="create_time"/>-->
<!--<ignoreColumn column="update_time"/>-->
</table>

</context>
</generatorConfiguration>

MBG启动配置

Step3:MBG的启动方式使用Maven命令行,在IDEA下的配置如图:

image-20240314111429641

Maven命令:

1
mybatis-generator:generate -e

上述命令可能会抛以下异常:

1
[ERROR] Could not find goal 'gererate' in plugin org.mybatis.generator:mybatis-generator-maven-plugin

此时,使用以下命令:

1
-Dmybatis.generator.overwrite=true mybatis-generator:generate

MBG运行

Step4:在IDEA的启动配置框中选择上述创建的启动配置,点击运行即可。

image-20240314112146548