class User{ private String userid; private String name; private String password; private Role role; public User(String userid,String name,String password){ this.userid=userid; this.name=name; this.password=password; } public void setRoles( Role role){ this.role=role; } public Role getRole(){ return this.role; } public String getInfo(){ return"用户ID:"+this.userid+",姓名:"+this.name+",密码:"+this.password; } } class Role{//角色 private int rid; private String title; private User users[]; private Group groups[]; public void setGroups(Group groups[]){ this.groups=groups; } public Group[] getGroups(){ return this.groups; } public Role(int rid,String title){ this.rid=rid; this.title=title; } public void setUsers(User users[] ){ this.users=users; } public User[] getUsers(){ return this.users; } public String getInfo(){ return"角色编号:"+rid+",名称:"+title; } } class Group{//权限组 private int gid; private String title; private Action actions[]; private Role roles[]; public void setRoles(Role roles[]){ this.roles=roles; } public Role[] getRoles(){ return this.roles; } public void setActions(Action actions[]){ this.actions=actions; } public Action[] getActions(){ return this.actions; } public Group(int gid,String title){ this.gid=gid; this.title=title; } public String getInfo(){ return "权限组ID:"+gid+",权限组名称:"+title; } } class Action{//权限 private int aid; private String title; private String url; private Group group; public void setGroup(Group group){ this.group=group; } public Group getGroup(){ return this.group; } public Action(int aid,String title,String url){ this.aid=aid; this.title=title; this.url=url; } public String getInfo(){ return"权限编号:"+aid+",名称:"+title+",路径:"+url; } } public class LianXi { public static void main(String[]args){ //1定义单独类对象 User ua=new User("user-a","用户A","hello"); User ub=new User("user-b","用户B","hello"); User uc=new User("user-c","用户C","hello"); //2定义权限对象 Action act1=new Action(1,"新闻管理","http://www.zhaoshaiya1.com"); Action act2=new Action(2,"资金管理","http://www.zhaoshaiya2.com"); Action act3=new Action(3,"备份管理","http://www.zhaoshaiya3.com"); Action act4=new Action(4,"缓存管理","http://www.zhaoshaiya4.com"); Action act5=new Action(5,"数据管理","http://www.zhaoshaiya5.com"); //定义权限组信息 Group g1=new Group(1,"数据管理"); Group g2=new Group(2,"人事管理"); Group g3=new Group(3,"信息管理"); //定义角色信息 Role r1=new Role(10,"超级管理员"); Role r2=new Role(20,"普通管理员"); //定义权限组与权限的关系,一对多关系 act1.setGroup(g1); act2.setGroup(g1); act3.setGroup(g2); act4.setGroup(g2); act5.setGroup(g3); g1.setActions(new Action[]{act1,act2}); g2.setActions(new Action[]{act3,act4}); g3.setActions(new Action[]{act5}); //权限组与角色关系,多对多 r1.setGroups(new Group[]{g1,g2,g3}); r2.setGroups(new Group[]{g2,g3}); g1.setRoles(new Role[]{r1}); g2.setRoles(new Role[]{r1,r2}); g3.setRoles(new Role[]{r1,r2}); //定义用户与角色的关系 ua.setRoles(r1); ub.setRoles(r2); uc.setRoles(r2); r1.setUsers(new User[]{ua}); r2.setUsers(new User[]{ub,uc}); //第二步取出数据 System.out.println(ua.getInfo()); System.out.println(" |-[角色]"+ua.getRole().getInfo()); for(int x=0;x<ua.getRole().getGroups().length;x++){ System.out.println(" |-[权限组]"+ua.getRole().getGroups()[x].getInfo()); for(int y=0;y<ua.getRole().getGroups()[x].getActions().length;y++){ System.out.println(" |-[权限]"+ua.getRole().getGroups()[x].getActions()[y].getInfo()); } } System.out.println(" =========================================== "); //一个权限可以输出具备此权限的角色,以及具备此角色的管理员;同时输出该权限的所有权限详情; System.out.println("[权限]"+act2.getInfo()); for(int x=0;x<act2.getGroup().getRoles().length;x++){ System.out.println(" |-[角色]"+act2.getGroup().getRoles()[x].getInfo()); for(int y=0;y<act2.getGroup().getRoles()[x].getUsers().length;y++){ System.out.println(" [用户]"+act2.getGroup().getRoles()[x].getUsers()[y].getInfo()); } } } }