一、判斷 Integer、Long?等常數(shù)類參數(shù)
<if test="Integer != null">
and tstc.Integer = #{Integer}
</if>
注意:判斷常數(shù)類參數(shù),只能判斷 != null,不能判斷 != ''?否則判斷不會生效
二、判斷 String?字符串類參數(shù)
<if test="str != null and str != ''">
and t.field = #{str}
</if>
注意:判斷字符串類參數(shù)可以判斷?!= ''
三、判斷參數(shù)值與指定的值,是否相等或不相等
//判斷常數(shù)類型
<if test="Long != null and Long == 0">
and t.Long = #{Long}
</if>
<if test="Long != null and Long != 1">
and t.Long = #{Long}
</if>
//判斷字符串類型
<if test="str != null and str == 'aa'.toString()">
and t.str = #{str}
</if>
<if test="str != null and str != 'aa'.toString()">
and t.str = #{str}
</if>
注意:
1、判斷 Integer、Long?等常數(shù)類型等于某個值,值不需要加引號
2、判斷 String?字符串類型等于某個值,值需要加單引號,并且用 .toString() 轉(zhuǎn)成字符串類型條件才會生效
四、判斷 List、Set?集合類參數(shù)
<if test="lists != null and lists.size() > 0">
and t.id in
<foreach collection="lists" open="(" item="item" separator="," close=")">
#{item}
</foreach>
</if>
注意:
1、判斷集合類參數(shù),判斷 != null?的同時一定要判斷?lists.size() > 0
2、否則 lists?集合不為 null?但集合中沒有元素,也會進入循環(huán)是有問題的
五、大于、小于等特殊字符轉(zhuǎn)義寫法
<if test="date != null">
and date_format(t.date, '%Y-%m-%d') <= date_format(#{date}, '%Y-%m-%d')
</if>
注意:
1、入?yún)傩灶愋蜑閐ate類型,只需要判斷 != null?即可
2、>=?為大于等于、<=?為小于等于,還有其他轉(zhuǎn)義寫法自行查閱
六、#{}、${}?獲取參數(shù)的區(qū)別及使用場景文章來源:http://www.zghlxwxcb.cn/news/detail-490561.html
//一般常規(guī)入?yún)⑹褂?#{} 占位符即可
<if test="str != null and str != ''">
and t.str = #{str}
</if>
//特殊情況可以使用 ${} 拼接
<if test="lists != null and lists.size() > 0">
order by t.sort_num,
<foreach collection="lists" item="sortStr" separator=",">
t.${sortStr}
</foreach>
${sortType}
</if>
注意:
1、#{}?是占位符,${}?是拼接參數(shù)
2、常規(guī)獲取參數(shù)使用 #{}?占位符即可,特殊情況也可以使用 ${}?拼接(例如根據(jù)多字段排序,需要使用 ${} 直接拼接,使用 #{}?不生效會報錯)
3、當使用 #{}?占位符不生效或報錯的情況下,直接使用 ${}?拼接即可文章來源地址http://www.zghlxwxcb.cn/news/detail-490561.html
到了這里,關(guān)于MyBatis Plus Mapper.xml映射文件常用標簽<if>、<foreach>、#{}、${}等的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!