美文网首页
41-综合实战:复杂多对多映射

41-综合实战:复杂多对多映射

作者: c88bc9f9d088 | 来源:发表于2020-11-18 09:46 被阅读0次

        在进行项目实际开发过程之中,对于用户的授权管理是一项重要的任务,下面给出了一个最为常见的用户权限管理的表结构设计,基本的关系如下:

    • 一个用户可以拥有多个角色,一个角色可能有多个用户;

    • 一个角色可以拥有多个权限;

        要求实现如下查询功能:

    • 可以根据一个用户找到该用户对应的所有角色,以及每一个角色对应的所有权限信息;

    • 可以根据一个角色找到该角色下的所有权限,以及拥有此角色的全部用户信息;

    • 可以根据一个权限找到具备有此权限的所有用户信息。

    class Member{
        private String mid;
        private String name;
        private Role roles []; //多个角色
        public Member(String mid,String name){
            this.mid = mid;
            this.name = name;
        }
        // setter、getter、无参构造略
        public void setRoles(Role roles[]){
            this.roles = roles;
        }
        public Role[] getRoles(){
            return this.roles;
        }
        public String getInfo(){
            return "【用户信息】mid = " + this.mid + "、name = " + this.name;
        }
    }
    class Role{
        private long rid;
        private String title;
        private Member members[];
        private Privilege privileges[];
        public Role(long rid,String title){
            this.rid = rid;
            this.title = title;
        }
        public void setMember(Member members[]){
            this.members = members;
        }
        public void setPrivilege(Privilege privileges[]){
            this.privileges = privileges;
        }
        public Member[] getMember(){
            return this.members;
        }
        public Privilege[] getPrivileges(){
            return this.privileges;
        }
        //  setter、getter、无参构造略
        public String getInfo(){
            return "【角色信息】rid = " + this.rid + "title = " + this.title;
        }
    }
    class Privilege{
        private long pid;
        private String title;
        private Role role;
        public Privilege(long pid,String title){
            this.pid = pid;
            this.title = title;
        }
        public void setRole(Role role){
            this.role = role;
        }
        public Role getRole(){
            return this.role;
        }
        public String getInfo(){
            return "【权限信息】pid = " + this.pid + "、title = " + this.title;
        }
    }
    public class JavaDemo{
        public static void main(String args[]){
            //第一步:根据结构设置对象数据
            Member memA = new Member("mldn-a","张三");
            Member memB = new Member("mldn-b","李四");
            Role roleA = new Role(1L,"系统配置");
            Role roleB = new Role(2L,"备份管理");
            Role roleC = new Role(3L,"人事管理");
            Privilege priA = new Privilege(1000L,"系统初始化");
            Privilege priB = new Privilege(1001L,"系统还原");
            Privilege priC = new Privilege(1002L,"系统环境修改");
            Privilege priD = new Privilege(2000L,"备份员工数据");
            Privilege priE = new Privilege(2001L,"备份部门数据");
            Privilege priF = new Privilege(2002L,"备份公文数据");
            Privilege priG = new Privilege(3000L,"增加员工");
            Privilege priH = new Privilege(3001L,"编辑员工");
            Privilege priI = new Privilege(3002L,"浏览员工");
            Privilege priJ = new Privilege(3003L,"员工离职");
            //增加角色与权限的对应关系
            roleA.setPrivilege(new Privilege[]{priA,priB,priC});
            roleB.setPrivilege(new Privilege[]{priD,priE,priF});
            roleC.setPrivilege(new Privilege[]{priG,priH,priI,priJ});
            // 增加权限与角色对应
            priA.setRole(roleA);
            priB.setRole(roleA);
            priC.setRole(roleA);
            priD.setRole(roleB);
            priE.setRole(roleB);
            priF.setRole(roleB);
            priG.setRole(roleC);
            priH.setRole(roleC);
            priI.setRole(roleC);
            priJ.setRole(roleC);
            // 增加用户与角色的对应关系
            memA.setRoles(new Role[] {roleA,roleB});
            memB.setRoles(new Role[] {roleA,roleB,roleC});
            roleA.setMember(new Member[]{memA,memB});
            roleB.setMember(new Member[]{memA,memB});
            roleC.setMember(new Member[]{memB});
            //第二步:根据要求获取数据
            System.out.println("------ 通过用户查找信息 ------");
            System.out.println(memB.getInfo());
            for(int x = 0; x < memB.getRoles().length; x++){
                System.out.println("\t|- " + memB.getRoles()[x].getInfo());
                for(int y = 0; y < memB.getRoles()[x].getPrivileges().length; y++){
                    System.out.println("\t\t|- " + memB.getRoles()[x].getPrivileges()[y].getInfo());
                }
            }
            System.out.println("------ 通过角色查找信息 ------");
            System.out.println(roleB.getInfo());
            System.out.println("\t|- 浏览此角色下的所有权限信息:");
            for(int x = 0; x < roleB.getPrivileges().length; x++){
                System.out.println("\t\t|- " + roleB.getPrivileges()[x].getInfo());
            }
            System.out.println("\t|- 浏览此角色下的所有用户信息:");
            for(int x = 0 ; x < roleB.getMember().length;x++){
                System.out.println("\t\t|- " + roleB.getMember()[x].getInfo());
            }
            System.out.println("------ 通过权限查找信息 ------");
            System.out.println(priA.getInfo());
            for(int x = 0; x < priA.getRole().getMember().length; x++){
                System.out.println("\t|- " + priA.getRole().getMember()[x].getInfo());
            }
        }   
    }
    

    相关文章

      网友评论

          本文标题:41-综合实战:复杂多对多映射

          本文链接:https://www.haomeiwen.com/subject/mwbqiktx.html