简介
mybatis 常用操作,用于记录mybatis经常使用的写法,相当于自己的备忘录吧!也希望能帮到一些刚刚入门的兄弟们,如果是大神看到不足或者是可以优化的地方,希望指正,在此拜谢。
1.if else 操作
<where>
<choose>
<when test="userId != null and orgIds != null">
and ( a.member_id = #{userId}
<if test="orgIds != null and orgIds != ''">
or a.carId in
<foreach collection="orgIds" item="id" open="(" separator="," close=")">
#{id}W
</foreach>
</if>
)
</when>
<otherwise>
<if test="userId != null and userId != ''">
and a.member_id = #{userId}
</if>
<if test="orgIds != null">
and a.carId in
<foreach collection="orgIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</if>
</otherwise>
</choose>
</where>
2.数组遍历
<foreach collection="orgIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
3.字符拼接
#1.多字符拼接,可以是当前表的字段
concat(str1,str2,str3)
#2.模糊查询的拼接
<if test="name != null">
and name like CONCAT('%', #{name},'%')
</if>
4.IFNULL(mysql)
#如果第一个参数的表达式 expression 为 NULL,则返回第二个参数的备用值
IFNULL(expression, alt_value)
5.DATEDIFF(mysql)
#DATEDIFF() 函数返回两个日期之间的天数
DATEDIFF(date1,date2) //DATEDIFF(NOW(),createTime)>3
6.CASE WHEN THEN
# 满足when后面的条件走对应的then ,最终结果赋值到xxx字段
CASE
WHEN ... THEN ...
WHEN ... THEN ...
END AS xxx
7.CASE WHEN ELSE
# 满足when后面的条件走对应的then,否则走else,最终结果赋值到xxx字段
CASE
WHEN ... THEN ...
ELSE ...
END AS xxx
结束语
目前先写这么多吧!后期想起来了,随时再加上,或者读者有什么想要加上的也可以给我留言,我这边添加上去方便咱们自己查找,也为服务一下别人
最后感谢各位的阅读,也希望多提宝贵的意见,或者多留言,帮忙完善当前文档,也为他人提供些帮助吧