mybatis处理集合、数组参数使用in查询 您所在的位置:网站首页 数组条件怎么使用 mybatis处理集合、数组参数使用in查询

mybatis处理集合、数组参数使用in查询

2024-07-17 12:41| 来源: 网络整理| 查看: 265

对于mybatis的参数类型是集合数组的时候进行查询。

 

第一种:参数list ,使用mybatis的标签

1 SELECT * FROM TABLE_NAME AS a WHERE 2 3 a.id not in #{extraIds} 4 5 #{extraId} 6 7 8 9 参数讲解的: 10 11 collection:需要循环的集合 12 13 item:每次循环的参数名字 14 15 index:索引(0开始) 16 17 separator:分隔符 18 19 open:整个循环开始的分隔符 20 21 close:整个循环结束的分隔符 View Code

 

第二种:参数string的数组,即:${}

需要处理参数形成extraIds=('1','2','3')这种类型,需要拼接字符 ''

或直接使用extraIds=("1,2,3")这种类型

注意在mybatis中使用的是${},不能使用#{}否则报错

1 SELECT * FROM TABLE_NAME AS a WHERE 2 3 a.id not in ${extraIds} 4 5 一小段示类代码: 6 7 String extraIds = "1,2,3,4,5"; 8 9 String[] extraIdArray = extraIds.split(","); 10 extIds = ""; 11 for (String extraId : extraIdArray) { 12 if (extraId != null && !"".equals(extraId)) { 13 extraIds += ",'" + extraId + "'"; 14 } 15 16 } 17 18 extraIds = "(" + extraIds.substring(1) + ")" 19 20 拼接后:('1','2','3','4','5') View Code

 

第三种:使用QueryWrapper 直接用list

 

1 List activityHelpSponsorIds = Arrays.asList(1,2,3); 2 QueryWrapper queryWrapper = new QueryWrapper(); 3 queryWrapper.lambda().in(HelpLog::getActivityHelpSponsorId, activityHelpSponsorIds); 4 List helperlogList = helpLogService.list(queryWrapper); View Code

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有