错误记录-数据库、SQL,Mybatis的XML

本文介绍了MyBatis中复杂的条件查询技巧,包括多表关联查询的注意事项、if标签使用细节、choose-when-otherwise标签的运用以及SQL过滤技巧等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 多表关联查询时(left join),主表一定是放在最前面(粗心)

2.Mybatis中,关于if test多个条件

(1)and or 必须小写,不识别大写

(2)or 作用范围的条件要加括号,For Example:

 <if test="beginTime!=null and beginTime!='' and (endTime == '' or endTime == null) ">     正确

 <if  test="beginTime!=null and beginTime!='' and endTime == '' or endTime == null"> 错误

 <if  test="(beginTime==null or beginTime=='' ) and (endTime == '' or endTime == null")> 正确

  (3) if test多个条件是可以使用 choose  when otherwise 来代替,如下,时间过滤

<choose>
<when test="beginTime!=null and beginTime!='' and (endTime == '' or endTime == null) ">
          AND Date(u.create_time) between #{beginTime,jdbcType=VARCHAR} and CURDATE()
</when>
<when test="endTime!=null and endTime!='' and (beginTime == '' or beginTime == null) ">
        AND Date(u.create_time) &lt;= #{endTime,jdbcType=VARCHAR}
</when>
<when test="beginTime!=null and beginTime!='' and endTime!=null and endTime!= '' ">
AND Date(u.create_time) between #{beginTime,jdbcType=VARCHAR} and #{endTime,jdbcType=VARCHAR}
</when>
<otherwise></otherwise>
</choose>

(4) 在Mysql时间过滤时,可以使用Date(时间字段)将时间转为‘2017-12-31’这样的格式来进行过滤

(5)SQL中对某字段是否为空进行过滤:

<if test="haveAccount!=null">
      and IF(ua.USER_ACCOUNT IS NULL,'否','是') = #{haveAccount}
    </if>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值