King's Studio

MyBatis-generator的使用

字数统计: 2.7k阅读时长: 14 min
2019/10/05 Share

MyBatis生成器(MBG)是MyBatisiBATIS的代码生成器。它为MyBatis的所有版本以及iBATIS生成代码。它将一个数据库表(或多个表),生成可用于访问表的组件。这减轻了设置对象和配置文件与数据库表进行交互的麻烦。MBG试图对简单CRUD(创建,检索,更新,删除)的大部分数据库操作进行简化。但开发者仍将需要手工编写SQL语句和对象代码以进行联接查询或存储过程。MyBatis-generator是由MyBatis官方提供的技术,现在网上有很多利用Maven导入的MyBatis-generator插件使用的教程,在此我想对操作进行一些简化,直接利用mybatis-generator-core的jar包进行操作,方便演示整个执行的过程。

搭建Mysql数据库的环境

首先在你的数据库中创建一张用户表,使用MyBatis-generator帮我们自动创建entity类、mapper文件以及dao接口并且帮我们配置好它们的依赖关系。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
create table  t_user
(
user_id int primary key auto_increment /*用户id*/,
user_email VARCHAR(50) /*邮箱*/,
user_name VARCHAR(50) /*昵称*/,
user_pwd VARCHAR(50) /*密码*/,
user_sex VARCHAR(50) /*性别*/,
user_city VARCHAR(50) /*所在城市*/,
user_avatar VARCHAR(50) /*头像*/,
user_motto VARCHAR(50) /*个性签名*/,
user_money int /*账户余额*/,
user_status int /*用户状态 1:正常2:禁用*/,
userCreate_time DATE /*用户创建时间 默认为当前时间*/
);

MyBatis-generator最核心的配置在于generatorConfig.xml文件的配置,此文件用于规定我们连接的是哪一个数据库,哪一张表,以及生成的策略是什么。

1
2
<!--首先在mybatis-generator-core核心jar包的同级目录下加入数据库连接的jar包,指定连接jar包的位置-->
<classPathEntry location="mysql-connector-java-5.1.18-bin.jar" />

然后是数据库连接信息的配置,同样oracle的连接也需要加入对应的ojdbc的jar包以及修改连接信息。

1
2
3
4
5
6
<!--mysql的连接信息-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://129.204.207.233:3306/qa"
userId="root"
password="xxxxxx">
</jdbcConnection>

附上完整的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
<?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="mysql-connector-java-5.1.18-bin.jar" />

<context id="mysql" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<plugin type="org.mybatis.generator.plugins.MapperConfigPlugin">
<property name="targetPackage" value="."/>
<property name="targetProject" value="src"/>
</plugin>

<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--mysql的连接信息-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://129.204.207.233:3306/qa"
userId="root"
password="xxxxxx">
</jdbcConnection>

<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--生成实体类的位置-->
<javaModelGenerator targetPackage="entity" targetProject="src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--生成Mapperxml的位置-->
<sqlMapGenerator targetPackage="mapper" targetProject="src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--生成dao接口的位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="dao" targetProject="src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>

<table schema="ums" tableName="t_user" domainObjectName="User">
<!-- 忽略外键的列,不配置则直接生成id,而不是对象,对象需要自己手动添加 -->
<!-- <ignoreColumn column="class_id"></ignoreColumn> -->
</table>

</context>
</generatorConfiguration>

执行java -jar命令生成代码

需要在mybatis-generator-core-1.3.2.jar的同级目录下执行该命令,然后Mybatis-generator会根据配置好的generatorConfig.xml生成对应的代码。

1
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

如果生成成功,会有下面的提示。

1
MyBatis Generator finished successfully.

整个测试项目的结构

mybatis

生成代码目录的结构:

mybatis2

