Mybatis where 1=1 和 標簽


<select id="selSampleListByIDX4" resultMap="BaseResultMap" parameterType="cn.com.git.cbs.datamodel.TBL_Sample">
select
<include refid="Base_Column_List" />
from SAMPLE
where 1=1
<if test="samplenumber != null" >
AND SAMPLENUMBER = #{samplenumber,jdbcType=DECIMAL}
</if>
</select>

 

Mybatis  之前拼條件的時候 寫法  where 1=1,也可以使用<where>標簽

 

<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG
<where>
<if test="state != null">
state = #{state}
</if>
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</where>
</select>

where 元素知道只有在一個以上的if條件有值的情況下才去插入“WHERE”子句。而且,若最后的內容是“AND”或“OR”開頭的,where 元素也知道如何將他們去除。

如果 where 元素沒有按正常套路出牌,我們還是可以通過自定義 trim 元素來定制我們想要的功能。比如,和 where 元素等價的自定義 trim 元素為:

 <trim prefix="WHERE" prefixOverrides="AND |OR "> ... </trim>  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM