码迷,mamicode.com
首页 > 其他好文 > 详细

用户角色权限表

时间:2020-02-26 22:49:06      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:join   判断   result   hid   用户权限   hide   主键   spl   int   

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;
View Code
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;
View Code
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;
View Code
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;
View Code
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;
View Code
在之前的表中添加数据
技术图片
#插入用户信息表
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);
View Code

根据上面的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<>();
}
View Code

编写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);
}
View Code
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> 
View Code
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> 
View Code
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> 
View Code

 

用户角色权限表

标签:join   判断   result   hid   用户权限   hide   主键   spl   int   

原文地址:https://www.cnblogs.com/chong-zuo3322/p/12369430.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!