学无先后,达者为师

网站首页 java综合 正文

mybatis中使用not in与 in的写法说明_java

作者:飞翔的丘八   更新时间: 2022-03-27 java综合

使用not in与 in的写法

首先声明我不是很喜欢用foreach,所以我的代码中很少出现foreach。不废话了,上代码:

in的用法

我的id是Long类型的

service方法,有一个Long的集合:

public List<RbacMenu> listByPackageId(List<Long> ids, String systemCode) {
    Map<String, Object> map = new HashMap<String, Object>();
    if(ids.size()!=0) {
        StringBuilder sbd = new StringBuilder();
        for(Long cateIds:ids){
            sbd.append(cateIds+",");
        }
        String idStr = sbd.toString();
        idStr = idStr.substring(0,idStr.length()-1);
        map.put("ids", idStr);
    }

实体类.xml

select * from xxx where 
<if test="ids != null"> FIND_IN_SET(id,#{ids})   </if>

not in的用法

service方法,有一个Long的集合:

public List<RbacMenu> listByPackageId(List<Long> ids, String systemCode) {
    Map<String, Object> map = new HashMap<String, Object>();
    if(ids.size()!=0) {
        StringBuilder sbd = new StringBuilder();
        for(Long cateIds:ids){
            sbd.append(cateIds+",");
        }
        String idStr = sbd.toString();
        idStr = idStr.substring(0,idStr.length()-1);
        map.put("notids", idStr);
    }

实体类.xml

select * from xxx where 
<if test="notids != null"> NOT FIND_IN_SET(id,#{notids})   </if>

使用in查询时的注意事项

当查询的参数只有一个时 

findByIds(List<Long> ids)

a 如果参数的类型是List, 则在使用时,collection属性要必须指定为 list

 <select id="findByIdsMap" resultMap="BaseResultMap">
         Select
         <include refid="Base_Column_List" />
         from jria where ID in
                  <foreach item="item" index="index" collection="list" 
                         open="(" separator="," close=")">
                        #{item}
                </foreach>
  </select> 
 findByIds(Long[] ids)

b 如果参数的类型是Array,则在使用时,collection属性要必须指定为 array

  <select id="findByIdsMap" resultMap="BaseResultMap">
                 select
                 <include refid="Base_Column_List" />
          from jria where ID in
                  <foreach item="item" index="index" collection="array" 
                         open="(" separator="," close=")">
                        #{item}
                </foreach>
  </select>

当查询的参数有多个时

例如 findByIds(String name, Long[] ids) 

这种情况需要特别注意,在传参数时,一定要改用Map方式, 这样在collection属性可以指定名称 

下面是一个示例

         Map<String, Object> params = new HashMap<String, Object>(2);
        params.put("name", name);
         params.put("ids", ids);
        mapper.findByIdsMap(params);
 
 <select id="findByIdsMap" resultMap="BaseResultMap">
                 select
                 <include refid="Base_Column_List" />
          from jria where ID in
                  <foreach item="item" index="index" collection="ids" 
                         open="(" separator="," close=")">
                        #{item}
                </foreach>
   </select> 

完整的示例如下:

例如有一个查询功能,Mapper接口文件定义如下方法:

List<Jria> findByIds(Long... ids);

使用 in 查询的sql拼装方法如下:

 <select id="findbyIds" resultMap="BaseResultMap">
                 select
                 <include refid="Base_Column_List" />
          from jria where ID in
                  <foreach item="item" index="index" collection="array" 
                         open="(" separator="," close=")">
                        #{item}
                </foreach>
  </select> 

原文链接:https://blog.csdn.net/qq_35956992/article/details/90641073

栏目分类
最近更新