2019-04-28 Mybatis generator逆向工程生成的Example代码分析

今天主要对Mybatis generator生成的DAO层等进行分析,讲解Example类的使用和扩展java

1.先在数据库建表mysql

1 CREATE TABLE `department` ( 2   `fid` varchar(255) NOT NULL, 3   `code` varchar(255) DEFAULT NULL COMMENT '部门代码', 4   `name` varchar(255) DEFAULT NULL COMMENT '部门名称', 5   PRIMARY KEY (`fid`) 6 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.建立Mybatis generator配置文件sql

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  3  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 4 <generatorConfiguration>
 5   <context id="context1">
 6    <!-- 去掉注释 -->
 7    <commentGenerator>
 8         <property name="suppressAllComments" value="true" />
 9     </commentGenerator> 
10     
11     <!-- 配置链接信息 -->
12     <jdbcConnection connectionURL="jdbc:mysql://localhost:3306/demo1?serverTimezone=GMT%2B8" 
13  driverClass="com.mysql.cj.jdbc.Driver" password="x5" userId="root" />
14     <!-- 配置实体类生成路径 -->
15     <javaModelGenerator targetPackage="com.wf.ew.demo1.model" targetProject="reverse\src\main\java" />
16        <!-- 配置MapperXML文件生成路径 -->
17     <sqlMapGenerator targetPackage="com.wf.ew.demo1.dao" targetProject="reverse\src\main\java" />
18     <!-- 配置DAO层生成路径 -->
19     <javaClientGenerator targetPackage="com.wf.ew.demo1.dao" targetProject="reverse\src\main\java" type="XMLMAPPER" />
20     <!-- 配置表和实体类的映射关系 -->
21     <table tableName="department" domainObjectName="Department">
22      <!-- <columnOverride column="???" property="???" /> -->
23     </table>
24   
25   </context>
26   
27 </generatorConfiguration>

3.生成的DepartmentExample数据库

 1 package com.wf.ew.demo1.model;  2 
 3 import java.util.ArrayList;  4 import java.util.List;  5 
 6 
 7 public class DepartmentExample {  8    
 9     /**
 10  * 排序条件  11      */
 12     protected String orderByClause;  13 
 14     /**
 15  * 去重标识  16      */
 17     protected boolean distinct;  18     
 19  
 20     /**
 21  * 最终以or进行链接的条件列表,Criteria为一个以and链接的条件集  22      */
 23     protected List<Criteria> oredCriteria;  24 
 25     /**
 26  * 初始化条件描述对象容器列表  27      */
 28     public DepartmentExample() {  29         oredCriteria = new ArrayList<Criteria>();  30  }  31     
 32     // 此处省略orderByClause、distinct的Getter、Setter和oredCriteria的Getter
 33  ……  34 
 35     /**
 36  * 增长以and链接的条件集(先补全后增长)  37      */
 38     public void or(Criteria criteria) {  39  oredCriteria.add(criteria);  40  }  41 
 42     /**
 43  * 增长空的以and链接的条件集(先增长后补全)  44  * @return
 45      */
 46     public Criteria or() {  47         Criteria criteria = createCriteriaInternal();  48  oredCriteria.add(criteria);  49         return criteria;  50  }  51 
 52     /**
 53  * 增长空的以and链接的条件集(当且仅当容器为空的才能加入)  54  * @return
 55      */
 56     public Criteria createCriteria() {  57         Criteria criteria = createCriteriaInternal();  58         if (oredCriteria.size() == 0) {  59  oredCriteria.add(criteria);  60  }  61         return criteria;  62  }  63 
 64     /**
 65  * 增长空的条件描述对象容器(内部实现)  66  * @return
 67      */
 68     protected Criteria createCriteriaInternal() {  69         Criteria criteria = new Criteria();  70         return criteria;  71  }  72 
 73     /**
 74  * 格式化过滤对象  75      */
 76     public void clear() {  77  oredCriteria.clear();  78         orderByClause = null;  79         distinct = false;  80  }  81 
 82     /**
 83  * 抽象类:条件描述对象容器(容器内的条件会以and链接)  84      */
 85     protected abstract static class GeneratedCriteria {  86         /**
 87  * 条件描述对象容器  88          */
 89         protected List<Criterion> criteria;  90 
 91         /**
 92  * 初始化条件描述对象容器  93          */
 94         protected GeneratedCriteria() {  95             super();  96             criteria = new ArrayList<Criterion>();  97  }  98 
 99         /**
100  * 判断是否有效(基本条件最少为1) 101  * @return
102          */
103         public boolean isValid() { 104             return criteria.size() > 0; 105  } 106 
107         /**
108  * 得到全部条件描述对象 109  * @return
110          */
111         public List<Criterion> getAllCriteria() { 112             return criteria; 113  } 114 
115         /**
116  * 得到条件描述对象容器 117  * @return
118          */
119         public List<Criterion> getCriteria() { 120             return criteria; 121  } 122 
123         /*== 通用增长条件描述对象的方法 == */
124         /**
125  * 增长没有没有占位符的条件描述 126  * @param condition 127          */
128         protected void addCriterion(String condition) { 129             if (condition == null) { 130                 throw new RuntimeException("Value for condition cannot be null"); 131  } 132             criteria.add(new Criterion(condition)); 133  } 134 
135         /**
136  * 增长单占位符的条件描述(没有typeHandler) 137  * @param condition 138  * @param value 139  * @param property 140          */
141         protected void addCriterion(String condition, Object value, String property) { 142             if (value == null) { 143                 throw new RuntimeException("Value for " + property + " cannot be null"); 144  } 145             criteria.add(new Criterion(condition, value)); 146  } 147 
148         /**
149  * 增长区间参数的条件描述(没有typeHandler) 150  * @param condition 151  * @param value1 152  * @param value2 153  * @param property 154          */
155         protected void addCriterion(String condition, Object value1, Object value2, String property) { 156             if (value1 == null || value2 == null) { 157                 throw new RuntimeException("Between values for " + property + " cannot be null"); 158  } 159             criteria.add(new Criterion(condition, value1, value2)); 160  } 161 
162         
163         // 如下省略 fid、code、name的14种条件查询生成(为空、不为空、=、<>、>、>=、<、<=、like、not like、in、not in、between、not between)
164  …… 165       
166      
167  } 168 
169     /**
170  * 静态内部类:条件描述对象容器实现类 171  * 172      */
173     public static class Criteria extends GeneratedCriteria { 174 
175         protected Criteria() { 176             super(); 177  } 178  } 179 
180     /**
181  * 静态内部类:条件描述对象 182  * 183      */
184     public static class Criterion { 185         private String condition; // 原子条件
186 
187         private Object value; // 第一个值
188 
189         private Object secondValue; // 第二个值
190 
191         private boolean noValue; // 是否没有值
192 
193         private boolean singleValue; // 是否单值(与listValue互逆)
194 
195         private boolean betweenValue; // 是否区间值
196 
197         private boolean listValue; // 是否列表(与singleValue互逆)
198 
199         private String typeHandler; 200 
201         public String getCondition() { 202             return condition; 203  } 204 
205         // 此处省略 value、noValue、singleValue、listValue、typeHandler的Getter方法
206  …… 207 
208         /**
209  * 建立没有占位符的条件 210  * @param condition 211          */
212         protected Criterion(String condition) { 213             super(); 214             this.condition = condition; 215             this.typeHandler = null; 216             this.noValue = true; 217  } 218 
219         /**
220  * 建立单占位符的条件 221  * @param condition 222  * @param value 223  * @param typeHandler 224          */
225         protected Criterion(String condition, Object value, String typeHandler) { 226             super(); 227             this.condition = condition; 228             this.value = value; 229             this.typeHandler = typeHandler; 230             if (value instanceof List<?>) { 231                 this.listValue = true; 232             } else { 233                 this.singleValue = true; 234  } 235  } 236 
237         /**
238  * 建立单占位符的条件(没有typeHandler) 239  * @param condition 240  * @param value 241          */
242         protected Criterion(String condition, Object value) { 243             this(condition, value, null); 244  } 245 
246         /**
247  * 建立区间参数的条件 248  * @param condition 249  * @param value 250  * @param secondValue 251  * @param typeHandler 252          */
253         protected Criterion(String condition, Object value, Object secondValue, String typeHandler) { 254             super(); 255             this.condition = condition; 256             this.value = value; 257             this.secondValue = secondValue; 258             this.typeHandler = typeHandler; 259             this.betweenValue = true; 260  } 261 
262         /**
263  * 建立区间参数的条件(没有typeHandler) 264  * @param condition 265  * @param value 266  * @param secondValue 267          */
268         protected Criterion(String condition, Object value, Object secondValue) { 269             this(condition, value, secondValue, null); 270  } 271  } 272 }

4.DepartmentExample的分析apache

  • DepartmentExample类中还包含了GeneratedCriteria、Criteria、Criterion三个静态内部类
  • Criterion表示一个最小粒度的条件的描述
  • GeneratedCriteria是一个抽象类;表示Criterion的集合,元素之间使用and链接
  • Criteria是GeneratedCriteria的实现类
  • DepartmentExample表示一个SQL语句的where、distinct、order by部分的描述;其中where部分表示Criteria的集合,元素间使用or链接

  使用一个例子进行说明 网络

 1 SELECT DISTINCT
 2     *
 3 FROM
 4  maintainbill  5 WHERE
 6  (  7         fState = '已完成'
 8         AND 
 9         fItemType LIKE '%网络%'
10  ) 11 OR ( 12     fState = '待安排'
13     AND 
14     fItemType LIKE '%电脑%'
15 );
  • 七、九、十二、14为一个Criterion
  • 七、9须要存放到同一个Criteria中
  • 十二、14须要存放到同一个Criteria中
  • 6-15位DepartmentExample的条件部分(以上两个Criteria须要存放入Example的List中)

5.使用DepartmentExample查询的优缺点mybatis

优势:app

  • 由于是代码生成的,能够省去本身写XML和接口方法
  • 查询条件灵活

缺点:dom

  • 查删改都须要使用Example,略显繁琐
  • 没有分页功能

6.扩展DepartmentExample分页ide

① 在Example中加入startindex(开始行索引)、limit(记录数)属性及Getter、Setter

②扩展MapperXML中查询列表的select

<if test="startindex !=null and limit !=null"> limit ${startindex},${limit} </if>

7.补充DepartmentMapper接口的方法说明

 1 package com.wf.ew.demo1.dao;  2 
 3 import java.util.List;  4 
 5 import org.apache.ibatis.annotations.Param;  6 
 7 import com.wf.ew.demo1.model.Department;  8 import com.wf.ew.demo1.model.DepartmentExample;  9 
10 public interface DepartmentMapper{ 11     /**
12  * 根据example查询记录数 13      */
14     long countByExample(DepartmentExample example); 15 
16     /**
17  * 根据example删除记录 18      */
19     int deleteByExample(DepartmentExample example); 20 
21     /**
22  * 插入行(插入全部属性值) 23      */
24     int insert(Department record); 25 
26     /**
27  * 插入行(插入非null属性值) 28      */
29     int insertSelective(Department record); 30 
31     /**
32  * 根据example查询记录 33      */
34     List<Department> selectByExample(DepartmentExample example); 35 
36    
37     /**
38  * 根据example更新表(更新非null属性) 39      */
40     int updateByExampleSelective(@Param("record") Department record, @Param("example") DepartmentExample example); 41 
42     /**
43  * 根据example更新表(更新全部属性) 44      */
45     int updateByExample(@Param("record") Department record, @Param("example") DepartmentExample example); 46 }
相关文章
相关标签/搜索