Mapper.xml檔案中如何判斷多個引數不為空和null
阿新 • • 發佈:2019-02-17
第一種:使用where標籤
<select id="***" resultMap="BaseResultMap" parameterType="java.util.Map">
select t.* from 表名 t
<where>
<if test=" 傳進來的欄位 != null and 傳進來的欄位 != ''">
and t.欄位 like '%${傳進來的欄位}%'
</if>
<if test="傳進來的欄位 != null and 傳進來的欄位 != ''">
and t.ASSET_TYPE like '%${assetType}%'
</if>
</where>
</select>
Mybatis中where 標籤知道只有在一個以上的if條件有值的情況下才去插入“where”子句,若最後的內容是“and”或“or”開頭的,where 標籤會知道如何將他們去除
第二種:使用trim標籤
<select id="***" resultMap="BaseResultMap" parameterType="java.util.Map">
select t.* from 表名 t
<trim prefix="where" prefixOverrides="and|or">
<if test=" 傳進來的欄位 != null and 傳進來的欄位 != ''">
and t.欄位 like '%${傳進來的欄位}%'
</if>
<if test="傳進來的欄位 != null and 傳進來的欄位 != ''">
and t.ASSET_TYPE like '%${assetType}%'
</if>
</trim>
</select>
Mybatis中trim是更靈活的去處多餘關鍵字的標籤,他可以實現where的效果
<select id="***" resultMap="BaseResultMap" parameterType="java.util.Map">
select t.* from 表名 t
<where>
<if test=" 傳進來的欄位 != null and 傳進來的欄位 != ''">
and t.欄位 like '%${傳進來的欄位}%'
</if>
<if test="傳進來的欄位 != null and 傳進來的欄位 != ''">
and t.ASSET_TYPE like '%${assetType}%'
</if>
</where>
</select>
Mybatis中where 標籤知道只有在一個以上的if條件有值的情況下才去插入“where”子句,若最後的內容是“and”或“or”開頭的,where 標籤會知道如何將他們去除
第二種:使用trim標籤
<select id="***" resultMap="BaseResultMap" parameterType="java.util.Map">
select t.* from 表名 t
<trim prefix="where" prefixOverrides="and|or">
<if test=" 傳進來的欄位 != null and 傳進來的欄位 != ''">
and t.欄位 like '%${傳進來的欄位}%'
</if>
<if test="傳進來的欄位 != null and 傳進來的欄位 != ''">
and t.ASSET_TYPE like '%${assetType}%'
</if>
</trim>
</select>
Mybatis中trim是更靈活的去處多餘關鍵字的標籤,他可以實現where的效果