要求:
1.将数据还原为简单java类
2.数据的输出:
可以根据一个用户输出它对应的角色以及每个角色对应的权限,以及包含的具体的权限详情:
一个权限可以输出具备此权限的角色,以及具备此角色的所有管理员,同时输出该权限的所有权限详情
一个角色可以输出它所包含的管理员,每个管理员对应的具体的权限,以及权限详情
第一步:数据表转换为简单java类
class User{ private String userid; private String name; private String password; public User(String userid,String name,String password){ this.userid = userid; this.name = name; this.password; } public String getlnfo(){ return "用户ID:"+this.userid +",姓名:"+this.name +",密码:"+this.password; } } class Role{ // 角色 private int rid; private String title; public User(int rid,String title){ this.rid = rid; this.title = title; } public String getlnfo(){ return "角色编号:"+this.rid +",名称:"+this.title; } } class Group{ // 权限 private int gid; private String title; public User(int gid,String title){ this.gid = gid; this.title = title; } public String getlnfo(){ return "权限组编号:"+this.gid +",组名称:"+this.title; } } classAction{// 权限 private int aid; private String title; private String url; public User(int aid,String title,String url){ this.rid = aid; this.title = title; this.url = url; } public String getlnfo(){ return "权限编号:"+this.aid +",权限名称:"+this.title +",路径"+this.url; } } public class manyToManyMapping{ public static void main(String args[]){ } }
第二步:设置关系
一个角色包含有多个用户,一对多关系
一个权限组包含多个权限,属于一对多
一个角色对应有多个权限组,每个权限组可能有多个角色,多对多
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 setRole(Role role){ this.role = role; } public Role getRole(){ return this.role; } public String getlnfo(){ return "用户ID:"+this.userid +",姓名:"+this.name +",密码:"+this.password; } } class Role{ // 角色 private int rid; private String title; private User users []; private Group groups[]; public Role(int rid,String title){ this.rid = rid; this.title = title; } public void setGroups(Group groups[]){ this.groups = groups; } public Group[] getGroups(){ return this.groups; } public void setUser(User users[]){ this.users = users; } public User[] getUser(){ return this.users; } public String getlnfo(){ return "角色编号:"+this.rid +",名称:"+this.title; } } class Group{ // 权限组 private int gid; private String title; private Action actions[]; private Role roles[]; public Group(int gid,String title){ this.gid = gid; this.title = title; } 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 String getlnfo(){ return "权限组编号:"+this.gid +",组名称:"+this.title; } } class Action{// 权限 private int aid; private String title; private String url; private Group group; public Action(int aid,String title,String url){ this.aid = aid; this.title = title; this.url = url; } public void setGroup(Group group){ this.group = group; } public Group getGroup(){ return this.group; } public String getlnfo(){ return "权限编号:"+this.aid +",权限名称:"+this.title +",路径"+this.url; } } public class manyToManyMapping{ public static void main(String args[]){ } }
分杀完毕。迅速,一定是不需要使用脑子就直接写出来的
第三步:设置关系
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 setRole(Role role){ this.role = role; } public Role getRole(){ return this.role; } public String getlnfo(){ return "用户ID:"+this.userid +",姓名:"+this.name +",密码:"+this.password; } } class Role{ // 角色 private int rid; private String title; private User users []; private Group groups[]; public Role(int rid,String title){ this.rid = rid; this.title = title; } public void setGroups(Group groups[]){ this.groups = groups; } public Group[] getGroups(){ return this.groups; } public void setUsers(User users[]){ this.users = users; } public User[] getUsers(){ return this.users; } public String getlnfo(){ return "角色编号:"+this.rid +",名称:"+this.title; } } class Group{ // 权限组 private int gid; private String title; private Action actions[]; private Role roles[]; public Group(int gid,String title){ this.gid = gid; this.title = title; } 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 String getlnfo(){ return "权限组编号:"+this.gid +",组名称:"+this.title; } } class Action{// 权限 private int aid; private String title; private String url; private Group group; public Action(int aid,String title,String url){ this.aid = aid; this.title = title; this.url = url; } public void setGroup(Group group){ this.group = group; } public Group getGroup(){ return this.group; } public String getlnfo(){ return "权限编号:"+this.aid +",权限名称:"+this.title +",路径"+this.url; } } public class manyToManyMapping{ 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,"新闻管理","naws"); Action act2 = new Action(1,"用户管理","users"); Action act3 = new Action(1,"备份管理","bks"); Action act4 = new Action(1,"缓存管理","caches"); Action act5 = new Action(1,"数据管理","datas"); //3.定义权限组信息 Group g1 = new Group(1,"数据管理"); Group g2 = new Group(2,"人事管理"); Group g3 = new Group(3,"信息管理"); //4.定义角色信息 Role r1 = new Role(10,"超级管理员角色"); Role r2 = new Role(10,"普通管理员角色"); //5.设置权限组与权限的关系,一对多关系 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}); //6.权限组与角色关系 r1.setGroups(new Group[]{g1,g2,g3}); r1.setGroups(new Group[]{g2,g3}); g1.setRoles(new Role []{r1}); g2.setRoles(new Role []{r1,r2}); g3.setRoles(new Role []{r1,r2}); //7.定义用户与角色关系 ua.setRole(r1); ub.setRole(r2); uc.setRole(r2); r1.setUsers(new User[]{ua}); r2.setUsers(new User[]{ub,uc}); //第二步:根据要求通过关系取出数据 //可以根据一个用户输出它对应的角色以及每个角色对应的权限,以及包含的具体的权限详情: System.out.println(ua.getlnfo()); System.out.println(" |-【角色】"+ua.getRole().getlnfo()); for(int x = 0;x<ua.getRole().getGroups().length;x++){ System.out.println(" |-【权限组】"+ua.getRole().getGroups()[x].getlnfo()); for(int y = 0;y<ua.getRole().getGroups()[x].getActions().length;y++){ System.out.println(" |-【权限】"+ua.getRole().getGroups()[x].getActions()[y].getlnfo()); } } System.out.println("***************************"); //一个权限可以输出具备此权限的角色,以及具备此角色的所有管理员,同时输出该权限的所有权限详情 System.out.println(act1.getlnfo()); //通过权限找到权限对应的权限组,一个权限组有多个角色 for(int x = 0;x<act1.getGroup().getRoles().length;x++){ System.out.println(" |-【角色】 "+act1.getGroup().getRoles()[x].getlnfo()); for(int y = 0;y<act1.getGroup().getRoles()[x].getUsers().length;y++){ System.out.println(" |-【用户】 "+act1.getGroup().getRoles()[x].getUsers()[y].getlnfo()); } } System.out.println("***************************"); //一个角色可以输出它所包含的管理员,每个管理员对应的具体的权限,以及权限详情 System.out.println(r1.getlnfo()); for(int x = 0;x<r1.getUsers().length;x++){ System.out.println(" |-【用户】 "+r1.getUsers()[x].getlnfo()); for(int y = 0;y<r1.getGroups().length;y++){ System.out.println(" |-【权限组】 "+r1.getGroups()[y].getlnfo()); for(int z = 0;z<r1.getGroups()[y].getActions().length;z++){ System.out.println(" |-【权限】 "+r1.getGroups()[y].getActions()[z].getlnfo()); } } } } }
引用,引用,引用 就是java基础的核心
总结
这种转换必须做到零用脑编写