Mybatis 示例 foreach 以Map 传递参数 (SpringMvc+Spring+Mybatis)
转自:https://blog.csdn.net/sunchuankai/article/details/79619926
Contrller
@RequestMapping("/userList")@ResponseBody
public List getUserList(String name,String role,HttpServletRequest request){
List roleList=new ArrayList();
if(role.length()>0){
String r[]=role.split(",");
for (String ro:r ) {
roleList.add(ro);
}
}
Map paramMap=new HashMap();
paramMap.put("name",name);
if(roleList.isEmpty()){
paramMap.put("roles",null);
}else{
paramMap.put("roles",roleList);
}
List list =userService.getAll3(paramMap);
System.out.println(list.toArray().toString());
return list;
}
Service
@Service("userService") public class UserServiceImpl implements UserServiceI { private UserMapper userMapper; public UserMapper getUserMapper() { return userMapper; } @Autowired public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } public User getUserById(String id) { return userMapper.selectByPrimaryKey(id); } public boolean userLogin(User user) { User u =userMapper.userLogin(user); if (u !=null){ return true; }else { return false; } } public Role getUserRole(User user) { List list =userMapper.getUserRole(user); if(!list.isEmpty()){ return (Role) list.get(0); }else{ return null; } } public List<Menu> getRoleMenu(Role role) { return userMapper.getRoleMenu(role); } public List<User> getAll() { return userMapper.getAll(); } public List<User> getAll2() { return userMapper.getAll2(); } public List<User> getAll3(Map paramMap) { return userMapper.getAll3(paramMap); } }
Dao (UserMapper.java)
List<User> getAll3(Map paramMap);
mapper.xml
<select id="getAll3" resultMap="userResultMap3" parameterType="java.util.Map"> SELECT tuser.ID, tuser.CREATDATETIME, tuser.MODIFYDATETIME, tuser.`NAME`, tuser.PWD, tuser.CREATE_TIME, tuser.UPDATE_TIME, trole.ID role_id, trole.TEXT role_text FROM tuser JOIN tuser_trole ON tuser.ID = tuser_trole.USER_ID JOIN trole ON tuser_trole.ROLE_ID = trole.ID where 1=1 <if test="name != null and name != ''"> and tuser.name like "%"#{name,jdbcType=VARCHAR}"%" </if> <if test="roles != null and roles !='' "> and trole.ID in <foreach collection="roles" index="index" item="roleList" open="(" separator="," close=")"> #{roleList} </foreach> </if></select>
其中 roles 为 参数map集合中的"KEY" ,item ="roleList" 中roleList 为参数map 集合中roles(key) 对应的Value
#{roleList} 为每条显示的数据
MyBatis中foreach传入参数为list、数组、map的不同写法
转自:https://blog.csdn.net/fjian123/article/details/79457797
最近在做项目中遇到一个mybatis的问题,整了好久,怎么改都报错,最后发现竟然是写法的问题。现将此坑总结如下。
collection属性是在使用foreach的时候最关键的也是最容易出错的,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,如果不区分,就容易出错。
主要有一下3种情况:
(1)如果传入的是单参数且参数类型是一个List的时候,collection属性值为list .
(2)如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array .(笔者当时就错在数组这)
(3)如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.
这里还要提到其中的foreach的用法:foreach主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。属性主要有item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束。
下面是三种类型数据的示例代码。
<!--List:forech中的collection属性类型是List,collection的值必须是:list,item的值可以随意,Dao接口中参数名字随意 -->
<select id="getIteamsList" resultType="Employees">
select *
from EMPLOYEES e
where e.EMPLOYEE_ID in
<foreach collection="list" item="employeeId" index="index"
open="(" close=")" separator=",">
#{employeeId}
</foreach>
</select>
<!--Array:forech中的collection属性类型是array,collection的值必须是:array,item的值可以随意,Dao接口中参数名字随意 -->
<select id="getItemsArray" resultType="Employees">
select *
from EMPLOYEES e
where e.EMPLOYEE_ID in
<foreach collection="array" item="employeeId" index="index"
open="(" close=")" separator=",">
#{employeeId}
</foreach>
</select>
<!--Map:forech中的collection属性是map.key-->
<select id="getItemsMap" resultType="Employees">
select *
from EMPLOYEES e
<where>
<if test="departmentId!=null and departmentId!=''">
e.DEPARTMENT_ID=#{departmentId}
</if>
<if test="employeeIdsArray!=null and employeeIdsArray.length!=0">
AND e.EMPLOYEE_ID in
<foreach collection="employeeIdsArray" item="employeeId"
index="index" open="(" close=")" separator=",">
#{employeeId}
</foreach>
</if>
</where>
</select>
mybatis实现in传入数组查询
转自:https://blog.csdn.net/hcwbr123/article/details/78421643
现象:
mybatis实现in语句传入数组
方法:
1:数据样式
2:实现方法
3:sql语句