mybatis根據使用者id查詢角色以及角色對應的許可權
阿新 • • 發佈:2018-12-10
查詢到的資料
{
"name": "admin",
"role": {
"id": 1,
"name": "管理員",
"permissionList": [{
"id": 1,
"name": "總公司"
}, {
"id": 2,
"name": "資訊部"
}]
},
"userId": 39
}
1.mapper檔案:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <!--column:表示資料庫中的欄位列名;property:表示實體類對應的欄位名--> <mapper namespace="com.sqxinxibu.neikanhoutai.dao.UserDao" > <!--使用者--> <!--<resultMap id="UserRolePermissionMap" type="UserRolePermission">--> <!--<result property="userId" column="id" />--> <!--<result property="name" column="name" jdbcType="VARCHAR"/>--> <!--<collection property="roleList" column="id" javaType="list" select="selectAllRoles"></collection>--> <!--<collection property="permissionList" column="id" javaType="list" select="selectAllpermissions"></collection>--> <!--</resultMap>--> <resultMap id="UserRolePermissionMap" type="UserRolePermission"> <result property="userId" column="id" /> <result property="name" column="name" jdbcType="VARCHAR"/> <collection property="role" column="id" javaType="com.sqxinxibu.neikanhoutai.entity.Roles" select="selectAllRoles"></collection> </resultMap> <!--許可權--> <resultMap id="PermissionMap" type="com.sqxinxibu.neikanhoutai.entity.Permission" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="name" property="name" jdbcType="VARCHAR" /> </resultMap> <!--角色--> <resultMap id="RolesMap" type="com.sqxinxibu.neikanhoutai.entity.Roles" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="name" property="name" jdbcType="VARCHAR" /> <!--許可權集合--> <collection property="permissionList" column="id" javaType="list" select="selectAllpermissions"></collection> </resultMap> <!--根據角色id查詢許可權(先從角色許可權關聯表查詢角色id相等,在查詢許可權id,根據許可權id在許可權表裡查詢許可權)--> <select id="selectAllpermissions" resultType="com.sqxinxibu.neikanhoutai.entity.Permission" parameterType="java.lang.Integer"> select id,name from sys_menu where id in (select sys_role_menu.menu_id from sys_role_menu where sys_role_menu.role_id=#{id}) </select> <!--根據使用者中的角色查詢角色--> <select id="selectAllRoles" resultMap="RolesMap" parameterType="java.lang.Integer"> select id,name from sys_role where id in (select sys_user_role.role_id from sys_user_role where sys_user_role.user_id=#{id}) </select> <select id="chaxunByUserId" resultMap="UserRolePermissionMap" parameterType="java.lang.Integer" > select * from user where id=#{id} </select> </mapper>
2.實體類:
2.1返回的實體類:
//使用者返回物件 public class UserRolePermission { private int userId; private String name; private Roles role; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Roles getRole() { return role; } public void setRole(Roles role) { this.role = role; } }
2.2:角色實體類:
//角色 public class Roles { private Integer id; private String name; private List<Permission> permissionList; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Permission> getPermissionList() { return permissionList; } public void setPermissionList(List<Permission> permissionList) { this.permissionList = permissionList; } @Override public String toString() { return "Roles [id=" + id + ", name=" + name + ", permissionList=" + permissionList + "]"; } }
2.3許可權實體類:
//許可權 public class Permission { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Permission{" + "id=" + id + ", name=" + name + '}'; } }
3.表結構:
3.1使用者表:
3.2角色表:
3.3許可權表:
3.2使用者和角色關聯表:
3.5角色和許可權關聯表:
4.dao層:
public interface UserDao { //根據使用者id查詢角色和許可權資訊 UserRolePermission chaxunByUserId(int id); //根據 }
5.service層:
@Service("userService") public class UserServiceImpl implements UserService { @Resource private UserDao userDao; public UserRolePermission chaxunByUserId(int id){ UserRolePermission userRolePermission=userDao.chaxunByUserId(id); System.out.println("查詢到的使用者資訊==="+ JSON.toJSONString(userRolePermission)); return userRolePermission; } }
6.controller層:
@RestController public class IndexController { @Autowired private UserServiceImpl userService; @RequestMapping("/test") public String test(@RequestParam int userid){ System.out.println("使用者id==="+userid); UserRolePermission userRolePermission=userService.chaxunByUserId(userid); String json=JSON.toJSONString(userRolePermission); System.out.println("查詢返回資訊:"+ json); return json; } }