mybatis提取<where><if>共用代码
mybatis项目dao层中很多sql语句都会拥有某些相同的查询条件,以<where><if test=""></if></where>的形式拼接在sql语句后,一个两个的sql语句感觉不到什么,但是如果查询语句特别多,但是查询的条件总是类似的,那就可以考虑把<where><if>这部分代码抽取出来,封装一下,然后需要条件搜索的sql语句直接引用就可以了。
先来看下没有抽取代码之前的条件sql语句

第一条
<select id = "getUserEmailByProvinceAndOrderType" resultType="String">
select DISTINCT(wo_responsibility) from t_view_workorder
<where>
<if test="province != '全国' and province != null">
wo_province = #{province}
</if>
<if test="orderType != '全部' and orderType != null">
and wo_type = #{orderType}
</if>
<if test="email != ''">
and wo_responsibility = #{email}
</if>
</where>
</select>
第二条
<select id = "getUndoneDelayOrderByProvinceAndOrderTypeAndUserEmail" resultType="com.chinamobile.sias.workorder.po.Workorder">
select * from t_view_workorder
<where>
<if test="province != '全国' and province != null">
wo_province = #{province}
</if>
<if test="orderType != '全部' and orderType != null">
and wo_type = #{orderType}
</if>
<if test="email != ''">
and wo_responsibility = #{email}
</if>
<if test="true"> and (wo_complete_time is null or wo_complete_time='') and (select curdate()) >= wo_regulations_time </if> </where> </select>

以上是两条sql语句,可以看出,两个sql语句中有某些查询条件是相同的

<if test="province != '全国' and province != null">
wo_province = #{province}
</if>
<if test="orderType != '全部' and orderType != null">
and wo_type = #{orderType}
</if>
<if test="email != ''">
and wo_responsibility = #{email}
</if>

此时我们就可以对此段判断条件进行提取。如下:

<sql id="common_where_if">
<if test="province != '全国' and province != null">
wo_province = #{province}
</if>
<if test="orderType != '全部' and orderType != null">
and wo_type = #{orderType}
</if>
<if test="email != ''">
and wo_responsibility = #{email}
</if>
</sql>

此时把<where>标签下相同的判断条件提去了出来,id自己取,这里定为 common_where_if.
那么如何使用这段代码呢,如下:
<include refid="common_where_if"/>
格式如下:

<select id = "getUserEmailByProvinceAndOrderType" resultType="String">
select DISTINCT(wo_responsibility) from t_view_workorder
<where>
<include refid="common_where_if"/>
</where> </select>

此时就在<where>标签中引用了共用的判断条件,再多的sql语句,再多的查询条件,只需要一个<include>就能解决重复的代码。
mybatis提取<where><if>共用代码的更多相关文章
- 02 使用Mybatis的逆向工程自动生成代码
1.逆向工程的作用 Mybatis 官方提供了逆向工程,可以针对数据库表自动生成Mybatis执行所需要的代码(包括mapper.xml.Mapper.java.pojo). 2.逆向工程的使用方法 ...
- Eclipse 使用mybatis generator插件自动生成代码
Eclipse 使用mybatis generator插件自动生成代码 标签: mybatis 2016-12-07 15:10 5247人阅读 评论(0) 收藏 举报 .embody{ paddin ...
- win8.1和wp8.1共用代码,需要注意的一些问题
最近写了一个应有,使用了mvvmlight,把viewmodel.model.common之类的代码都放到了shared共享,写下来才发现,有不少问题是自已下手之前没注意到的,有些地方实在没法中途改了 ...
- 使用Mybatis的逆向工程自动生成代码
1.逆向工程的作用 Mybatis 官方提供了逆向工程,可以针对数据库表自动生成Mybatis执行所需要的代码(包括mapper.xml.Mapper.java.pojo). 2.逆向工程的使用方法 ...
- mybatis 逆向工程(通过数据库表针对单表自动生成mybatis执行所需要的代码)
mybatis需要程序员自己编写sql语句,mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码(mapper.java.mapper.xml.pojo…),可以让程序员 ...
- MyBatis使用Generator自动生成代码
MyBatis中,可以使用Generator自动生成代码,包括DAO层. MODEL层 .MAPPING SQL映射文件. 第一步: 配置好自动生成代码所需的XML配置文件,例如(generator. ...
- mybatis generator自动生成sqlmap代码的不完善之处以及解决方法
a) 建表时,字段名称建议用"_"分隔多个单词,比如:AWB_NO.REC_ID...,这样生成的entity,属性名称就会变成漂亮的驼峰命名,即:awbNo.recId b)or ...
- mybatis逆向工程--自动生成实体代码(mybatis-generator)
随便找个目录, 添加文件, 如图 主要是两个jar包, generator的下载路径: https://github.com/mybatis/generator/releases 驱动包随 ...
- mybatis递归,一对多代码示例
今天需要做一个功能,根据专业,有不同的章节,章节下面有对应的习题, 由于只有这么两级,可以不用使用递归,直接查询父集,之后foreach查询子集放入对应的list集合. 虽然实现了,感觉毕竟,太low ...
随机推荐
- idea 版本升级至最新版
前言:当前最新版为官网上的2018.2.3版本 一.下载最新版 官网地址:http://www.jetbrains.com/idea/download/#section=windows 百度网盘地址: ...
- Hibernate的一对多实例
一对多在现实生活中很常见,今天做了个Hibernate的一对多的实例,也是个入门过程,写下来跟大家分享. 最重要的是xml配置文件,之前因为把英文"(引号)错误的复制成中文的“”(引号),导 ...
- python中垃圾回收机制
Python垃圾回收机制详解 一.垃圾回收机制 Python中的垃圾回收是以引用计数为主,分代收集为辅.引用计数的缺陷是循环引用的问题.在Python中,如果一个对象的引用数为0,Python虚拟 ...
- javascript中让你捉摸不定的this
this到底指向谁,估计很多人在使用javascript的过程中都遇到过,这个关键字如果没搞懂,在一些高级功能中都会困难重重,搜了下相关文章,介绍的都挺多的,也有很深入的,比如汤姆大叔的<深入理 ...
- mysql 删除某一个数据库下面的所有表,但不删除数据库
删除某一个数据库下面的所有表,但不删除数据库.该语句经过从concat拼接,最后查询出来的是删除表的语句,然后执行那些查询出来的语句就ok了select concat(‘drop table ‘,ta ...
- 详解php常量const与define的区别和实例
所谓常量是一个简单的标识符.在脚本执行期间该值不能改变.常量默认大小写敏感.通常常量标识符总是大写的.常量只能包含标量数据(boolean.integer.float和string).可以定义reso ...
- 全功能开发团队(FSD)
- python 字符串的方法和注释
capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 center(width) 将字符串居中,并使用空格填充至长度 width 的新字符串 c ...
- Oracle Sequence Cache 参数说明
转自 http://blog.csdn.net/tianlesoftware/article/details/5995051 之前整理的一篇文章: ORACLE SEQUENCE 介绍 http:// ...
- leetcode题解之分解字符串域名
1.题目描述 A website domain like "discuss.leetcode.com" consists of various subdomains. At the ...