avatar

目录
SSM-Mybatis-Generator
  • Mybatis是目前很流行得一个持久层框架,Mybatis得逆向工程会根据我们设计好得数据库表(大前提),自动生成javabean(xxx.java,xxxExample.java), xxxMapper.java(interface), xxxMapper.xml

  • IDEA + Mysql + Java程序 + xml配置文件 + SSM

    这里省略数据库建表得步骤.

Project Construct:

在这里插入图片描述

Step1 导入依赖

向”pom.xml”文件中加入下面的依赖

xml
1
2
3
4
5
6
7


<dependency>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-coreartifactId>
<version>1.3.5version>
dependency>

Step2 XML配置文件示例

在项目中添加”mbg.xml”,可以随意取名,位置随意,只要在核心Java程序(在下面叫:”MBGTest.java”)中写对这个配置文件的路径就可以.

In the most common use case, MyBatis Generator (MBG) is driven by an XML configuration file. The configuration file tells MBG:
  • How to connect to the database
  • What objects to generate, and how to generate them
  • What tables should be used for object generation

Official Website:http://www.mybatis.org/generator/configreference/xmlconfig.html

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

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>
<property name="suppressAllComments" value="true"/>
commentGenerator>

<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/classpie"
userId="root"
password="">
jdbcConnection>


<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
javaTypeResolver>


<javaModelGenerator targetPackage="com.maven.ssm.bean" targetProject=".\src\main\java">

<property name="enableSubPackages" value="true"/>

<property name="trimStrings" value="true"/>
javaModelGenerator>


<sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true"/>
sqlMapGenerator>


<javaClientGenerator type="XMLMAPPER"
targetPackage="com.maven.ssm.dao" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
javaClientGenerator>


<table tableName="classinfo" domainObjectName="ClassInfo">table>
<table tableName="userinfo" domainObjectName="UserInfo">table>
context>

generatorConfiguration>

Step3 Java核心程序

“MBGTest.java”,代码如下,黏贴复制修改一下xml配置文件的路径,没有其他的技术含量.

然后只需要运行一下这个Java程序就成了.

java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class MBGTest {

public static void main(String[] args) throws Exception {
List warnings = new ArrayList();
boolean overwrite = true;
File configFile = new File("mbg.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
callback, warnings);
myBatisGenerator.generate(null);
}

}

Step4 结果

来看一下运行完的结果,项目发生了哪些变化:

在这里插入图片描述

UserInfo.java

java
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
package com.maven.ssm.bean;

public class UserInfo {
private String account;

private String phone;

private String password;

private String name;

private String school;

private Integer role;

private String avatar;

public String getAccount() {
return account;
}

public void setAccount(String account) {
this.account = account == null ? null : account.trim();
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name == null ? null : name.trim();
}

public String getSchool() {
return school;
}

public void setSchool(String school) {
this.school = school == null ? null : school.trim();
}

public Integer getRole() {
return role;
}

public void setRole(Integer role) {
this.role = role;
}

public String getAvatar() {
return avatar;
}

public void setAvatar(String avatar) {
this.avatar = avatar == null ? null : avatar.trim();
}
}

UserInfoExample.java

java
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
package com.maven.ssm.bean;

import java.util.ArrayList;
import java.util.List;

public class UserInfoExample {
protected String orderByClause;

protected boolean distinct;

protected List oredCriteria;

public UserInfoExample() {
oredCriteria = new ArrayList();
}

public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}

public String getOrderByClause() {
return orderByClause;
}

public void setDistinct(boolean distinct) {
this.distinct = distinct;
}

public boolean isDistinct() {
return distinct;
}

public List getOredCriteria() {
return oredCriteria;
}

public void or(Criteria criteria) {
oredCriteria.add(criteria);
}

public Criteria or() {
Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria);
return criteria;
}

public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}
return criteria;
}

protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria();
return criteria;
}

public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
}

