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

来源:互联网 发布:淘宝推广平台哪个好 编辑:程序博客网 时间:2024/06/03 12:56

第一种:使用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的效果

阅读全文
0 0