介绍
Mybatis Generator(MBG)是Mybatis的一个代码生成工具。MBG解决了对数据库操作有最大影响的一些CRUD操作,很大程度上提升开发效率。如果需要联合查询仍然需要手写sql。相信很多人都听说过微服务,各个微服务之间是松耦合的。每个微服务仅关注于完成一件任务并很好地完成该任务。在一个微服务的开发过程中很可能只关注对单表的操作。所以MBG在开发过程中可以快速的生成代码提升开发效率。
本文将说到在springboot的项目中如何去配置(XML形式和Java配置类形式)和使用MBG以及MBG生成代码的两种方式(XML形式和注解形式),在springboot中更推荐去使用注解的形式。
MBG配置
1.添加依赖
?
|
1
2
3
4
5
|
2.XML配置
配置示例:在新建springboot项目的根目录下创建mbg.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
|
<?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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否自动去除生成注释 true 不生成-->
<property name="suppressAllComments" value="true"/>
<!--生成的注释包含时间戳-->
<!-- <property name="suppressDate" value="true"/> -->
</commentGenerator>
<!-- 数据库连接信息 -->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/definesys"
userId="root"
password="welcome1">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="true"/>
</javaTypeResolver>
<!-- 实体类 bean 带有get和set方法的bean -->
<javaModelGenerator targetPackage="com.mgb.domain" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- sql语句相关的xml或者注解的生成包路径 -->
<sqlMapGenerator targetPackage="com.mgb.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成的接口所在的位置 注解type="ANNOTATEDMAPPER" xml type="XMLMAPPER" -->
<javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="com.mgb.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="user_info" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
<property name="constructorBased" value="false" />
<!-- 数据库表主键 -->
<generatedKey column="id" sqlStatement="JDBC" identity="true" />
</table>
</context>
</generatorConfiguration>
|
配置详解
?
|
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
