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 ...
随机推荐
- 关于Google圆角高光高宽自适应按钮及其拓展
关于Google圆角高光高宽自适应按钮及其拓展————源自张鑫旭css讲解 这篇文章发布于 2009年10月24日,星期六,18:08,归类于 css相关. 阅读 48770 次, 今日 1 次 by ...
- 用hmac验证客户端的合法性
服务器端程序 import os import hmac import socket def auth(conn): secret_key = b'rock' rand_b = os.urandom( ...
- 【HTML&CSS】搜狐页面代码编写
<!DOCTYPE html> <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"& ...
- CSS 几款比较常用的翻转特效
第一个:360度翻转特效 <style>* { margin:0; padding:0; } .aa { width: 220px; height: 220px; margin: 0 au ...
- sql 去重关键字 distinct
单列去重: mysql: drop table test;create table test(id int(4));insert into test values(1),(2),(3),(4),(1) ...
- Nested Prefab Mode 嵌套预制体 保存问题 Dirty
Unity2018.3 Nested Prefab Mode 嵌套预制体 Unity2018.3开始,新增了一个Prefab Mode,俗称嵌套预制体,在Prefab里套Prefab.实际应用中多少会 ...
- hdfs操作手册
hdfscli 命令行 # hdfscli --help HdfsCLI: a command line interface for HDFS. Usage: hdfscli [interactive ...
- MySQL InnoDB锁机制之Gap Lock、Next-Key Lock、Record Lock解析
MySQL InnoDB支持三种行锁定方式: l 行锁(Record Lock):锁直接加在索引记录上面,锁住的是key. l 间隙锁(Gap Lock):锁定索引记录间隙,确保索引记录的间隙 ...
- [翻译] BezierString
BezierString https://github.com/lvnyk/BezierString Rendering NSAttributedStrings along arbitrary con ...
- Python学习---ModelForm拾遗180325
ModelForm适用于前台验证和后台直接操作数据库的前后台未做分离,可以一次执行验证和保存数据的场景. 注意: 1. ModelForm里面没有删除方法,需要手动删除内容 2. ModelFor ...