一对多映射

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("\t" + 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("\t-->" + item.getSubitems()[x] .getInfo()) ;
}
System.out.println("----------------------------------------------") ;
System.out.println(item.getInfo()) ;
for ( int x = 0 ; x < item.getSubitems().length ; x++ ) { //根据总类 显示子类型
System.out.println("\t-->" + item.getSubitems()[x] .getInfo()) ; //子类型
for ( int y = 0 ; y < item.getSubitems()[x].getProducts().length ; y++ ) { //根据子类型,查看子类型下的商品
System.out.println("\t\t-->" + 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 + "\t 密码:" + 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("\t" + a1.getRoles().getInfo()); //根据挂管理员找到一个角色
for ( int x = 0 ; x < a1.getRoles().getGroups().length ; x++ ) {//根据角色找到权限组
System.out.println("\t \t" + a1.getRoles().getGroups()[x].getInfo()) ;
for ( int y = 0 ; y < a1.getRoles().getGroups()[x].getActions().length ; y++ ) {//根据权限组找权限
System.out.println("\t \t \t" + 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("\t" + g2.getRoles()[x].getInfo()) ;
for ( int y = 0 ; y < g2.getRoles()[x].getAdmins().length ; y++ ) {
System.out.println("\t\t" + g1.getRoles()[x].getAdmins()[y].getInfo()) ;
}
}
}
}

管理权限映射

Java 数据表映射的更多相关文章

  1. IT忍者神龟之Hibernat持久化对象-数据表映射配置回想

    1.持久化对象POJO编写规则: 1) 有空參public构造器: 2) 提供标识属性.映射数据表主键: 3) 属性提供setter和getter方法. 4) 属性使用基本数据类型的包装类型.基本类型 ...

  2. EF4.1 企业架构模式 自动映射数据表(转载)

    在讲解之前,先来看看解决方案的架构: 1.在Nop.Core下的Domain里建立一个实体Category:2.在Nop.Data下的Mapping\Catatog\下建立一个数据表映射Categor ...

  3. 以对象的方式来访问xml数据表(三)

    怎样以对象的方式来访问xml数据表? 在讲如何具体实现(二)中所说的专门用于访问xml文件的动态链接库之前,我们先来看看这个动态链接库具体要实现什么功能. 动态链接库IXmlDB.dll的功能: 1. ...

  4. ThinkPHP 学习笔记 ( 三 ) 数据库操作之数据表模型和基础模型 ( Model )

    //TP 恶补ing... 一.定义数据表模型 1.模型映射 要测试数据库是否正常连接,最直接的办法就是在当前控制器中实例化数据表,然后使用 dump 函数输出,查看数据库的链接状态.代码: publ ...

  5. ThinkPHP 数据库操作之数据表模型和基础模型 ( Model )

    一.定义数据表模型 1.模型映射 要测试数据库是否正常连接,最直接的办法就是在当前控制器中实例化数据表,然后使用 dump 函数输出,查看数据库的链接状态.代码: public function te ...

  6. 数据表-java类的映射

    1.一个数据表对应一个java类 2.数据表的字段对应java类的属性 3.一对多的数据表关系 一方用一个java对象表示 多方用一个java对象数组表示 4.多对多的数据表关系:采用中间表,将多对多 ...

  7. 实体类和数据表的映射异常(XXX is not mapping[ ])

    在使用SSH框架开发过程,使用hibernate框架提供的工具类实现与数据库数据交互,在执行cmd操作时,如果出现以下异常: org.hibernate.hql.ast.QuerySyntaxExce ...

  8. 数据表与简单java类映射转换

    简单的Java类的定义来源于数据表的结构, 例如:雇员信息表.部门信息表描述的就是雇员或部门的信息, 在实际的开发之中,数据表和简单java类之间的映射关系如下: 1. 数据实体表设计 = 类的定义: ...

  9. java基础复习-自定义注解4(结合JDBC技术,打造类表映射微框架)

    写在前面: 1.该框架为自己所写的第一个框架类产品,可能有着许多不足的地方,读者可以到评论区指出.同时,该微框架的源码也会开源至博客中,够后来的学习者借鉴.由于该框架逻辑结构稍些复杂,不可能花大量篇幅 ...

随机推荐

  1. select,poll,epoll用法

    http://blog.csdn.net/sunboy_2050/article/details/6126712 select用法 #include <sys/time.h>       ...

  2. socket API详解

    send函数 int send( SOCKET s,   const char FAR *buf,   int len,   int flags ); 不论是客户还是服务器应用程序都用send函数来向 ...

  3. 【转】java中定义二维数组的几种写法

    原文链接 注:以下的 type[][] var 也可以这样申明 type var[][] type为数组的类型,var为变量名 写法一:行列固定的数组 //定义二维数组写法1 class Test { ...

  4. 20、Semantic-UI之数据验证

    20.1 实现数据验证   在很多前端框架中都提供了数据验证的操作,比如jQuery的验证框架等,但是jQuery的验证框架js文件太多:在使用Semantic-UI框架的时候只需要导入semanti ...

  5. Linq转换操作之OfType,Cast,AsEnumerable,ToLookup源码分析

    Linq转换操作之OfType,Cast,AsEnumerable,ToLookup源码分析 一:Tolookup 1. 从方法的注解上可以看到,ToLookup也是一个k,v的形式,那么问题来了,它 ...

  6. Arduino I2C + 三轴加速度计ADXL345

    ADXL345是ADI公司生产的三轴数字加速度计芯片,与ST的LIS3DH功能接近.主要特性有: 工作电压:2.0 ~ 3.6V 功耗:待机功耗0.1μA:工作时与数据输出频率(ODR)有关,如ODR ...

  7. 转载C#中Trim()、TrimStart()、TrimEnd()的用法

    C#中Trim().TrimStart().TrimEnd()的用法:    这三个方法用于删除字符串头尾出现的某些字符.Trim()删除字符串头部及尾部出现的空格,删除的过程为从外到内,直到碰到一个 ...

  8. 如何使用jQuery + css3制作绚丽的网页经验总结

    常见的网页特效有:轮播,滚动,闪烁,渐变,图标GIF效果,翻转,折叠,3D变换,主视觉等.以前没有CSS3时一些复杂的特效都要借助Flash来实现,Flash为什么会被淘汰,个人认为有以下几点: 1. ...

  9. ftp服务器PDF文件在线查看

    曾做过电厂的项目,有一些功能需要和甲方的厂家对接,其中就有需要实现甲方ftp服务器上的PDF.JPG等文件的查看功能.就PDF文件为例,这里使用的是pdf插件,需要将参数通过链接发给ftp,获取到PD ...

  10. go iris xorm包使用(sqlite3数据库增删查改)

    官网https://studyiris.com/example/orm/xorm.html例子,稍做修改 1.我是win64,但没有遇到mingw问题,应该是之前安装过gcc环境,参考:测试一下rob ...