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) <= #{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>