Mybatis if test 判断 list不为 null 并且判断集合大小不为0

1 基本使用方法

<if test="list!=null and list.size()!=0">

</if>

2 结合 In 条件判断

如查询两个用户的用户信息,SQL 如下

select * from user where id in ( '1231' , '2323' )

在使用Mybaits 查询里,传入所要查询的用户 ID 集合,可以是数组也可以是集合类型。

如果参数的类型是List, 则在使用如下

mapper java 中 定义接口

List<User> selectByIds(List idList);

<select id="selectByIds" resultMap="BaseResultMap">
	SELECT
	*
	from t_user
	WHERE 1=1
	 <if test="idList!=null ">
            and id in
            <foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
</select>

  • collection:该属性的对应方法的参数类型可以是List、数组、Map。如果方法的参数类型不属于前三种,则必须和方法参数@Param指定的元素名一致。
  • item: 表示迭代过程中每个元素的别名。可以随便起名,但是必须跟元素中的#{}里面的名称一致。
  • index:表示迭代过程中每次迭代到的位置(下标)
  • open:前缀
  • close:后缀
  • separator:分隔符,表示迭代时每个元素之间以什么分隔。`

猜你喜欢

转载自blog.csdn.net/zl18603543572/article/details/125006233