欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

解决mybatis where-if中if不能识别大写AND,OR的问题

时间:2021-06-13 08:41:32|栏目:JAVA代码|点击:

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>

上一篇:使用java编程从0到1实现一个简单计算器

栏    目:JAVA代码

下一篇:Java读写.properties文件解决中文乱码问题

本文标题:解决mybatis where-if中if不能识别大写AND,OR的问题

本文地址:http://www.codeinn.net/misctech/140849.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有