@Param 的代码
@Insert("insert into sys_role_permission(permissionid,roleid) values (#{permissionId},#{roleId})")
int addRolePermission(@Param("permissionId") Integer pId, @Param("roleId") Integer roleId);
@Param:
- 当 @Insert 后面的条件有多个的时候
values (#{permissionId},#{roleId})
,并且方法中的int addRolePermission(@Param("permissionId") Integer pId, @Param("roleId") Integer roleId);
参数名pId,roleId和 sql中的条件参数#{permissionId} ,#{roleId} 的名称不一致的时候,就可以使用 @Param 来指定 sql后面的参数名 - 使用@Param后,接口中的参数顺序也可以打乱,只要id唯一即可
- 当只有一个参数的时候可以不使用此注解
- 主要是作用在dao层
/**
* 增加当前用户的所有角色信息
* @param ids
* @param userId
* @return
*/
@ResponseBody
@RequestMapping(value = "/role/add",method = RequestMethod.POST)
public Map<String,Object> addRoleUser(@RequestParam("ids") List<Integer> ids,@RequestParam("userId") Integer userId)
{
HashMap<String, Object> resultJson = new HashMap<>();
int result = 0;
try {
result = userService.addUserRole(ids, userId);
} catch (Exception e) {
resultJson.put("success", false);
resultJson.put("message", "授权失败");
e.printStackTrace();
}
if (result > 0) {
resultJson.put("success", true);
resultJson.put("message", "授权成功");
}
return resultJson;
}
@RequestParam
- 前端提交的form表单数据的name属性 和方法中的参数名不一致时 ,springMVC就无法自动封装参数,所以需要@RequestParam(前端name属性名称)来指定前端提交的表单的name属性的名称
- 当前端的name属性和方法的参数名一致的时候,可以不使用此注解
- 主要作用在Controller层
前端jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- 页面meta -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>用户角色管理</title>
<body>
<div>
<form id="userRoleForm" action="/user/role/add" method="post">
<table id="dataList"
class="table table-bordered table-striped table-hover dataTable">
<thead>
<tr>
<th class="" style="padding-right: 0px">
<input id="selall" type="checkbox" class="icheckbox_square-blue">
</th>
<th class="sorting_asc">ID</th>
<th class="sorting">角色名字</th>
<th class="sorting">角色描述</th>
</tr>
</thead>
<tbody>
<c:forEach items="${roleList}" var="role">
<tr>
<td><input name="ids" <c:if test="${fn:contains(userRoleIds,'['.concat(role.id).concat(']'))}">checked="checked"</c:if> type="checkbox" value="${role.id}"></td>
<td>${role.id}</td>
<td>${role.roleName }</td>
<td>${role.roleDesc}</td>
</tr>
</c:forEach>
</tbody>
</table>
<div class="modal-footer">
<%--
隐藏表单,用户ID
--%>
<input type="hidden" name="userId" value="${userId}" />
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
<button type="button" id="formbtn" class="btn btn-primary">提交更改</button>
</div>
</form>
</div>
</body>
<script>
$(function () {
$("#formbtn").click(function () {
$.ajax({
url:'/user/role/add',
type:'post',
data:$('#userRoleForm').serialize(),
dataType:'json',
success:function (data) {
if(data.success){
//添加成功
location.reload();
}else{
alert(data.message);
}
}
});
});
});
</script>
</html>