mybatis中where-if不能识别大写AND,OR如何解决

发布时间:2021-02-13 18:12:17 作者:Leah
来源:亿速云 阅读:513

这期内容当中小编将会给大家带来有关mybatis中where-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时,将走不进判断方法内的示例截图:

mybatis中where-if不能识别大写AND,OR如何解决

解决方法:

在test判断条件中添加”or availableAmount==0“即可,以下是示例截图:

mybatis中where-if不能识别大写AND,OR如何解决

或者在业务场景允许的情况下,只判断availableAmount!=null

<if test="availableAmount!=null">
  ...
</if>

上述就是小编为大家分享的mybatis中where-if不能识别大写AND,OR如何解决了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. mybatis查询结果接收不同怎么解决
  2. Postgresql中mybatis出现报错如何解决

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis where-if

上一篇:如何在Thinkphp中ajaxFileUpload实现异步传输图片

下一篇:如何在Linux中部署一个.net core Api项目

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》