2019-04-28 Mybatis generator逆向工程生成的Example代码分析
今天主要对Mybatis generator生成的DAO层等进行分析,讲解Example类的使用和扩展
1.先在数据库建表
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配置文件
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的分析
- 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 );
- 7、9、12、14为一个Criterion
- 7、9需要存放到同一个Criteria中
- 12、14需要存放到同一个Criteria中
- 6-15位DepartmentExample的条件部分(以上两个Criteria需要存放入Example的List中)
5.使用DepartmentExample查询的优缺点
优点:
- 因为是代码生成的,可以省去自己写XML和接口方法
- 查询条件灵活
缺点:
- 查删改都需要使用Example,略显繁琐
- 没有分页功能
6.扩展DepartmentExample分页
① 在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 }
转载于:https://www.cnblogs.com/WongHugh/p/10783251.html
2019-04-28 Mybatis generator逆向工程生成的Example代码分析相关推荐
- 在IDEA中使用MyBatis Generator逆向工程生成代码
本文介绍一下用Maven工具如何生成Mybatis的代码及映射的文件. 一.配置Maven pom.xml 文件 在pom.xml增加以下插件: <build><finalName& ...
- mybatis.generator逆向工程生成数据库访问mybatis代码
1.添加POM依赖 <dependency><groupId>org.mybatis</groupId><artifactId>mybatis</ ...
- idea mybatis generator插件_在idea中使用mybatis generator逆向工程生成代码
用maven工具生成mybatis的代码和映射文件. 1.配置pom.xml文件 在pom.xml下添加插件如下: <build> <finalName>zsxt</fi ...
- Mybatis Generator 自动生成数据库XML(Mybatis Generator 逆向工程)
Mybatis Generator 逆向工程自动生成XML 1.MyBatis Generator简单介绍: (1)MyBatis Generator 会生成: Dao,Model,Mapping基础 ...
- IDEA 中使用 MyBatis Generator 逆向工程出现运行成功但是没有生成相应的文件
记录一个 MyBatis Generator 逆向工程出现运行成功但是没有生成相应的文件问题. 我这里使用的是main方法运行,在出现运行成功没有报错但是也没有生成对应的文件,把下面这一段代码加上运行 ...
- SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件
我们这一一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池的好处我 ...
- Mybatis Generator逆向工程的使用
MyBatis Generator简介 MyBatis Generator(MBG)是MyBatis MyBatis 和iBATIS的代码生成器.它将为所有版本的MyBatis以及版本2.2.0之后的 ...
- SpringBoot如何自动生成实体类和Dao层以及映射文件(mybatis generator 自动生成代码)
一.首先添加自动生成代码插件 <!-- mybatis generator 自动生成代码插件 生成时解除注释 --><plugin><groupId>org.myb ...
- mybatis generator自动生成sqlmap代码的不完善之处以及解决方法
mybatis generator自动生成sqlmap代码的不完善之处以及解决方法 参考文章: (1)mybatis generator自动生成sqlmap代码的不完善之处以及解决方法 (2)http ...
最新文章
- [node 工具] 用 Node.js 将 bugzilla 上的 bug 列表导入到 excel 表格在线版本之一(server 端)...
- 《JAVA程序设计》第八周学习总结
- flink source 同步_如何生成 Flink 作业的交互式火焰图?
- 这两天做项目出现的几个问题
- Perl 面对对象的案例理解
- 计算机桌面文字显示软件,电脑桌面添加文字_电脑桌面添加文字软件
- 刚刚,微软发出警告:这个 IE 0day 影响所有 Windows 版本,已遭用且无补丁!
- Linux学习之二十、循环
- 【JZOJ6225】【20190618】计数
- visual studio 2019配置dlib读取jpeg图片
- 如何建设一个开源图形引擎的文档网站
- 极客大学架构师训练营 系统架构 消息队列 负载均衡 数据库备份 第10课 听课总结
- tp6验证码无法验证
- android 经纬度的格式,的Android如何转换经纬度成度格式
- 利用telemetry进行权限维持
- 八问顺丰,被你们偷的iPad究竟何时能赔?
- 【BZOJ】3698:XWW的难题-上下界网络流
- 最新表白墙网站源码 手机电脑自适应 带后台
- VAPS XT航空仪表开发第一节
- SAP HANA命令行方式备份恢复
热门文章
- Spring component detection logic
- SAP CRM Fiori应用My Opportunity标题显示不正确的问题分析
- linux 使用nginx 权限不够,对于nginx和Linux,有一些关于权限的问题。
- 电脑卡顿不流畅怎么解决_电脑用久了卡怎么办?学会这四招解决你的电脑卡顿问题...
- gis怎么通过水库划分子流域_ArcGIS提取小流域——以北京为例,提取出密云水库...
- 拦截器读post参数导致接口post请求报错_vue中Axios的封装和API接口的管理
- 如何获取当日日期_Power BI时间智能函数如何处理2月29日的?
- python画父子关系图_将有父子关系的一维数组转换成树形结构(多维)数据
- python读取第二行_使用Python操作Excel(二):读取数据表
- traybox错误怎么办_toolbox提示错误无法使用怎么办?