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

网友投稿 792 2022-11-08

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

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

目录使用not in与 in的写法in的用法not in的用法使用in查询时的注意事项当查询的参数只有一个时 当查询的参数有多个时

使用not in与 in的写法

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

in的用法

我的id是Long类型的

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

public List listByPackageId(List ids, String sysehMHrbtemCode) {

Map map = new HashMap();

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

FIND_IN_SET(id,#{ids})

not in的用法

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

public List listByPackageId(List ids, String systemCode) {

Map map = new HashMap();

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

NOT FIND_IN_SET(id,#{notids})

使用in查询时的注意事项

当查询的参数只有一个时

findByIds(List ids)

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

Select

from jria where ID in

open="(" separator="," close=")">

#{item}

open="(" separator="," close=")">

#{item}

findByIds(Long[] ids)

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

select

from jria where ID in

open="(" separator="," close=")">

#{item}

open="(" separator="," close=")">

#{item}

当查询的参数有多个时

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

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

下面是一个示例

Map params = new HashMap(2);

params.put("name", name);

params.put("ids", ids);

mapper.findByIdsMap(params);

select

from jria where ID in

open="(" separator="," close=")">

#{item}

完整的示例如下:

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

List findByIds(Long... ids);

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

select

from jria where ID in

open="(" separator="," close=")">

#{item}

open="(" separator="," close=")">

#{item}

完整的示例如下:

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

List findByIds(Long... ids);

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

select

from jria where ID in

open="(" separator="," close=")">

#{item}

open="(" separator="," close=")">

#{item}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:HDU 2106 decimal system(进制转换+模拟)
下一篇:HDU 2044 一只小蜜蜂...(递推+打表)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~