SpringMVC页面向Controller提交数组+Mybatis传多个参数+MySQL一次写入多条记录
DAO层定义:
Service层
控制层,authId接收页面仅提交一个的数据,authId[]接收页面提交多个authID的数据。required=false,如果页面一个authId也没提交,两个参数都是null。
页面html
int insertRole2Authorities(@Param("roleId")int roleId, @Param("authorities")List<Integer> authId);
XML文件:
<!-- 一个角色对应多个权限,批量insert --> <insert id="insertRole2Authorities" useGeneratedKeys="true" > insert into tm05_roles_authorities (role_id,authority_id) values <foreach collection="authorities" item="authId" index="index" separator="," > (#{roleId,jdbcType=INTEGER},#{authId}) </foreach> </insert>
Service层
@Transactional public int writeRoleAuth(int roleId, List<Integer> authId) { int updates = 0; updates += roleDao.deleteAllAuthByRoleId(roleId); updates += roleDao.insertRole2Authorities(roleId,authId); return updates; }
控制层,authId接收页面仅提交一个的数据,authId[]接收页面提交多个authID的数据。required=false,如果页面一个authId也没提交,两个参数都是null。
@ResponseBody @RequestMapping(value={"/edit_auth"}, method=RequestMethod.POST) public Map<String, Object> editRoleAuthorites( @RequestParam(value="roleId", required=true)int roleId, @RequestParam(value="authId", required=false)Integer authId, @RequestParam(value="authId[]", required=false)List<Integer> authorities) { Map<String, Object> modal = new HashMap<String, Object>(3); try{ int ok = 0; if(authId == null && authorities!=null && authorities.size()>0) ok = service.writeRoleAuth(roleId,authorities); else if(authId != null && authorities==null){ authorities = new ArrayList<Integer>(); authorities.add(authId); ok = service.writeRoleAuth(roleId,authorities); } else ok = dao.deleteAllAuthByRoleId(roleId); modal.put("ok", ok); } catch(Exception e){ modal.put("ok", 0); modal.put("error",e.getMessage()); e.printStackTrace(); } return modal; }
页面html
div class="input-group"> <span class="input-group-addon"> <input type="checkbox" value="1" name="authId"></span> <label class="form-control">权限1 -- 具体内容</label> </div> <div class="input-group"> <span class="input-group-addon"> <input type="checkbox" value="2" name="authId"></span> <label class="form-control">权限1 -- 具体内容</label> </div> <div class="input-group"> <span class="input-group-addon"> <input type="checkbox" value="3" name="authId"></span> <label class="form-control">权限1 -- 具体内容</label> </di<span style="font-family: Arial, Helvetica, sans-serif;">v></span>
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇: MyBatis传入多个参数,传入数组和列表数据的处理