解决mybatis where-if中if不能识别大写AND,OR的问题
程序员文章站
2022-03-24 08:21:04
mybatis报错:caused by: org.apache.ibatis.ognl.parseexception: encountered " "and “” at line 1错误代码:<...
mybatis报错:
caused by: org.apache.ibatis.ognl.parseexception: encountered " "and “” at line 1
错误代码:
<select id="selectaccountlist" resultmap="baseresultmap"> select ct.customer_name customername,sam.city_code,sam.user_name,sam.account_name from sys_account_manager sam left join sys_customer ct on ct.id = sam.customer_id where sam.deleted = 0 <if test="customername != null and customername != '' "> and ct.customer_name like concat('%',#{customername},'%') </if> <if test="citycode != null and citycode != '' "> and locate(#{citycode},sam.city_code) </if> order by status,account_validity_time desc </select>
正确代码:
原因是:
if条件中and为大写,大写不能识别,应改为小写。
<select id="selectaccountlist" resultmap="baseresultmap"> select ct.customer_name customername,sam.city_code,sam.user_name,sam.account_name from sys_account_manager sam left join sys_customer ct on ct.id = sam.customer_id where sam.deleted = 0 <if test="customername != null and customername != '' "> and ct.customer_name like concat('%',#{customername},'%') </if> <if test="citycode != null and citycode != '' "> and locate(#{citycode},sam.city_code) </if> order by status,account_validity_time desc </select>
补充:mybatis中if判断遇到的坑
最近在项目开发的过程中,遇到了mybatis的一个坑(也许是mybatis有意这样设计的),对于integer或者long这种引用数据类型,在做if判断的时候,如果引用数据类型为0,则mybatis将会视为”“空字符串,所以走不进判断逻辑里。
以下余额字段为long类型,availableamount值为0时,将走不进判断方法内的示例截图:
解决方法:
在test判断条件中添加”or availableamount==0“即可,以下是示例截图:
或者在业务场景允许的情况下,只判断availableamount!=null
<if test="availableamount!=null"> ... </if>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
上一篇: python聚类算法选择方法实例
下一篇: shell脚本读取命令行参数的实现
推荐阅读
-
从Myeclipse 导入到eclipse中无法识别为 web项目 问题的解决步骤
-
虚拟机中ubuntu不能联网问题的解决方法
-
电脑意外关机后VMware中linux不能联网问题的解决方法
-
从Myeclipse 导入到eclipse中无法识别为 web项目 问题的解决步骤
-
python中不能连接超时的问题及解决方法
-
解决新django中的path不能使用正则表达式的问题
-
解决mysql中只能通过localhost访问不能通过ip访问的问题
-
adb不是内部或外部命令,关于Android Studio中ADB命令不能用问题的解决办法
-
python中不能连接超时的问题及解决方法
-
解决uni-app开发中刘海屏下背景不能全屏铺满的问题