用户角色权限表
Posted chong-zuo3322
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用户角色权限表相关的知识,希望对你有一定的参考价值。
user_info.sql(用户表)
DROP TABLE IF EXISTS `user_info`; CREATE TABLE `user_info` ( `uid` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) DEFAULT ‘‘ COMMENT ‘用户名‘, `password` varchar(256) DEFAULT NULL COMMENT ‘登录密码‘, `name` varchar(256) DEFAULT NULL COMMENT ‘用户真实姓名‘, `id_card_num` varchar(256) DEFAULT NULL COMMENT ‘用户身份证号‘, `state` char(1) DEFAULT ‘0‘ COMMENT ‘用户状态:0:正常状态,1:用户被锁定‘, PRIMARY KEY (`uid`), UNIQUE KEY `username` (`username`) USING BTREE, UNIQUE KEY `id_card_num` (`id_card_num`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
sys_role.sql(角色表)
DROP TABLE IF EXISTS `sys_role`; CREATE TABLE `sys_role` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT ‘主键‘, `available` char(1) DEFAULT ‘0‘ COMMENT ‘是否可用0可用 1不可用‘, `role` varchar(20) DEFAULT NULL COMMENT ‘角色标识程序中判断使用,如"admin"‘, `description` varchar(100) DEFAULT NULL COMMENT ‘角色描述,UI界面显示使用‘, PRIMARY KEY (`id`), UNIQUE KEY `role` (`role`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
sys_user_role.sql(用户-角色表)
DROP TABLE IF EXISTS `sys_user_role`; CREATE TABLE `sys_user_role` ( `uid` int(11) DEFAULT NULL COMMENT ‘用户id‘, `role_id` int(11) DEFAULT NULL COMMENT ‘角色id‘, KEY `uid` (`uid`) USING BTREE, KEY `role_id` (`role_id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
sys_permission.sql(权限表)
DROP TABLE IF EXISTS `sys_permission`; CREATE TABLE `sys_permission` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT ‘主键‘, `parent_id` int(11) DEFAULT NULL COMMENT ‘父编号,本权限可能是该父编号权限的子权限‘, `parent_ids` varchar(20) DEFAULT NULL COMMENT ‘父编号列表‘, `permission` varchar(100) DEFAULT NULL COMMENT ‘权限字符串,menu例子:role:*,button例子:role:create,role:update,role:delete,role:view‘, `resource_type` varchar(20) DEFAULT NULL COMMENT ‘资源类型,[menu|button]‘, `url` varchar(200) DEFAULT NULL COMMENT ‘资源路径 如:/userinfo/list‘, `name` varchar(50) DEFAULT NULL COMMENT ‘权限名称‘, `available` char(1) DEFAULT ‘0‘ COMMENT ‘是否可用0可用 1不可用‘, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
sys_role_permission.sql(角色-权限表)
DROP TABLE IF EXISTS `sys_role_permission`; CREATE TABLE `sys_role_permission` ( `role_id` int(11) DEFAULT NULL COMMENT ‘角色id‘, `permission_id` int(11) DEFAULT NULL COMMENT ‘权限id‘, KEY `role_id` (`role_id`) USING BTREE, KEY `permission_id` (`permission_id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
在之前的表中添加数据
#插入用户信息表 INSERT INTO user_info(uid,username,`password`,`name`,id_card_num) VALUES (null,‘admin‘,‘123456‘,‘超哥‘,‘133333333333333333‘); INSERT INTO user_info(uid,username,`password`,`name`,id_card_num) VALUES (null,‘test‘,‘123456‘,‘孙悟空‘,‘155555555555555555‘); #插入用户角色表 INSERT INTO `sys_role` (`id`,`available`,`description`,`role`) VALUES (null,0,‘管理员‘,‘admin‘); INSERT INTO `sys_role` (`id`,`available`,`description`,`role`) VALUES (null,0,‘VIP会员‘,‘vip‘); INSERT INTO `sys_role` (`id`,`available`,`description`,`role`) VALUES (null,1,‘测试‘,‘test‘); #插入用户_角色关联表 INSERT INTO `sys_user_role` (`role_id`,`uid`) VALUES (1,1); INSERT INTO `sys_user_role` (`role_id`,`uid`) VALUES (2,2); #插入权限表 INSERT INTO `sys_permission` (`id`,`available`,`name`,`parent_id`,`parent_ids`,`permission`,`resource_type`,`url`) VALUES (null,0,‘用户管理‘,0,‘0/‘,‘userInfo:view‘,‘menu‘,‘userInfo/view‘); INSERT INTO `sys_permission` (`id`,`available`,`name`,`parent_id`,`parent_ids`,`permission`,`resource_type`,`url`) VALUES (null,0,‘用户添加‘,1,‘0/1‘,‘userInfo:add‘,‘button‘,‘userInfo/add‘); INSERT INTO `sys_permission` (`id`,`available`,`name`,`parent_id`,`parent_ids`,`permission`,`resource_type`,`url`) VALUES (null,0,‘用户删除‘,1,‘0/1‘,‘userInfo:del‘,‘button‘,‘userInfo/del‘); #插入角色_权限表 INSERT INTO `sys_role_permission` (`permission_id`,`role_id`) VALUES (1,1); INSERT INTO `sys_role_permission` (`permission_id`,`role_id`) VALUES (2,1); INSERT INTO `sys_role_permission` (`permission_id`,`role_id`) VALUES (3,2);
根据上面的sql创建实体类
public class User { private Integer uid; private String username; private String password; private String name; private String id_card_num; private String state; private Set<Role> roles = new HashSet<>(); } public class Role { private Integer id; private String role; private String description; private String available; private Set<User> users = new HashSet<>(); private Set<Permission> permissions = new HashSet<>(); } public class Permission { private Integer id; private Integer parent_id; private String parent_ids; private String permission; private String resource_type; private String url; private String name; private String available; private Set<Role> roles = new HashSet<>(); }
编写mapper
@Mapper public interface UserMapper { User findByUserName(String userName); int insert(User user); int del(@Param("username") String username); } @Mapper public interface RoleMapper { Set<Role> findRolesByUserId(@Param("uid") Integer uid); } @Mapper public interface PermissionMapper { Set<Permission> findPermissionsByRoleId(@Param("roles") Set<Role> roles); }
UserMapper.xml
<?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" > <mapper namespace="com.springboot.test.shiro.modules.user.dao.UserMapper"> <!-- 查询用户信息 --> <select id="findByUserName" resultType="com.springboot.test.shiro.modules.user.dao.entity.User"> SELECT * FROM user_info WHERE username = #{userName} </select> <!-- 添加用户 --> <!-- 创建用户 --> <insert id="insert" parameterType="com.springboot.test.shiro.modules.user.dao.entity.User"> <selectKey resultType="java.lang.Integer" keyProperty="uid" order="AFTER"> SELECT LAST_INSERT_ID() </selectKey> insert into user_info <trim prefix="(" suffix=")" suffixOverrides="," > <if test="uid != null" > uid, </if> <if test="username != null and username != ‘‘" > username, </if> <if test="password != null and password != ‘‘" > password, </if> <if test="name != null and name != ‘‘" > `name`, </if> <if test="id_card_num != null and id_card_num != ‘‘" > id_card_num, </if> <if test="state != null and state != ‘‘" > state, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="uid != null" > #{uid}, </if> <if test="username != null and username != ‘‘" > #{username}, </if> <if test="password != null and password != ‘‘" > #{password}, </if> <if test="name != null and name != ‘‘" > #{name}, </if> <if test="id_card_num != null and id_card_num != ‘‘" > #{id_card_num}, </if> <if test="state != null and state != ‘‘" > #{state}, </if> </trim> </insert> <!-- 删除用户 --> <delete id="del"> DELETE FROM user_info WHERE username = #{username} </delete> </mapper>
RoleMapper.xml
<?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" > <mapper namespace="com.springboot.test.shiro.modules.user.dao.RoleMapper"> <!-- 查询用户信息 --> <select id="findRolesByUserId" resultType="com.springboot.test.shiro.modules.user.dao.entity.Role"> SELECT r.* from sys_role r LEFT JOIN sys_user_role ur on r.id = ur.role_id where ur.uid = #{uid} </select> </mapper>
PermissionMapper.xml
<?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" > <mapper namespace="com.springboot.test.shiro.modules.user.dao.PermissionMapper"> <!-- 查询用户权限信息 --> <select id="findPermissionsByRoleId" resultType="com.springboot.test.shiro.modules.user.dao.entity.Permission"> SELECT p.* from sys_permission p LEFT JOIN sys_role_permission rp on p.id = rp.permission_id WHERE rp.role_id IN <foreach collection="roles" index="index" item="item" open="(" close=")" separator=","> #{item.id} </foreach> </select> </mapper>
以上是关于用户角色权限表的主要内容,如果未能解决你的问题,请参考以下文章