使用mybatis时Mapper.xml文件中如何判断多个参数不为空和null

第一种:使用where标签


<select id="***" resultMap="BaseResultMap" parameterType="java.util.Map">
select t.* from 表名 t

<where>

<if test=" 传进来的字段 != null and 传进来的字段 != ''">
andt.字段 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的效果


版权声明:本文为WangKun_0612原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。