mybatis动态sql语句拼接总结

mybatis中 如果 需要在where后面追加 条件判断语句中 有多个判断条件  我们可以在if条件后面 写上类似三元运算符的关系表达式
其中 a.pub_time IS NOT NULL 为条件 如果 不为空 那么执行 ,号前的语句 否则执行 ,号后的语句

SELECT a.id FROM paper a LEFT JOIN paper_personnel_rel b ON a.id
= b.paper_id WHERE a.is_deleted = 0 AND b.personnel_id = #{personnelId} AND IF( a.pub_time IS NOT NULL, <choose> <when test="outTime != null">date_format(a.pub_time,'%Y-%m-%d') BETWEEN #{joinTime} AND #{outTime} </when> <otherwise>date_format(a.pub_time,'%Y-%m-%d') >= #{joinTime}</otherwise> </choose> , <choose> <when test="outTime != null">date_format(a.summary_time,'%Y-%m-%d') BETWEEN #{joinTime} AND #{outTime} </when> <otherwise>date_format(a.summary_time,'%Y-%m-%d') >= #{joinTime}</otherwise> </choose> )

 

posted @ 2022-10-10 18:19  花心大萝卜li  阅读(551)  评论(0)    收藏  举报