One - One Code All

Blog Content

SSM-Mybatis实现select in 查询

Java   2018-03-14 21:51:06

mapper.xml

select * from user where id in

		#{id}

java:

List idList=new ArrayList();
		idList.add("1002");
		idList.add("6002");
		idList.add("3206");


如果是字符串:

mapper.xml

select * from user where id in ( ${ids} )  

java:

List idList=new ArrayList();
		idList.add("1002");
		idList.add("6002");
		idList.add("3206");
		String ids = "'"+StringUtils.join(codeList,"','")+"'";


上述依赖包

commons-lang3-3.3.2.jar


如果是list:

mapper.xml

select * from table

    id in 
    #{item}
    

java:

public abstract List findByIds(@Param("ids")List ids);

如果是数组:

mapper.xml

  
select  
  
from tabs where ID in  
  
 #{item}  
  


java:

public abstract List findByIds(@Param("ids")Long[] ids);


当查询的参数有多个时,例如 findByIds(String name, Long[] ids)


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

下面是一个示例

mapper.xml

  
 select  
   
 from tabs where ID in  
   
  #{item}  
   

java:

Map params = new HashMap(2);
params.put("name", name);
params.put("ids", ids);
mapper.findByIdsMap(params);



上一篇:SSM-SpringBoot JAVA导出数据到CSV文件
下一篇:SSM-Mybatis保存多条记录,foreach循环列表和数组select in

The minute you think of giving up, think of the reason why you held on so long.