mybatis参数是list,arry和map
来源:互联网 发布:卡尔曼滤波器 知乎 编辑:程序博客网 时间:2024/06/05 18:26
- 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list .
- 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array .
- 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.
public class Employees {
private Integer employeeId;
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
private Date hireDate;
private String jobId;
private BigDecimal salary;
private BigDecimal commissionPct;
private Integer managerId;
private Short departmentId;
}
映射文件:
<!--List:forech中的collection属性类型是List,collection的值必须是:list,item的值可以随意,Dao接口中参数名字随意 -->
<select id="getEmployeesListParams" 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的值必须是:list,item的值可以随意,Dao接口中参数名字随意 -->
<select id="getEmployeesArrayParams" 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,其它所有属性都是map.key,比如下面的departmentId -->
<select id="getEmployeesMapParams" 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>
Mapper类:
public interface EmployeesMapper {
List<Employees> getEmployeesListParams(List<String> employeeIds);
List<Employees> getEmployeesArrayParams(String[] employeeIds);
List<Employees> getEmployeesMapParams(Map<String,Object> params);
}
案例是给一个用户赋予多个权限,多个权限用其对应的主键 id 为参数,组成了 一个id数组,传给springMVC,然后springMVC传给mybatis,然后mybatis批量插入。其实类似的场景还有批量删除多个,也是类似的。
1. 前台页面
<thead><tr><th>权限选择</th><th>name</th><th>permission</th></tr></thead> <c:forEach var="priv" items="${list }"> <tr class="odd gradeX"> <td><input type="checkbox" name="priv_id" value="${priv.id}" /></td> <td><c:out value="${priv.name}"/></td> <td><c:out value="${priv.permission}"/></td> </tr> </c:forEach>
2. jquery获得选中的项的id值:
//jquery获取复选框值 var priv_ids =[];//定义一个数组 $('input[name="priv_id"]:checked').each(function(){ // 遍历每一个name为priv_id的复选框,其中选中的执行函数 priv_ids.push($.trim($(this).val())); // 将选中的值添加到数组priv_ids中 }); console.log(priv_ids); var indata = {userId:user_id, privIds:priv_ids}; $.post("/ems/priv/setPrivilege", indata, function(data){ if(data != null && data.result == 'ok'){ console.log(data.msg); alert(data.msg); }else{ alert(data.msg); } }, 'json');
提交的json格式的数据: var indata = {userId:user_id, privIds:priv_ids};
其中的 priv_ids 是一个有 id 组成的数组。
3. springMVC接收数组参数:
@RequestMapping(value="/setPrivilege")@ResponseBodypublic void setPrivilege(@RequestParam(value = "privIds[]") Integer[] privIds, Integer userId, PrintWriter writer){ System.out.println(JSON.toJSONString(privIds)); System.out.println(JSON.toJSONString(userId)); int result = this.privilegeService.setPrivilegeForUser(privIds, userId); System.out.println("result=" + JSON.toJSONString(result)); Map<String, String> map = new HashMap<>(); if(result > 0){ map.put("result", "ok"); map.put("msg", "设置成功"); writer.write(JSON.toJSONString(map)); }}
我们看到使用了: @RequestParam(value = "privIds[]" ) Integer[] privIds 来获取前台传来的数组参数。
springMVC接收参数时,最好不要使用 int, long等原始类型,而应该使用它们对应的包装类型,不然当传入的参数为空时,会报错,而包装类型可以使用null表示传入的空值。
4. service层的处理,很简单,直接使用map向mybatis传递参数:
@Service("privilegeService")@Transactionalpublic class PrivilegeServiceImpl implements PrivilegeService { @Autowired private PrivilegeMapper privilegeMapper; @Override @Transactional(readOnly=true) public List<Privilege> getAllPrivilege() { return privilegeMapper.getAllPrivilege(); } @Override public int setPrivilegeForUser(Integer[] privIds, Integer userId) { Map<String, Object> map = new HashMap<>(); map.put("privIds", privIds); map.put("userId", userId); return this.privilegeMapper.setPrivilegeForUser(map); }}
5. 最后看 mybatis 的 xml 中的sql如何写:
<insert id="setPrivilegeForUser" parameterType="map"> insert into user_privilege(user_id, privilege_id) values <foreach collection="privIds" index="index" item="item" separator=","> ( #{userId}, #{item} ) </foreach> </insert>
我们看到使用了 foreach 来循环传递进来的数组 privIds ,最后组成的sql语句如下所示:
insert into user_privilege(user_id, privilege_id) values (3, 1),(3,2),(3,3),(3,4)
user_id 不变,而privilege_id 是数组 privIds 中的循环出来的 id 值。其实就是数据库的批量插入。
6. 批量删除多个的处理
删除时,和前面批量插入处理也是极其类似的,只在最后mybatis中xml中sql的写法有点区别:
<delete id="deleteByIds" parameterType="java.util.List"> delete from user_privilege where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </delete>
比较批量删除和批量插入,可以看出 foreach 中的 open="(" 和 close=")" 只在循环的开始和结束会加上 ,而 separator="," 是每循环一次,就加一次逗号 。
7. 批量插入传入对象List的例子:
<insert id="batchInsertStudent" parameterType="java.util.List"> insert into student (id,name,sex,tel,address) values <foreach collection="list" item="item" index="index" separator="," > (#{item.id},#{item.name},#{item.sex},#{item.tel},#{item.address}) </foreach> </insert>
其实掌握了 批量插入和批量删除,批量更新也是一样的。
- mybatis参数是list,arry和map
- Mybatis 中遍历map 参数中的 list 和 array 属性
- mybatis list<map>作为参数 foreach循环
- mybatis复合参数 map中包含list的参数
- Mybatis传入多个参数 Map List作为参数
- mybatis 关于 传入参数为集合list和map的写法
- 在mybatis中传多个参数,并且遍历其中的list和数组,使用map集合
- mybatis传递Map和List集合示例
- MyBatis传入参数为集合 list 数组 map写法
- MyBatis传入参数为集合 list 数组 map写法
- MyBatis传入参数为集合 list 数组 map写法
- MyBatis传入参数为集合 list 数组 map写法
- MyBatis传入参数为list、数组、map写法
- MyBatis传入参数为集合 list 数组 map写法
- mybatis 查询参数为list、数组、map的情况
- MyBatis传入参数为list、数组、map写法
- MyBatis传入参数为list、数组、map写法
- 在mybatis查询时使用map以及list参数
- Android问题——PopupWindow的isShowing()一直都是返回false
- Effective cpp 读书笔记8
- scala:Guides and Overviews阅读笔记之一 -- Mutable and Immutable Collections
- Hadoop中Map端shuffle过程及源码解析
- poj3185//BFS随便切...
- mybatis参数是list,arry和map
- KMP算法简单实现
- python安装与windows开发环境搭建
- iOS 图片轮播器
- EventBus的使用与深入学习
- python链接mysql数据库并以键值对的形式查询显示
- cmake入门教程
- 很牛的netcat
- android图文居中按钮控件