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

心已赠人 2023-10-07 18:59 105阅读 0赞
1 基本使用方法
  1. <if test="list!=null and list.size()!=0">
  2. </if>
2 结合 In 条件判断

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

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

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

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

mapper java 中 定义接口

  1. List<User> selectByIds(List idList);
  2. <select id="selectByIds" resultMap="BaseResultMap">
  3. SELECT
  4. *
  5. from t_user
  6. WHERE 1=1
  7. <if test="idList!=null ">
  8. and id in
  9. <foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
  10. #{id}
  11. </foreach>
  12. </if>
  13. </select>
  • collection:该属性的对应方法的参数类型可以是List、数组、Map。如果方法的参数类型不属于前三种,则必须和方法参数@Param指定的元素名一致。
  • item: 表示迭代过程中每个元素的别名。可以随便起名,但是必须跟元素中的#{}里面的名称一致。
  • index:表示迭代过程中每次迭代到的位置(下标)
  • open:前缀
  • close:后缀
  • separator:分隔符,表示迭代时每个元素之间以什么分隔。`

发表评论

表情:
评论列表 (有 0 条评论,105人围观)

还没有评论,来说两句吧...

相关阅读