mybatis if test 不為空字符串或null的解決
更新時間:2022年11月30日 14:50:44 作者:在奮斗的大道
這篇文章主要介紹了mybatis if test 不為空字符串或null的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
mybatis if test 不為空字符串或null
<sql id="public_content">
<if test="productId != null and productId !=''" >
and a.product_id = #{productId,jdbcType=VARCHAR}
</if>
<if test="productDefinId != null" >
and a.product_defin_id = #{productDefinId,jdbcType=VARCHAR}
</if>
<if test="productUid != null and productUid !=''">
and a.product_uid = #{productUid,jdbcType=VARCHAR}
</if>
<if test="productKey != null" >
and a.product_key = #{productKey,jdbcType=VARCHAR}
</if>
<if test="prouuctSecret != null" >
and a.prouuct_secret = #{prouuctSecret,jdbcType=VARCHAR}
</if>
<if test="productPass != null" >
and a.product_pass = #{productPass,jdbcType=VARCHAR}
</if>
<if test="productVisitPass != null" >
and a.product_visit_pass = #{productVisitPass,jdbcType=VARCHAR}
</if>
<if test="createTime != null and createTime !=''" >
and a.create_time = #{createTime,jdbcType=VARCHAR}
</if>
<if test="pageSize != null and pageNum !=null " >
ORDER BY a.product_id DESC limit #{pageNum},#{pageSize}
</if>
</sql>mybatis中if test判斷數值字符串注意項
?? ?<if test="cutList != null">
?? ? ? ?<if test="isInterrupt == '1'.toString() ">
?? ??? ?AND A.basic_id IN (
?? ??? ? <foreach collection="cutList" item="item" index="index" separator="," >
? ? ? ? ? ? ??? ??? ?#{item}
? ? ? ? ? ?? ?</foreach>
? ? ? ? ? ? ? ? )
? ? ? ? ? ? </if>
? ? ? ? ? ? <if test="isInterrupt == '0'.toString() ">
?? ??? ?AND A.basic_id NOT IN (
?? ??? ?<foreach collection="cutList" item="item" index="index" separator="," >
? ? ? ? ? ? ??? ??? ?#{item}
? ? ? ? ? ?? ?</foreach>
? ? ? ? ? ? ? ? )
? ? ? ? ? ? </if>
?? ?</if> ? ? ? ? ? ? ? ? ? ? ? 1. Mybatis中 if test 判斷數值字符串注意項
if test 判斷是否為某一數值字符串時需在數值字符串后加上toString()方法
如:
<if test="isInterrupt == '1'.toString() ">
2. Mybatis中遍歷list入參
此處還有個知識點是mybatis使用foreach標簽來遍歷入參list。
如:
?? ?<if test="isInterrupt == '1'.toString() ">
?? ??? ?AND A.basic_id IN (
?? ??? ?<foreach collection="cutList" item="item" index="index" separator="," >
? ? ? ? ? ? ??? ? ? ?#{item}
? ? ? ? ? ?? ?</foreach>
? ? ? ? ? ? ? ? )
? ? ? ? ? ? </if>collection為需遍歷數組,item為當前循環(huán)的變量,index為計數器,separator=","指循環(huán)一次則以“,”分隔
這里IN 的()可以直接用open="(" close=")"屬性設置
<foreach collection="cutList" item="item" index="index" open="(" close=")" separator="," >總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java并發(fā)編程之JUC并發(fā)核心AQS同步隊列原理剖析
AbstractQueuedSynchronizer 簡稱 AQS,可能我們幾乎不會直接去使用它,但它卻是 JUC 的核心基礎組件,支撐著 java 鎖和同步器的實現(xiàn),大神 Doug Lea 在設計 JUC 包時希望能夠抽象一個基礎且通用的組件以支撐上層模塊的實現(xiàn),AQS 應運而生2021-09-09

