通过mybatis工具generatorConfig.xml自动生成实体,DAO,映射文件

简介

  Mybatis属于半自动ORM,可以利用mybatis工具generatorConfig.xml自动生成DAO、实体、映射文件的方式来代替手动书写的方式,这样既提高了工作效率也可以在项目避免出现的一些细微难调试的BUG。

前提条件:

1、需要准备的第三方jar包为:

mybatis-generator-core-1.3.2.jarmysql-connector-java-5.1.39-bin.jar

其中mybatis-generator-core-1.3.2.jar的下载地址为:

https://github.com/mybatis/generator/releases,

mysql-connector-java-5.1.39-bin.jar的下载地址为:

https://dev.mysql.com/downloads/connector/j/

2、项目自身的generatorConfig.xml文件需要和mybatis-generator-core-1.3.2.jar必须在同一个目录下。比如我的项目中对应的目录和文件为:

wKiom1dMHOPC6HJUAABzmYaiyWs263.jpg-wh_50


操作步骤:

1、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
<? 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:数据库的JDBC驱动的jar包地址--> 
     < classPathEntry  location = "E:\jar\mysql-connector-java-5.1.39\mysql-connector-java-5.1.39\mysql-connector-java-5.1.39-bin.jar"  />
< context  id = "MysqlTables"  targetRuntime = "MyBatis3" >
<!-- 注释 -->  
< commentGenerator >
< property  name = "suppressAllComments"  value = "true" />  <!-- 是否取消注释 --> 
< property  name = "suppressDate"  value = "true"  />  <!-- 是否生成注释代时间戳-->  
</ commentGenerator >
<!-- JDBC连接 -->  
< jdbcConnection  driverClass = "com.mysql.jdbc.Driver" 
connectionURL = "jdbc:mysql://127.0.0.1:3306/jycps?useUnicode=true&amp;characterEncoding=UTF-8"
userId = "root"
password = "root" >
</ jdbcConnection >
<!-- 类型转换 -->  
< javaTypeResolver  >
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->  
< property  name = "forceBigDecimals"  value = "false"  />
</ javaTypeResolver >
<!-- 生成实体类地址 -->    
< javaModelGenerator  targetPackage = "com.jiayou.cps.pojo"  targetProject = "D:\workspace\jy_cps\jy_cps\src\main\java" >
< property  name = "enableSubPackages"  value = "true"  />  <!-- 是否在当前路径下新加一层-->
< property  name = "trimStrings"  value = "true"  />  <!-- 是否针对string类型的字段在set的时候进行trim调用 -->
</ javaModelGenerator >
<!-- 生成MAPXML文件 -->
< sqlMapGenerator  targetPackage = "sqlmap/test"   targetProject = "D:\workspace\jy_cps\jy_cps\src\main\resources" >
< property  name = "enableSubPackages"  value = "true"  />  <!-- 是否在当前路径下新加一层-->
</ sqlMapGenerator >
<!-- 生成DAO -->      
< javaClientGenerator  type = "XMLMAPPER"  targetPackage = "com.jiayou.cps.dao"   targetProject = "D:\workspace\jy_cps\jy_cps\src\main\java" >
< property  name = "enableSubPackages"  value = "true"  />  <!-- 是否在当前路径下新加一层-->
</ javaClientGenerator >
<!-- 配置表信息 -->
< table  schema = ""  tableName = "tb_test"  domainObjectName = "Test" 
    enableCountByExample = "true" 
    enableUpdateByExample = "true" 
    enableDeleteByExample = "true" 
    enableSelectByExample = "true" 
    selectByExampleQueryId = "true"  >
</ table >
</ context >
</ generatorConfiguration >

注意事项:

1)上述配置的XML文件千万不要有注释!暂时在我测试时是这个样子的,可能在执行生成实体、DAO、映射文件时会报以下错误:

wKioL1dMJgfjiB-MAABULLluWkE177.jpg-wh_50

2)<classPathEntry location="E:\jar\mysql-connector-java-5.1.39\mysql-connector-java-5.1.39\mysql-connector-java-5.1.39-bin.jar" />中的mysql-connector-java-5.1.39-bin.jar版本一定要跟你项目中mysql的jar包版本一致,不然在执行生成实体、DAO、映射文件时可能会报下述错误,这个以前在java培训机构学习的时候,老师特意强调过:

wKiom1dMJOvQ1jbCAAH0UNrHxuM074.jpg-wh_50

3)生成DAO、实体、映射文件的路径要规范好,我自个的配置同上述generatorConfig.xml的配置,我的项目的基本目录结构为:

wKiom1dMIIyxOqGVAAA54QpM2ac633.jpg-wh_50

2、执行生成DAO、实体、映射文件的操作。

1)进入到项目对应generatorConfig.xml文件的路径下。

wKiom1dMHOPC6HJUAABzmYaiyWs263.jpg-wh_50

2)在该目录按住Shift,右键鼠标选择"在此处打开命令窗口"。

把生成文件的语句“java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite”复制到DOS命令行中,回车等待生成结果。

wKiom1dMJaGRSNsLAADCzv0-dKU991.jpg-wh_50