protected abstract static class GeneratedCriteria {
protected List criteria;

protected GeneratedCriteria() {
super();
criteria = new ArrayList();
}

public boolean isValid() {
return criteria.size() > 0;
}

public List getAllCriteria() {
return criteria;
}

public List getCriteria() {
return criteria;
}

protected void addCriterion(String condition) {
if (condition == null) {
throw new RuntimeException("Value for condition cannot be null");
}
criteria.add(new Criterion(condition));
}

protected void addCriterion(String condition, Object value, String property) {
if (value == null) {
throw new RuntimeException("Value for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value));
}

protected void addCriterion(String condition, Object value1, Object value2, String property) {
if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value1, value2));
}

public Criteria andAccountIsNull() {
addCriterion("account is null");
return (Criteria) this;
}

public Criteria andAccountIsNotNull() {
addCriterion("account is not null");
return (Criteria) this;
}

public Criteria andAccountEqualTo(String value) {
addCriterion("account =", value, "account");
return (Criteria) this;
}

public Criteria andAccountNotEqualTo(String value) {
addCriterion("account <>", value, "account");
return (Criteria) this;
}

public Criteria andAccountGreaterThan(String value) {
addCriterion("account >", value, "account");
return (Criteria) this;
}

public Criteria andAccountGreaterThanOrEqualTo(String value) {
addCriterion("account >=", value, "account");
return (Criteria) this;
}

public Criteria andAccountLessThan(String value) {
addCriterion("account <", value, "account");
return (Criteria) this;
}

public Criteria andAccountLessThanOrEqualTo(String value) {
addCriterion("account <=", value, "account");
return (Criteria) this;
}

public Criteria andAccountLike(String value) {
addCriterion("account like", value, "account");
return (Criteria) this;
}

public Criteria andAccountNotLike(String value) {
addCriterion("account not like", value, "account");
return (Criteria) this;
}

public Criteria andAccountIn(List values) {
addCriterion("account in", values, "account");
return (Criteria) this;
}

public Criteria andAccountNotIn(List values) {
addCriterion("account not in", values, "account");
return (Criteria) this;
}

public Criteria andAccountBetween(String value1, String value2) {
addCriterion("account between", value1, value2, "account");
return (Criteria) this;
}

public Criteria andAccountNotBetween(String value1, String value2) {
addCriterion("account not between", value1, value2, "account");
return (Criteria) this;
}

public Criteria andPhoneIsNull() {
addCriterion("phone is null");
return (Criteria) this;
}

public Criteria andPhoneIsNotNull() {
addCriterion("phone is not null");
return (Criteria) this;
}

public Criteria andPhoneEqualTo(String value) {
addCriterion("phone =", value, "phone");
return (Criteria) this;
}

public Criteria andPhoneNotEqualTo(String value) {
addCriterion("phone <>", value, "phone");
return (Criteria) this;
}

public Criteria andPhoneGreaterThan(String value) {
addCriterion("phone >", value, "phone");
return (Criteria) this;
}

public Criteria andPhoneGreaterThanOrEqualTo(String value) {
addCriterion("phone >=", value, "phone");
return (Criteria) this;
}

public Criteria andPhoneLessThan(String value) {
addCriterion("phone <", value, "phone");
return (Criteria) this;
}

public Criteria andPhoneLessThanOrEqualTo(String value) {
addCriterion("phone <=", value, "phone");
return (Criteria) this;
}

public Criteria andPhoneLike(String value) {
addCriterion("phone like", value, "phone");
return (Criteria) this;
}

public Criteria andPhoneNotLike(String value) {
addCriterion("phone not like", value, "phone");
return (Criteria) this;
}

public Criteria andPhoneIn(List values) {
addCriterion("phone in", values, "phone");
return (Criteria) this;
}

public Criteria andPhoneNotIn(List values) {
addCriterion("phone not in", values, "phone");
return (Criteria) this;
}

public Criteria andPhoneBetween(String value1, String value2) {
addCriterion("phone between", value1, value2, "phone");
return (Criteria) this;
}

public Criteria andPhoneNotBetween(String value1, String value2) {
addCriterion("phone not between", value1, value2, "phone");
return (Criteria) this;
}

public Criteria andPasswordIsNull() {
addCriterion("password is null");
return (Criteria) this;
}

public Criteria andPasswordIsNotNull() {
addCriterion("password is not null");
return (Criteria) this;
}

public Criteria andPasswordEqualTo(String value) {
addCriterion("password =", value, "password");
return (Criteria) this;
}

public Criteria andPasswordNotEqualTo(String value) {
addCriterion("password <>", value, "password");
return (Criteria) this;
}

public Criteria andPasswordGreaterThan(String value) {
addCriterion("password >", value, "password");
return (Criteria) this;
}

public Criteria andPasswordGreaterThanOrEqualTo(String value) {
addCriterion("password >=", value, "password");
return (Criteria) this;
}

public Criteria andPasswordLessThan(String value) {
addCriterion("password <", value, "password");
return (Criteria) this;
}

public Criteria andPasswordLessThanOrEqualTo(String value) {
addCriterion("password <=", value, "password");
return (Criteria) this;
}

public Criteria andPasswordLike(String value) {
addCriterion("password like", value, "password");
return (Criteria) this;
}

public Criteria andPasswordNotLike(String value) {
addCriterion("password not like", value, "password");
return (Criteria) this;
}

public Criteria andPasswordIn(List values) {
addCriterion("password in", values, "password");
return (Criteria) this;
}

public Criteria andPasswordNotIn(List values) {
addCriterion("password not in", values, "password");
return (Criteria) this;
}

public Criteria andPasswordBetween(String value1, String value2) {
addCriterion("password between", value1, value2, "password");
return (Criteria) this;
}

public Criteria andPasswordNotBetween(String value1, String value2) {
addCriterion("password not between", value1, value2, "password");
return (Criteria) this;
}

public Criteria andNameIsNull() {
addCriterion("name is null");
return (Criteria) this;
}

public Criteria andNameIsNotNull() {
addCriterion("name is not null");
return (Criteria) this;
}

public Criteria andNameEqualTo(String value) {
addCriterion("name =", value, "name");
return (Criteria) this;
}

public Criteria andNameNotEqualTo(String value) {
addCriterion("name <>", value, "name");
return (Criteria) this;
}

public Criteria andNameGreaterThan(String value) {
addCriterion("name >", value, "name");
return (Criteria) this;
}

public Criteria andNameGreaterThanOrEqualTo(String value) {
addCriterion("name >=", value, "name");
return (Criteria) this;
}

public Criteria andNameLessThan(String value) {
addCriterion("name <", value, "name");
return (Criteria) this;
}

public Criteria andNameLessThanOrEqualTo(String value) {
addCriterion("name <=", value, "name");
return (Criteria) this;
}

public Criteria andNameLike(String value) {
addCriterion("name like", value, "name");
return (Criteria) this;
}

public Criteria andNameNotLike(String value) {
addCriterion("name not like", value, "name");
return (Criteria) this;
}

public Criteria andNameIn(List values) {
addCriterion("name in", values, "name");
return (Criteria) this;
}

public Criteria andNameNotIn(List values) {
addCriterion("name not in", values, "name");
return (Criteria) this;
}

public Criteria andNameBetween(String value1, String value2) {
addCriterion("name between", value1, value2, "name");
return (Criteria) this;
}

public Criteria andNameNotBetween(String value1, String value2) {
addCriterion("name not between", value1, value2, "name");
return (Criteria) this;
}

public Criteria andSchoolIsNull() {
addCriterion("school is null");
return (Criteria) this;
}

public Criteria andSchoolIsNotNull() {
addCriterion("school is not null");
return (Criteria) this;
}

public Criteria andSchoolEqualTo(String value) {
addCriterion("school =", value, "school");
return (Criteria) this;
}

public Criteria andSchoolNotEqualTo(String value) {
addCriterion("school <>", value, "school");
return (Criteria) this;
}

public Criteria andSchoolGreaterThan(String value) {
addCriterion("school >", value, "school");
return (Criteria) this;
}

public Criteria andSchoolGreaterThanOrEqualTo(String value) {
addCriterion("school >=", value, "school");
return (Criteria) this;
}

public Criteria andSchoolLessThan(String value) {
addCriterion("school <", value, "school");
return (Criteria) this;
}

public Criteria andSchoolLessThanOrEqualTo(String value) {
addCriterion("school <=", value, "school");
return (Criteria) this;
}

public Criteria andSchoolLike(String value) {
addCriterion("school like", value, "school");
return (Criteria) this;
}

public Criteria andSchoolNotLike(String value) {
addCriterion("school not like", value, "school");
return (Criteria) this;
}

public Criteria andSchoolIn(List values) {
addCriterion("school in", values, "school");
return (Criteria) this;
}

public Criteria andSchoolNotIn(List values) {
addCriterion("school not in", values, "school");
return (Criteria) this;
}

public Criteria andSchoolBetween(String value1, String value2) {
addCriterion("school between", value1, value2, "school");
return (Criteria) this;
}

public Criteria andSchoolNotBetween(String value1, String value2) {
addCriterion("school not between", value1, value2, "school");
return (Criteria) this;
}

public Criteria andRoleIsNull() {
addCriterion("role is null");
return (Criteria) this;
}

public Criteria andRoleIsNotNull() {
addCriterion("role is not null");
return (Criteria) this;
}

public Criteria andRoleEqualTo(Integer value) {
addCriterion("role =", value, "role");
return (Criteria) this;
}

public Criteria andRoleNotEqualTo(Integer value) {
addCriterion("role <>", value, "role");
return (Criteria) this;
}

public Criteria andRoleGreaterThan(Integer value) {
addCriterion("role >", value, "role");
return (Criteria) this;
}

public Criteria andRoleGreaterThanOrEqualTo(Integer value) {
addCriterion("role >=", value, "role");
return (Criteria) this;
}

public Criteria andRoleLessThan(Integer value) {
addCriterion("role <", value, "role");
return (Criteria) this;
}

public Criteria andRoleLessThanOrEqualTo(Integer value) {
addCriterion("role <=", value, "role");
return (Criteria) this;
}

public Criteria andRoleIn(List values) {
addCriterion("role in", values, "role");
return (Criteria) this;
}

public Criteria andRoleNotIn(List values) {
addCriterion("role not in", values, "role");
return (Criteria) this;
}

public Criteria andRoleBetween(Integer value1, Integer value2) {
addCriterion("role between", value1, value2, "role");
return (Criteria) this;
}

public Criteria andRoleNotBetween(Integer value1, Integer value2) {
addCriterion("role not between", value1, value2, "role");
return (Criteria) this;
}

public Criteria andAvatarIsNull() {
addCriterion("avatar is null");
return (Criteria) this;
}

public Criteria andAvatarIsNotNull() {
addCriterion("avatar is not null");
return (Criteria) this;
}

public Criteria andAvatarEqualTo(String value) {
addCriterion("avatar =", value, "avatar");
return (Criteria) this;
}

public Criteria andAvatarNotEqualTo(String value) {
addCriterion("avatar <>", value, "avatar");
return (Criteria) this;
}

public Criteria andAvatarGreaterThan(String value) {
addCriterion("avatar >", value, "avatar");
return (Criteria) this;
}

public Criteria andAvatarGreaterThanOrEqualTo(String value) {
addCriterion("avatar >=", value, "avatar");
return (Criteria) this;
}

public Criteria andAvatarLessThan(String value) {
addCriterion("avatar <", value, "avatar");
return (Criteria) this;
}

public Criteria andAvatarLessThanOrEqualTo(String value) {
addCriterion("avatar <=", value, "avatar");
return (Criteria) this;
}

public Criteria andAvatarLike(String value) {
addCriterion("avatar like", value, "avatar");
return (Criteria) this;
}

public Criteria andAvatarNotLike(String value) {
addCriterion("avatar not like", value, "avatar");
return (Criteria) this;
}

public Criteria andAvatarIn(List values) {
addCriterion("avatar in", values, "avatar");
return (Criteria) this;
}

public Criteria andAvatarNotIn(List values) {
addCriterion("avatar not in", values, "avatar");
return (Criteria) this;
}

public Criteria andAvatarBetween(String value1, String value2) {
addCriterion("avatar between", value1, value2, "avatar");
return (Criteria) this;
}

public Criteria andAvatarNotBetween(String value1, String value2) {
addCriterion("avatar not between", value1, value2, "avatar");
return (Criteria) this;
}
}

