Mybatis中sql语句中的in查询,一定要判断null的情况
2015-05-03 00:00
501 查看
不严谨的写法,可能会报错:in (),这种情况不符合mysql的语法。
select from loanwhere LOAN_ID in
<foreach item="item" index="id" collection="list" open="(" separator="," close=")">
#{item}
要么在Mybatis的sql文件中,要么在Java程序中。
sql中的写法:
<select id="findByLoanIds" parameterType="List">
select *
from p2p_loan_variation where 1= 1
<if test="list != null and list.size>0">
and LOAN_ID in
<foreach item="item" index="id" collection="list" open="("
separator="," close=")">
#{item}
</foreach>
</if>
<if test="list==null or list.size==0">and 1=0</if>
</select>
Java中的写法:
List<LoanVariation> list = null;
if(ListUtils.isNotEmpty(loadIds){
list =loanVariationDao.findByLoanIds(loanIds);
}
return list;
如果有多个Java方法调用dao mapper,在sql中写比较省事,毕竟只需要一次“参数检查”。
上面的写法是针对一个查询条件的,如果有多个查询条件,记得处理条件之间的关系,
比如 a=1 and b =2 or c=3.
如果有in条件,最好加上括号,(a=1) and (b =2 ) or ( c in ...)
select from loanwhere LOAN_ID in
<foreach item="item" index="id" collection="list" open="(" separator="," close=")">
#{item}
要么在Mybatis的sql文件中,要么在Java程序中。
sql中的写法:
<select id="findByLoanIds" parameterType="List">
select *
from p2p_loan_variation where 1= 1
<if test="list != null and list.size>0">
and LOAN_ID in
<foreach item="item" index="id" collection="list" open="("
separator="," close=")">
#{item}
</foreach>
</if>
<if test="list==null or list.size==0">and 1=0</if>
</select>
Java中的写法:
List<LoanVariation> list = null;
if(ListUtils.isNotEmpty(loadIds){
list =loanVariationDao.findByLoanIds(loanIds);
}
return list;
如果有多个Java方法调用dao mapper,在sql中写比较省事,毕竟只需要一次“参数检查”。
上面的写法是针对一个查询条件的,如果有多个查询条件,记得处理条件之间的关系,
比如 a=1 and b =2 or c=3.
如果有in条件,最好加上括号,(a=1) and (b =2 ) or ( c in ...)
相关文章推荐
- Mybatis中sql语句中的in查询,一定要判断null的情况
- Mybatis中sql语句中的in查询,一定要判断null的情况
- Mybatis中sql语句中的in查询,一定要判断null的情况
- Mybatis中sql语句中的in查询,一定要判断null的情况
- Mybatis中sql语句中的in查询,判断null和size为0的情况
- mybatis sql in 查询(mybatis sql语句传入参数是list)mybatis中使用in查询时in怎么接收值
- mybatis实现sql查询in()语句
- sql查询in包含null的情况
- mybatis mapper查询结果为NULL 复制出sql执行有结果
- sql语句中条件查询in、like、=的效率
- Mybatis查询实例,sql中的in在Mybatis中怎么写--以list为查询条件
- mybatis查询sql:where语句“查询时间字段书写问题”引发的索引失效
- mybatis sql in 查询总结
- 解决:mybatis执行SQL语句部分参数返回NULL
- SQL查询和判断语句
- mybatis之xml中日期时间段查询的sql语句
- mybatis处理集合、循环、数组和in查询等语句的使用
- SQL 查询语句 IIF()处理条件判断
- Mybatis查询SQL中如何使用in?
- mybatis的分页插件pagehelper-fix使用、数据库分页查询模板sql、总结mysql与oracle语句的区别