SpringMVC页面向Controller提交数组+Mybatis传多个参数+MySQL一次写入多条记录

14 篇文章 0 订阅
13 篇文章 0 订阅
DAO层定义:
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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值