public static class Criteria extends GeneratedCriteria {

protected Criteria() {
super();
}
}

public static class Criterion {
private String condition;

private Object value;

private Object secondValue;

private boolean noValue;

private boolean singleValue;

private boolean betweenValue;

private boolean listValue;

private String typeHandler;

public String getCondition() {
return condition;
}

public Object getValue() {
return value;
}

public Object getSecondValue() {
return secondValue;
}

public boolean isNoValue() {
return noValue;
}

public boolean isSingleValue() {
return singleValue;
}

public boolean isBetweenValue() {
return betweenValue;
}

public boolean isListValue() {
return listValue;
}

public String getTypeHandler() {
return typeHandler;
}

protected Criterion(String condition) {
super();
this.condition = condition;
this.typeHandler = null;
this.noValue = true;
}

protected Criterion(String condition, Object value, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.typeHandler = typeHandler;
if (value instanceof List) {
this.listValue = true;
} else {
this.singleValue = true;
}
}

protected Criterion(String condition, Object value) {
this(condition, value, null);
}

protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.secondValue = secondValue;
this.typeHandler = typeHandler;
this.betweenValue = true;
}

protected Criterion(String condition, Object value, Object secondValue) {
this(condition, value, secondValue, null);
}
}
}

UserInfoMapper.java

java
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
package com.maven.ssm.dao;

import com.maven.ssm.bean.UserInfo;
import com.maven.ssm.bean.UserInfoExample;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface UserInfoMapper {
long countByExample(UserInfoExample example);

int deleteByExample(UserInfoExample example);

int deleteByPrimaryKey(String account);

int insert(UserInfo record);

int insertSelective(UserInfo record);

List selectByExample(UserInfoExample example);

UserInfo selectByPrimaryKey(String account);

int updateByExampleSelective(@Param("record") UserInfo record, @Param("example") UserInfoExample example);

int updateByExample(@Param("record") UserInfo record, @Param("example") UserInfoExample example);

int updateByPrimaryKeySelective(UserInfo record);

int updateByPrimaryKey(UserInfo record);
}

UserInfoMapper.xml

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
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

mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.maven.ssm.dao.UserInfoMapper">
<resultMap id="BaseResultMap" type="com.maven.ssm.bean.UserInfo">
<id column="account" jdbcType="VARCHAR" property="account" />
<result column="phone" jdbcType="VARCHAR" property="phone" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="school" jdbcType="VARCHAR" property="school" />
<result column="role" jdbcType="INTEGER" property="role" />
<result column="avatar" jdbcType="VARCHAR" property="avatar" />
resultMap>
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<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 close=")" collection="criterion.value" item="listItem" open="(" 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="(" prefixOverrides="and" suffix=")">
<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 close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
foreach>
when>
choose>
foreach>
trim>
if>
foreach>
where>
sql>
<sql id="Base_Column_List">
account, phone, password, name, school, role, avatar
sql>
<select id="selectByExample" parameterType="com.maven.ssm.bean.UserInfoExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
if>
<include refid="Base_Column_List" />
from userinfo
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
if>
<if test="orderByClause != null">
order by ${orderByClause}
if>
select>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from userinfo
where account = #{account,jdbcType=VARCHAR}
select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
delete from userinfo
where account = #{account,jdbcType=VARCHAR}
delete>
<delete id="deleteByExample" parameterType="com.maven.ssm.bean.UserInfoExample">
delete from userinfo
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
if>
delete>
<insert id="insert" parameterType="com.maven.ssm.bean.UserInfo">
insert into userinfo (account, phone, password,
name, school, role,
avatar)
values (#{account,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{name,jdbcType=VARCHAR}, #{school,jdbcType=VARCHAR}, #{role,jdbcType=INTEGER},
#{avatar,jdbcType=VARCHAR})
insert>
<insert id="insertSelective" parameterType="com.maven.ssm.bean.UserInfo">
insert into userinfo
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="account != null">
account,
if>
<if test="phone != null">
phone,
if>
<if test="password != null">
password,
if>
<if test="name != null">
name,
if>
<if test="school != null">
school,
if>
<if test="role != null">
role,
if>
<if test="avatar != null">
avatar,
if>
trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="account != null">
#{account,jdbcType=VARCHAR},
if>
<if test="phone != null">
#{phone,jdbcType=VARCHAR},
if>
<if test="password != null">
#{password,jdbcType=VARCHAR},
if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
if>
<if test="school != null">
#{school,jdbcType=VARCHAR},
if>
<if test="role != null">
#{role,jdbcType=INTEGER},
if>
<if test="avatar != null">
#{avatar,jdbcType=VARCHAR},
if>
trim>
insert>
<select id="countByExample" parameterType="com.maven.ssm.bean.UserInfoExample" resultType="java.lang.Long">
select count(*) from userinfo
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
if>
select>
<update id="updateByExampleSelective" parameterType="map">
update userinfo
<set>
<if test="record.account != null">
account = #{record.account,jdbcType=VARCHAR},
if>
<if test="record.phone != null">
phone = #{record.phone,jdbcType=VARCHAR},
if>
<if test="record.password != null">
password = #{record.password,jdbcType=VARCHAR},
if>
<if test="record.name != null">
name = #{record.name,jdbcType=VARCHAR},
if>
<if test="record.school != null">
school = #{record.school,jdbcType=VARCHAR},
if>
<if test="record.role != null">
role = #{record.role,jdbcType=INTEGER},
if>
<if test="record.avatar != null">
avatar = #{record.avatar,jdbcType=VARCHAR},
if>
set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
if>
update>
<update id="updateByExample" parameterType="map">
update userinfo
set account = #{record.account,jdbcType=VARCHAR},
phone = #{record.phone,jdbcType=VARCHAR},
password = #{record.password,jdbcType=VARCHAR},
name = #{record.name,jdbcType=VARCHAR},
school = #{record.school,jdbcType=VARCHAR},
role = #{record.role,jdbcType=INTEGER},
avatar = #{record.avatar,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
if>
update>
<update id="updateByPrimaryKeySelective" parameterType="com.maven.ssm.bean.UserInfo">
update userinfo
<set>
<if test="phone != null">
phone = #{phone,jdbcType=VARCHAR},
if>
<if test="password != null">
password = #{password,jdbcType=VARCHAR},
if>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
if>
<if test="school != null">
school = #{school,jdbcType=VARCHAR},
if>
<if test="role != null">
role = #{role,jdbcType=INTEGER},
if>
<if test="avatar != null">
avatar = #{avatar,jdbcType=VARCHAR},
if>
set>
where account = #{account,jdbcType=VARCHAR}
update>
<update id="updateByPrimaryKey" parameterType="com.maven.ssm.bean.UserInfo">
update userinfo
set phone = #{phone,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
name = #{name,jdbcType=VARCHAR},
school = #{school,jdbcType=VARCHAR},
role = #{role,jdbcType=INTEGER},
avatar = #{avatar,jdbcType=VARCHAR}
where account = #{account,jdbcType=VARCHAR}
update>
mapper>

Step5 测试一下Mapper

现在,测试一下看看xml实现文件是否可用,这里用插入一条数据才判断

测试之前,先记录一下数据库

在这里插入图片描述

在com.maven.ssm.test包下创建MapperTest.java文件,代码如下:

java
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
package com.maven.ssm.test;

import com.maven.ssm.bean.UserInfo;
import com.maven.ssm.dao.UserInfoMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class MapperTest {

@Autowired
UserInfoMapper userInfoMapper;

@Test
public void insertUserTest(){
UserInfo userInfo = new UserInfo();
userInfo.setAccount("11111111");
userInfo.setPhone("15668055238");
userInfo.setPassword("q123123");
userInfo.setName("TestName");
userInfo.setSchool("JustGuess");
userInfo.setRole(0);
int result = userInfoMapper.insertSelective(userInfo);
if (result>0){
System.out.println("insert user success!");
} else {
System.out.println("insert user failed!");
}
}
}

运行结果如下:

1.控制台输出结果:

在这里插入图片描述

2.数据库表中数据的变化:

在这里插入图片描述

Over

有问题请在留言中给出

That’s all,thank you.

文章作者: XiaoMing
文章链接: https://xiaoming0929.github.io/2020/04/02/mybatis-generator/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 XiaoMing's Blog
打赏
  • 微信
    微信
  • 支付寶
    支付寶