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 ...
随机推荐
- C#设计模式--命令模式(学习Learning hard C#设计模式笔记)
原文地址http://www.cnblogs.com/zhili/p/CommandPattern.html class Program { static void Main(string[] arg ...
- CentOS 7 防火墙端口配置
CentOS 7 防火墙端口配置查看防火墙是否开启systemctl status firewalld 若没有开启则开启systemctl start firewalld 查看所有开启的端口firew ...
- [AngularJS] “多重路由”嵌套模块——AngularJS“路由”嵌套学习资料教程
这是小编的一些学习资料,理论上只是为了自己以后学习需要的,但是还是需要认真对待的 以下内容仅供参考,请慎重使用学习 1.AngularJS路由嵌套 Angularjs本身自带路由模块,可以满足通过不同 ...
- OpenGL学习--02--绘制一个红色三角形
The OpenGL buffer is created, bound, filled and configured with the standard functions (glGenBuffers ...
- jQuery实现大图轮播
css样式: *{ margin: 0; padding: 0;}ul{ list-style:none;}.slideShow{ width: 620px; heigh ...
- Android 通过URL获取网络资源
1.先在AndroidManifest.xml中注册加入访问因特网服务的权限: <uses-permission android:name="android.permission.IN ...
- ecloipse背景修改豆沙
Eclipse背景色的修改 Eclipse背景色的修改,修改为豆沙色 值是85 123 205 一.修改编辑区 ①这个比较简单一般都会不多说. 1.首先点击Window 然后选择Preferen ...
- Fragment的setUserVisibleHint方法实现懒加载
public abstract class LazyFragment extends Fragment { protected boolean isVisible; /** ...
- 搭建Kafka开发环境
Kafka版本是:kafka_2.10-0.8.2.1 1.maven工程方式 在pom.xml中配置kafka依赖 1 2 3 4 5 <dependency> <grou ...
- Acticity的生命周期和启动模式
典型情况下的生命周期 onCreate 表示创建Acticity,在这个方法中可以做一些初始化的操作,如加载界面布局资源,初始化Activity所需的数据 onRestart 表示重新启动Activi ...