Java —— 多对多映射综合

来源:互联网 发布:zeroclipboard.js下载 编辑:程序博客网 时间:2024/06/11 00:13


所谓多对多就是就一堆类而言,这一个类中有不止一个其他类中的引用,而其他类中又会存在其他类的不止一个对象的引用关系。。

以下是“网易云课堂”上讲解多对多映射关系中的代码。。


管理员,角色,权限组,权限。

其实权限组就是一类权限的总称,而还存在一个角色—权限组,这种由两个类相结合的一般需要通过两个类中的引用关系数组来进行实现。


class Admin {private String aid;private String password;private Role role;public Admin(String aid,String password) {this.aid = aid;this.password = password;}public void setRole(Role role) {this.role = role;}public Role getRole() {return this.role;}public String getInfo() {return "管理员id:"+this.aid+". 密码: "+this.password;}}class Role {private int rid;private String title;private Admin admins [];private Group groups [];public Role(int rid,String title) {this.rid = rid;this.title = title;}public void setAdmins(Admin admins []) {this.admins = admins;}public Admin [] getAdmins() {return this.admins;}public void setGroups(Group groups []) {this.groups = groups;}  public Group [] getGroups() {return this.groups;}public String getInfo() {return "角色id:"+this.rid+". 标题:"+this.title;}}class Group {private int gid;private String title;private Role roles [];private Action actions [];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 getInfo() {return "权限组id:"+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 getInfo() {return "权限id:"+this.aid+". 标题:"+this.title+". 路径:"+this.url;}}public class Demo {public static void main(String []args) {// 设置关系数据// 1.准备出若干个对象Admin a1 = new Admin("admin","hello");Admin a2 = new Admin("Sxtopc","hello");Admin a3 = new Admin("ayou","hello");Role r1 = new Role(1,"系统管理员");Role r2 = new Role(2,"信息管理员");Group g1 = new Group(10,"信息管理");Group g2 = new Group(11,"用户管理");Group g3 = new Group(12,"数据管理");Group g4 = new Group(13,"接口管理");Group g5 = new Group(14,"备份管理");Action at1 = new Action(1001,"新闻发布","-");Action at2 = new Action(1002,"新闻列表","-");Action at3 = new Action(1003,"新闻审核","-");Action at4 = new Action(1004,"增加用户","-");Action at5 = new Action(1005,"用户列表","-");Action at6 = new Action(1006,"登录日志","-");Action at7 = new Action(1007,"雇员数据","-");Action at8 = new Action(1008,"部门数据","-");Action at9 = new Action(1009,"公司数据","-");Action at10 = new Action(1010,"服务传输","-");Action at11 = new Action(1011,"短信平台","-");Action at12 = new Action(1012,"全局备份","-");Action at13 = new Action(1013,"局部备份","-");// 2.设置对象间的基本引用关系// 设置管理员与角色的关系a1.setRole(r1);a2.setRole(r2);a3.setRole(r2);r1.setAdmins(new Admin[] {a1});r2.setAdmins(new Admin[] {a2,a3});// 设置角色与管理员组r1.setGroups(new Group[] {g1,g2,g3,g4,g5});r2.setGroups(new Group[] {g1,g2});g1.setRoles(new Role[] {r1,r2});g2.setRoles(new Role[] {r1,r2});g3.setRoles(new Role[] {r1});g4.setRoles(new Role[] {r1});g5.setRoles(new Role[] {r1});// 设置管理员组和权限的关系g1.setActions(new Action[] {at1,at2,at3});g2.setActions(new Action[] {at4,at5,at6});g3.setActions(new Action[] {at7,at8,at9});g4.setActions(new Action[] {at10,at11});g5.setActions(new Action[] {at12,at13});at1.setGroup(g1);at2.setGroup(g1);at3.setGroup(g1);at4.setGroup(g2);at5.setGroup(g2);at6.setGroup(g2);at7.setGroup(g3);at8.setGroup(g3);at9.setGroup(g3);at10.setGroup(g4);at11.setGroup(g4);at12.setGroup(g5);at13.setGroup(g5);// 第二步:取出数据System.out.println(a1.getInfo()) ;System.out.println("-----------------------------------") ;System.out.println("\t|- "+a1.getRole().getInfo());for(int i = 0; i<a1.getRole().getGroups().length; i++) {System.out.println("\t\t|- "+ a1.getRole().getGroups()[i].getInfo());for(int j = 0; j<a1.getRole().getGroups() [i].getActions().length; j++) {System.out.println("\t\t\t|- "+a1.getRole().getGroups() [i].getActions()[j].getInfo()) ;}}System.out.println("-----------------------------------") ;System.out.println(g2.getInfo()) ;for(int i = 0; i<g2.getRoles().length; i++) {System.out.println("\t|- "+g2.getRoles()[i].getInfo());for(int j = 0; j<g2.getRoles() [i].getAdmins().length; j++) {System.out.println("\t\t|- "+g2.getRoles() [i].getAdmins() [j].getInfo());}}System.out.println("-----------------------------------") ;}}





0 0
原创粉丝点击