mybatis里面xml中SQL语句if语句里嵌套if语句
2016-02-24 11:35
1256 查看
mybatis里面的if语句需要加上(但是有时不需要 但是(又是在if语句中的
案例一 不完善
<select id="selectRechargeInfoChainCount" resultType="int">
select count(*) from t_group_card_recharge_info
<where>
<if test="cardNo!=null and cardNo!=''">
and card_no =#{cardNo}
</if>
<if test="mobile!=null and mobile!=''">
and mobile =#{mobile}
</if>
<if test="null!=beginDate">
and back_time <![CDATA[>=]]> #{beginDate}
</if>
<if test="null!=endDate">
and back_time <![CDATA[<=]]> #{endDate}
</if>
<if test="clinicId!=null and clinicId!=''">
and
( //这个括号与下一个括号一一对应 但是下面不满足时会报错,上面的括号是多余的
clinic_id =#{clinicId}
</if>
<if test="sysId!=null and sysId!='' and sysId != -1">
or sys_id =#{sysId} )
</if>
</where>
</select>
案例二完善 案例一
<select id="selectRechargeInfoChainCount" resultType="int">
select count(*) from t_group_card_recharge_info
<where>
<if test="cardNo!=null and cardNo!=''">
and card_no =#{cardNo}
</if>
<if test="mobile!=null and mobile!=''">
and mobile =#{mobile}
</if>
<if test="null!=beginDate">
and back_time <![CDATA[>=]]> #{beginDate}
</if>
<if test="null!=endDate">
and back_time <![CDATA[<=]]> #{endDate}
</if>
<if test="clinicId!=null and clinicId!=''">
and
<if test="sysId!=null and sysId!='' and sysId != -1">
(
</if> //这样不会因为下面不满足而多出括号
clinic_id =#{clinicId}
</if>
<if test="sysId!=null and sysId!='' and sysId != -1">
or sys_id =#{sysId} )
</if>
</where>
</select>
注意颜色一样的标记便于理解
关注微信公共号 戒色专家
拥有健康的生活态度。
案例一 不完善
<select id="selectRechargeInfoChainCount" resultType="int">
select count(*) from t_group_card_recharge_info
<where>
<if test="cardNo!=null and cardNo!=''">
and card_no =#{cardNo}
</if>
<if test="mobile!=null and mobile!=''">
and mobile =#{mobile}
</if>
<if test="null!=beginDate">
and back_time <![CDATA[>=]]> #{beginDate}
</if>
<if test="null!=endDate">
and back_time <![CDATA[<=]]> #{endDate}
</if>
<if test="clinicId!=null and clinicId!=''">
and
( //这个括号与下一个括号一一对应 但是下面不满足时会报错,上面的括号是多余的
clinic_id =#{clinicId}
</if>
<if test="sysId!=null and sysId!='' and sysId != -1">
or sys_id =#{sysId} )
</if>
</where>
</select>
案例二完善 案例一
<select id="selectRechargeInfoChainCount" resultType="int">
select count(*) from t_group_card_recharge_info
<where>
<if test="cardNo!=null and cardNo!=''">
and card_no =#{cardNo}
</if>
<if test="mobile!=null and mobile!=''">
and mobile =#{mobile}
</if>
<if test="null!=beginDate">
and back_time <![CDATA[>=]]> #{beginDate}
</if>
<if test="null!=endDate">
and back_time <![CDATA[<=]]> #{endDate}
</if>
<if test="clinicId!=null and clinicId!=''">
and
<if test="sysId!=null and sysId!='' and sysId != -1">
(
</if> //这样不会因为下面不满足而多出括号
clinic_id =#{clinicId}
</if>
<if test="sysId!=null and sysId!='' and sysId != -1">
or sys_id =#{sysId} )
</if>
</where>
</select>
注意颜色一样的标记便于理解
关注微信公共号 戒色专家
拥有健康的生活态度。
相关文章推荐
- SQL 截取日期作为条件
- MS SQL修改sa密码
- 解决MSSQL 2008不能用IP登录的问题
- MySQL数据库事务隔离级别(Transaction Isolation Level)(顶)
- MS SQLSERVER如何实现跨服务器查询
- SQL SERVER传递datetime参数注意事…
- mongodb 添加用户及权限设置详解
- MySQL zabbix 流量统计分析脚本(内部业务需求)
- Redis 未授权访问配合 SSH key 文件利用分析
- 如何修改mysql 并发数
- ITOO---"系统设置"之数据库表
- spring-redis SortedSet类型成员的过期时间处理
- mySql ERROR 1046(3D000): No Datab
- MySQL 清除表空间碎片
- MySQL大量数据插入各种方法性能分析与比较
- oracle中的(+)
- mysqldump导入导出数据库基本方法
- Mysql 导入数据库方法 及失败解决
- MySQL新建用户,授权,删除用户,修改密码
- 基于Twemproxy的Redis集群方案