簡介
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
結束語
目前先寫這麼多吧!後期想起來了,随時再加上,或者讀者有什麼想要加上的也可以給我留言,我這邊添加上去友善咱們自己查找,也為服務一下别人
最後感謝各位的閱讀,也希望多提寶貴的意見,或者多留言,幫忙完善目前文檔,也為他人提供些幫助吧