一对多映射
class Province { //每一个类就相当于数据库中的一个表; private int pid ; private String name ; private City cities [] ; //一对多 //setter getter 无参构造 略~ public Province(int pid , String name) { this.pid = pid ; this.name = name ; } public void setCities(City cities[]) { this.cities = cities ; } public City[] getCities() { return this.cities ; } public String getInfo() { return "省份编号:" + this.pid + ", 名称:" + this.name ; } } class City { private int cid ; private String name ; private Province province ; //省份对象元素 public City(int cid , String name) { this.cid = cid ; this.name = name ; } public void setProvince(Province province) { this.province = province ; } public Province getProvince() { return this.province; } public String getInfo() { return "城市编号:" + this.cid + ", 名称:" + this.name ; } } /* 每一个实例化的对象都是单独的个体的存在,占用的是独立的内存空间 所以每一个实例对象的操作不影响其它实例对象或者类的数据 */ public class TestPC { public static void main(String args[]) { // 设置关系数据 Province pro = new Province(1,"江苏省") ; // 声明Province类对象 City c1 = new City(1001,"南京市") ; City c2 = new City(1002,"苏州市") ; City c3 = new City(1003,"宿迁市") ; // 什么多个City类对象 //设置关系 c1.setProvince(pro) ; // 利用City实例对象c1调用setProvince()方法并将pro对象传递 c2.setProvince(pro) ; // 这就是所谓的 "引用传递" c3.setProvince(pro) ; pro.setCities(new City[] {c1,c2,c3}) ; // 调用setCities方法,传入的是数组 // System.out.println(c2.getProvince().getInfo()) ; for ( int x = 0 ; x < pro.getCities().length ; x++ ) { System.out.println(" " + pro.getCities()[x].getInfo()) ; } } }
一对多对多映射
class Item { // 父栏目 private int iid ; private String name ; private String note ; //设置简单的表和表(类-类)的关联 private Subitem subitems [] ; // 一对多 private Product products [] ; // 一对多 //构建简答Java类-构造 public Item(int iid , String name , String note) { this.iid = iid ; this.name = name ; this.note = note ; } public void setSubitems(Subitem subitems[]) { this.subitems = subitems ; } public Subitem [] getSubitems() { return this.subitems ; } public void setProducts(Product products[]) { this.products = products ; } public Product [] getProducts() { return this.products ; } public String getInfo() { return "栏目名称:" + this.iid + ", 名称:" + this.name + ", 描述:" + this.note ; } } class Subitem { // 子栏目 private int sid ; private String name ; private String note ; private Item item ; private Product products [] ; //存放的是Product类的实例对象元素 public Subitem(int sid , String name , String note) { this.sid = sid ; this.name = name ; this.note = note ; } public void setItem(Item item){ this.item = item ; } public void setProducts(Product products []) { this.products = products ; } public Item getItem() { return this.item ; } public Product [] getProducts() { return this.products ; } public String getInfo() { return "子栏目编号:" + this.sid + ",名称:" + this.name + ", 描述:" + this.note ; } } class Product { // 商品 private int pid ; private String name ; private double price ; private Item item ; private Subitem subitems ; public Product(int pid , String name , double price) { this.pid = pid ; this.name = name ; this.price = price ; } public void setItem(Item item) { this.item = item ; } public Item getItem() { return item ; } public void setSubitems(Subitem Subitems) { this.subitems = subitems ; } public Subitem getSubitems(Subitem subitems) { return this.subitems ; } public String getInfo() { return "商品编号:" + this.pid + ", 名称:" + this.name + ", 价格" + this.price ; } } public class TestISP { public static void main(String args[]) { // 第一步;设置数据 // 设置单独的类实例对象 Item item = new Item (1,"图书","---") ; // 总类 Subitem suba = new Subitem(1001,"科技类","--") ;// 二分类 Subitem subb = new Subitem(1002,"文学类","--") ; Subitem subc = new Subitem(1003,"图画类","--") ; Product proa = new Product(1001001,"物种起源",98.9) ; //商品 Product prob = new Product(1001002,"宇宙探索",120.0) ; Product proc = new Product(1001003,"魔法奥秘",29.9) ; Product prod = new Product(1002001,"知识",19.9) ; Product proe = new Product(1002002,"道德经",89.8) ; Product prof = new Product(1003001,"365夜故事",9.9) ; Product prog = new Product(1003002,"童话公主",9.9) ; //设置引用关系 suba.setItem(item) ; // 设置Subitem类的多对一的属性 subb.setItem(item) ; subc.setItem(item) ; proa.setItem(item) ; prob.setItem(item) ; proc.setItem(item) ; prod.setItem(item) ; proe.setItem(item) ; prof.setItem(item) ; prog.setItem(item) ; proa.setSubitems(suba) ; prob.setSubitems(suba) ; proc.setSubitems(suba) ; prod.setSubitems(subb) ; proe.setSubitems(subb) ; prof.setSubitems(subc) ; prog.setSubitems(subc) ; suba.setProducts(new Product[] {proa,prob,proc} ) ; // 一个分类对应多个商品 subb.setProducts(new Product[] {prod,proe}) ; subc.setProducts(new Product[] {prof,prog}) ; item.setSubitems(new Subitem[] {suba,subb,subc}) ; //一个总类对应多个分类 item.setProducts(new Product[] {proa,prob,proc,prod,proe,prof,prog}) ; //一个总类对应多个商品 //取出数据 //通过一个类型,找到对应的全部子类型 System.out.println(item.getInfo()) ; //显示总类 for ( int x = 0 ; x < item.getSubitems().length ; x++ ) { System.out.println(" -->" + item.getSubitems()[x] .getInfo()) ; } System.out.println("----------------------------------------------") ; System.out.println(item.getInfo()) ; for ( int x = 0 ; x < item.getSubitems().length ; x++ ) { //根据总类 显示子类型 System.out.println(" -->" + item.getSubitems()[x] .getInfo()) ; //子类型 for ( int y = 0 ; y < item.getSubitems()[x].getProducts().length ; y++ ) { //根据子类型,查看子类型下的商品 System.out.println(" -->" + item.getSubitems()[x].getProducts()[y].getInfo()) ; //商品 } } } } /* 程序中,定义的类属性成员的目的是,再调用成员时候,进行的是对象的引用传递 */
多对多映射
class Admin { private String aid ; private String password ; private Role roles ; public Admin(String aid , String password ) { this.aid = aid ; this.password = password ; } public void setRoles(Role roles) { this.roles = roles ; } public Role getRoles() { return roles ; } 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 admins ; } public void setGroups (Group [] groups) { this.groups = groups ; } public Group [] getGroups () { return groups ; } public String getInfo() { return "角色ID:" + this.rid + ",角色名称:" + this.title ; } } class Group { private int gid ; private String title ; private Role roles [] ; //一个Group对应多个Role private Action actions [] ; //一个Group对应多个Action public Group(int gid , String title ) { this.gid = gid ; this.title = title ; } public void setRoles(Role [] roles) { this.roles = roles ; } public Role [] getRoles() { return roles ; } public void setActions (Action [] actions) { this.actions = actions ; } public Action [] getActions () { return actions ; } public String getInfo() { return "权限组ID:" + this.gid + ",权限组名称:" + this.title ; } } class Action { private int aid ; private String title ; private String url ; private Group groups ; //一个权限对应一个权限组 public Action (int aid , String title , String url) { this.aid = aid ; this.title = title ; this.url = url ; } public void setGroups(Group groups) { this.groups = groups ; } public Group getGroups () { return groups ; } public String getInfo() { return "权限ID:" + this.aid + ",权限名称:" + this.title + ",路径:" + this.url ; } } //测试 public class TestAdmin { public static void main(String args[]) { //1 设置完整的映射关系 // 实例化类对象 Admin a1 = new Admin("admin" , "hello") ; Admin a2 = new Admin("mldn" , "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 ac1 = new Action(1001,"新闻发布","--") ; Action ac2 = new Action(1002,"新闻列表","--") ; Action ac3 = new Action(1003,"新闻审核","--") ; Action ac4 = new Action(1004,"增加用户","--") ; Action ac5 = new Action(1005,"用户列表","--") ; Action ac6 = new Action(1006,"登录日志","--") ; Action ac7 = new Action(1007,"雇员数据","--") ; Action ac8 = new Action(1008,"部门数据","--") ; Action ac9 = new Action(1009,"公司数据","--") ; Action ac10 = new Action(1010,"服务传输","--") ; Action ac11 = new Action(1011,"短信平台","--") ; Action ac12 = new Action(1012,"全部备份","--") ; Action ac13 = new Action(10013,"局部备份","--") ; //设置管理员和角色的关系(Admin <> Role) a1.setRoles(r1) ; a2.setRoles(r2) ; a3.setRoles(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[] {ac1,ac2,ac3}); g2.setActions(new Action[] {ac4,ac5,ac6}); g3.setActions(new Action[] {ac7,ac8,ac9}); g4.setActions(new Action[] {ac10,ac11}); g5.setActions(new Action[] {ac12,ac13}); ac1.setGroups(g1) ; ac2.setGroups(g1) ; ac3.setGroups(g1) ; ac4.setGroups(g2) ; ac5.setGroups(g2) ; ac6.setGroups(g2) ; ac7.setGroups(g3) ; ac8.setGroups(g3) ; ac9.setGroups(g3) ; ac10.setGroups(g4) ; ac11.setGroups(g4) ; ac12.setGroups(g5) ; ac13.setGroups(g5) ; //数据读取 System.out.println("------------------------------------") ; System.out.println(a1.getInfo()); //找到一个管理员 System.out.println(" " + a1.getRoles().getInfo()); //根据挂管理员找到一个角色 for ( int x = 0 ; x < a1.getRoles().getGroups().length ; x++ ) {//根据角色找到权限组 System.out.println(" " + a1.getRoles().getGroups()[x].getInfo()) ; for ( int y = 0 ; y < a1.getRoles().getGroups()[x].getActions().length ; y++ ) {//根据权限组找权限 System.out.println(" " + a1.getRoles().getGroups()[x].getActions()[y].getInfo()) ; } } System.out.println("------------------------------------") ; System.out.println(g2.getInfo()) ; for ( int x = 0 ; x < g2.getRoles().length ; x++ ) { System.out.println(" " + g2.getRoles()[x].getInfo()) ; for ( int y = 0 ; y < g2.getRoles()[x].getAdmins().length ; y++ ) { System.out.println(" " + g1.getRoles()[x].getAdmins()[y].getInfo()) ; } } } }