解决mybatis where-if中if不能识别大写AND,OR的问题
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>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
相关文章
MyBatis-Plus条件构造器之condition参数的使用
这篇文章主要介绍了MyBatis-Plus条件构造器之condition参数的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-12-12SpringBoot学习系列之MyBatis Plus整合封装的实例详解
MyBatis-Plus是一款MyBatis的增强工具(简称MP),为简化开发、提高效率,这篇文章给大家介绍MyBatis Plus整合封装的实例详解,感兴趣的朋友跟随小编一起看看吧2020-08-08Java的System.getProperty()方法获取大全
这篇文章主要介绍了Java的System.getProperty()方法获取大全,罗列了System.getProperty()方法获取各类信息的用法,具有一定的参考借鉴价值,需要的朋友可以参考下2014-12-12
最新评论