Mybatis使用IN语句查询(模糊查询)
一、需要执行的SQL
select fgroup.id AS firmGroupId,fgroup.firmName,ftype.firmTypeName from org_firm_group fgroup LEFT JOIN org_firm_type ftype ON ftype.id=fgroup.firmTypeId WHERE fgroup.id in(1289,1423,1386,1353)
二、接收的Mapper
@Override
public ArrayList<HashMap<String,Object>> findUserId(List<Integer> firmGroupId) {
return baseMapper.findUserId(firmGroupId);
}
三、Mapper.xml
<select id="findUserId" resultType="java.util.HashMap">
select fgroup.id AS firmGroupId,fgroup.firmName,ftype.firmTypeName
from org_firm_group fgroup
LEFT JOIN org_firm_type ftype ON ftype.id=fgroup.firmTypeId
WHERE fgroup.id IN
<foreach item="item" index="index" collection="firmGroupId" open="(" separator="," close=")">
(#{item})
</foreach>
</select>
MyBatis中提供了foreach语句实现IN查询,foreach语法如下:
1. foreach语句中, collection属性的参数类型可以使:List、数组、map集合
2. collection: 必须跟mapper.java中@Param标签指定的元素名一样
3. item: 表示在迭代过程中每一个元素的别名,可以随便起名,但是必须跟元素中的#{
}里面的名称一样。
4. index:表示在迭代过程中每次迭代到的位置(下标)
5. open:前缀, sql语句中集合都必须用小括号()括起来
6. close:后缀
7.separator:分隔符,表示迭代时每个元素之间以什么分隔
