<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>