一般出现上述图片中的内容就基本上没问题。我的项目中对应生成的文件列表为:

wKioL1dMJAiAqUgVAACJ8W3Z4L8103.jpg-wh_50

上述标注蓝色勾状的文件是通过上述命令新生成的

其中新生成的文件内容分别为:

TestMapper 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package  com.jiayou.cps.dao;
import  com.jiayou.cps.pojo.Test;
import  com.jiayou.cps.pojo.TestExample;
import  java.util.List;
import  org.apache.ibatis.annotations.Param;
public  interface  TestMapper {
     int  countByExample(TestExample example);
     int  deleteByExample(TestExample example);
     int  deleteByPrimaryKey(Integer tbId);
     int  insert(Test record);
     int  insertSelective(Test record);
     List<Test> selectByExample(TestExample example);
     Test selectByPrimaryKey(Integer tbId);
     int  updateByExampleSelective( @Param ( "record" ) Test record,  @Param ( "example" ) TestExample example);
     int  updateByExample( @Param ( "record" ) Test record,  @Param ( "example" ) TestExample example);
     int  updateByPrimaryKeySelective(Test record);
     int  updateByPrimaryKey(Test record);
}


Test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package  com.jiayou.cps.pojo;
public  class  Test {
     private  Integer tbId;
     private  String tbName;
     public  Integer getTbId() {
         return  tbId;
     }
     public  void  setTbId(Integer tbId) {
         this .tbId = tbId;
     }
     public  String getTbName() {
         return  tbName;
     }
     public  void  setTbName(String tbName) {
         this .tbName = tbName ==  null  null  : tbName.trim();
     }
}


TestExample

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
package  com.jiayou.cps.pojo;
import  java.util.ArrayList;
import  java.util.List;
import  com.jiayou.cps.mybatis.page.BaseExample;
public  class  TestExample  extends  BaseExample{
     protected  String orderByClause;
     protected  boolean  distinct;
     protected  List<Criteria> oredCriteria;
     public  TestExample() {
         oredCriteria =  new  ArrayList<Criteria>();
     }
     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<Criteria> 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<Criterion> criteria;
         protected  GeneratedCriteria() {
             super ();
             criteria =  new  ArrayList<Criterion>();
         }
         public  boolean  isValid() {
             return  criteria.size() >  0 ;
         }
         public  List<Criterion> getAllCriteria() {
             return  criteria;
         }
         public  List<Criterion> 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 andTbIdIsNull() {
             addCriterion( "tb_id is null" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbIdIsNotNull() {
             addCriterion( "tb_id is not null" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbIdEqualTo(Integer value) {
             addCriterion( "tb_id =" , value,  "tbId" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbIdNotEqualTo(Integer value) {
             addCriterion( "tb_id <>" , value,  "tbId" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbIdGreaterThan(Integer value) {
             addCriterion( "tb_id >" , value,  "tbId" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbIdGreaterThanOrEqualTo(Integer value) {
             addCriterion( "tb_id >=" , value,  "tbId" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbIdLessThan(Integer value) {
             addCriterion( "tb_id <" , value,  "tbId" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbIdLessThanOrEqualTo(Integer value) {
             addCriterion( "tb_id <=" , value,  "tbId" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbIdIn(List<Integer> values) {
             addCriterion( "tb_id in" , values,  "tbId" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbIdNotIn(List<Integer> values) {
             addCriterion( "tb_id not in" , values,  "tbId" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbIdBetween(Integer value1, Integer value2) {
             addCriterion( "tb_id between" , value1, value2,  "tbId" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbIdNotBetween(Integer value1, Integer value2) {
             addCriterion( "tb_id not between" , value1, value2,  "tbId" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbNameIsNull() {
             addCriterion( "tb_name is null" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbNameIsNotNull() {
             addCriterion( "tb_name is not null" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbNameEqualTo(String value) {
             addCriterion( "tb_name =" , value,  "tbName" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbNameNotEqualTo(String value) {
             addCriterion( "tb_name <>" , value,  "tbName" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbNameGreaterThan(String value) {
             addCriterion( "tb_name >" , value,  "tbName" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbNameGreaterThanOrEqualTo(String value) {
             addCriterion( "tb_name >=" , value,  "tbName" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbNameLessThan(String value) {
             addCriterion( "tb_name <" , value,  "tbName" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbNameLessThanOrEqualTo(String value) {
             addCriterion( "tb_name <=" , value,  "tbName" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbNameLike(String value) {
             addCriterion( "tb_name like" , value,  "tbName" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbNameNotLike(String value) {
             addCriterion( "tb_name not like" , value,  "tbName" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbNameIn(List<String> values) {
             addCriterion( "tb_name in" , values,  "tbName" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbNameNotIn(List<String> values) {
             addCriterion( "tb_name not in" , values,  "tbName" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbNameBetween(String value1, String value2) {
             addCriterion( "tb_name between" , value1, value2,  "tbName" );
             return  (Criteria)  this ;
         }
         public  Criteria andTbNameNotBetween(String value1, String value2) {
             addCriterion( "tb_name not between" , value1, value2,  "tbName" );
             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 );
         }
     }
}

TestMapper.xml


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值