实际开发中,我们通常会使用MyBatis-generator为我们配置好表和实体类的对应关系,而生成的Mapper.xml完全符合开发的代码规范,它已经帮我们做好了resultMap的配置,示例如下:

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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="dao.UserMapper" >
<resultMap id="BaseResultMap" type="entity.User" >
<id column="user_id" property="userId" jdbcType="INTEGER" />
<result column="user_email" property="userEmail" jdbcType="VARCHAR" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="user_pwd" property="userPwd" jdbcType="VARCHAR" />
<result column="user_sex" property="userSex" jdbcType="VARCHAR" />
<result column="user_city" property="userCity" jdbcType="VARCHAR" />
<result column="user_avatar" property="userAvatar" jdbcType="VARCHAR" />
<result column="user_motto" property="userMotto" jdbcType="VARCHAR" />
<result column="user_money" property="userMoney" jdbcType="INTEGER" />
<result column="user_status" property="userStatus" jdbcType="INTEGER" />
<result column="userCreate_time" property="usercreateTime" jdbcType="DATE" />
</resultMap>
<sql id="Example_Where_Clause" >
<where >
<foreach collection="oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause" >
<where >
<foreach collection="example.oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List" >
user_id, user_email, user_name, user_pwd, user_sex, user_city, user_avatar, user_motto,
user_money, user_status, userCreate_time
</sql>
<select id="selectByExample" resultMap="BaseResultMap" parameterType="entity.UserExample" >
select
<if test="distinct" >
distinct
</if>
<include refid="Base_Column_List" />
from t_user
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from t_user
where user_id = #{userId,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from t_user
where user_id = #{userId,jdbcType=INTEGER}
</delete>
<delete id="deleteByExample" parameterType="entity.UserExample" >
delete from t_user
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="entity.User" >
insert into t_user (user_id, user_email, user_name,
user_pwd, user_sex, user_city,
user_avatar, user_motto, user_money,
user_status, userCreate_time)
values (#{userId,jdbcType=INTEGER}, #{userEmail,jdbcType=VARCHAR}, #{userName,jdbcType=VARCHAR},
#{userPwd,jdbcType=VARCHAR}, #{userSex,jdbcType=VARCHAR}, #{userCity,jdbcType=VARCHAR},
#{userAvatar,jdbcType=VARCHAR}, #{userMotto,jdbcType=VARCHAR}, #{userMoney,jdbcType=INTEGER},
#{userStatus,jdbcType=INTEGER}, #{usercreateTime,jdbcType=DATE})
</insert>
<insert id="insertSelective" parameterType="entity.User" >
insert into t_user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="userId != null" >
user_id,
</if>
<if test="userEmail != null" >
user_email,
</if>
<if test="userName != null" >
user_name,
</if>
<if test="userPwd != null" >
user_pwd,
</if>
<if test="userSex != null" >
user_sex,
</if>
<if test="userCity != null" >
user_city,
</if>
<if test="userAvatar != null" >
user_avatar,
</if>
<if test="userMotto != null" >
user_motto,
</if>
<if test="userMoney != null" >
user_money,
</if>
<if test="userStatus != null" >
user_status,
</if>
<if test="usercreateTime != null" >
userCreate_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="userId != null" >
#{userId,jdbcType=INTEGER},
</if>
<if test="userEmail != null" >
#{userEmail,jdbcType=VARCHAR},
</if>
<if test="userName != null" >
#{userName,jdbcType=VARCHAR},
</if>
<if test="userPwd != null" >
#{userPwd,jdbcType=VARCHAR},
</if>
<if test="userSex != null" >
#{userSex,jdbcType=VARCHAR},
</if>
<if test="userCity != null" >
#{userCity,jdbcType=VARCHAR},
</if>
<if test="userAvatar != null" >
#{userAvatar,jdbcType=VARCHAR},
</if>
<if test="userMotto != null" >
#{userMotto,jdbcType=VARCHAR},
</if>
<if test="userMoney != null" >
#{userMoney,jdbcType=INTEGER},
</if>
<if test="userStatus != null" >
#{userStatus,jdbcType=INTEGER},
</if>
<if test="usercreateTime != null" >
#{usercreateTime,jdbcType=DATE},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="entity.UserExample" resultType="java.lang.Integer" >
select count(*) from t_user
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map" >
update t_user
<set >
<if test="record.userId != null" >
user_id = #{record.userId,jdbcType=INTEGER},
</if>
<if test="record.userEmail != null" >
user_email = #{record.userEmail,jdbcType=VARCHAR},
</if>
<if test="record.userName != null" >
user_name = #{record.userName,jdbcType=VARCHAR},
</if>
<if test="record.userPwd != null" >
user_pwd = #{record.userPwd,jdbcType=VARCHAR},
</if>
<if test="record.userSex != null" >
user_sex = #{record.userSex,jdbcType=VARCHAR},
</if>
<if test="record.userCity != null" >
user_city = #{record.userCity,jdbcType=VARCHAR},
</if>
<if test="record.userAvatar != null" >
user_avatar = #{record.userAvatar,jdbcType=VARCHAR},
</if>
<if test="record.userMotto != null" >
user_motto = #{record.userMotto,jdbcType=VARCHAR},
</if>
<if test="record.userMoney != null" >
user_money = #{record.userMoney,jdbcType=INTEGER},
</if>
<if test="record.userStatus != null" >
user_status = #{record.userStatus,jdbcType=INTEGER},
</if>
<if test="record.usercreateTime != null" >
userCreate_time = #{record.usercreateTime,jdbcType=DATE},
</if>
</set>
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map" >
update t_user
set user_id = #{record.userId,jdbcType=INTEGER},
user_email = #{record.userEmail,jdbcType=VARCHAR},
user_name = #{record.userName,jdbcType=VARCHAR},
user_pwd = #{record.userPwd,jdbcType=VARCHAR},
user_sex = #{record.userSex,jdbcType=VARCHAR},
user_city = #{record.userCity,jdbcType=VARCHAR},
user_avatar = #{record.userAvatar,jdbcType=VARCHAR},
user_motto = #{record.userMotto,jdbcType=VARCHAR},
user_money = #{record.userMoney,jdbcType=INTEGER},
user_status = #{record.userStatus,jdbcType=INTEGER},
userCreate_time = #{record.usercreateTime,jdbcType=DATE}
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="entity.User" >
update t_user
<set >
<if test="userEmail != null" >
user_email = #{userEmail,jdbcType=VARCHAR},
</if>
<if test="userName != null" >
user_name = #{userName,jdbcType=VARCHAR},
</if>
<if test="userPwd != null" >
user_pwd = #{userPwd,jdbcType=VARCHAR},
</if>
<if test="userSex != null" >
user_sex = #{userSex,jdbcType=VARCHAR},
</if>
<if test="userCity != null" >
user_city = #{userCity,jdbcType=VARCHAR},
</if>
<if test="userAvatar != null" >
user_avatar = #{userAvatar,jdbcType=VARCHAR},
</if>
<if test="userMotto != null" >
user_motto = #{userMotto,jdbcType=VARCHAR},
</if>
<if test="userMoney != null" >
user_money = #{userMoney,jdbcType=INTEGER},
</if>
<if test="userStatus != null" >
user_status = #{userStatus,jdbcType=INTEGER},
</if>
<if test="usercreateTime != null" >
userCreate_time = #{usercreateTime,jdbcType=DATE},
</if>
</set>
where user_id = #{userId,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="entity.User" >
update t_user
set user_email = #{userEmail,jdbcType=VARCHAR},
user_name = #{userName,jdbcType=VARCHAR},
user_pwd = #{userPwd,jdbcType=VARCHAR},
user_sex = #{userSex,jdbcType=VARCHAR},
user_city = #{userCity,jdbcType=VARCHAR},
user_avatar = #{userAvatar,jdbcType=VARCHAR},
user_motto = #{userMotto,jdbcType=VARCHAR},
user_money = #{userMoney,jdbcType=INTEGER},
user_status = #{userStatus,jdbcType=INTEGER},
userCreate_time = #{usercreateTime,jdbcType=DATE}
where user_id = #{userId,jdbcType=INTEGER}
</update>
<select resultMap="BaseResultMap" parameterType="entity.UserExample" id="selectByExampleWithRowbounds" >
select
<if test="distinct" >
distinct
</if>
<include refid="Base_Column_List" />
from t_user
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
</mapper>

原文作者:金奇

原文链接:https://www.rossontheway.com/2019/10/05/MyBatis-generator的使用/

发表日期:October 5th 2019, 12:00:00 am

更新日期:October 5th 2019, 12:10:45 pm

版权声明:本文采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可,除特别声明外,转载请注明出处!

CATALOG
  1. 1. 搭建Mysql数据库的环境
  2. 2. 执行java -jar命令生成代码
  3. 3. 整个测试项目的结构