Mybatis接收Integer参数
最近在做项目的时候,在mybatis中用标签判断分页参数时,出现了问题。
1
2
3
|
< if test = "startRow != null and startRow != '' and selectRow != null and selectRow != ''" > LIMIT #{startRow},#{selectRow} </ if > |
这段代码是为了在SQL中设置分页参数,接收的参数类型都为Integer。
我们都知道要查询第一页的话,startRow要赋值为0,但是这段代码一直不起作用。正常来说,0既不为null,也不等于空字符串;
但是查阅资料才知道,mybatis接收Integer参数时,若参数值为0,为被解析为空字符串' '
解决办法
1
2
3
|
< if test = "startRow != null and selectRow != null" > LIMIT #{startRow},#{selectRow} </ if > |
1.只判断Integer参数不为null,不用判断不为空字符串;
2.可以判断参数不等于0(在我的例子中不适用)
Mybatis在使用Integer类型
当传人mybatis构成sql语句时
传入的类型为int类型的值为0时,会被认为是空字符串,所以只要这样判断
就可以了:
1
2
3
|
< if test = "payStatus != null and payStatus != '' or payStatus == 0" > AND info.pay_status = #{payStatus} </ if > |
当payStatus的值为0时,再加一个payStatus==0就会走入if。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_41743796/article/details/88910994