数据表与简单Java类映射转换
- 简单Java类是现在面向对象设计的主要分析基础,但是对于实际的开发之中简单Java类的定义来源是有依据的,往往都是根据数据表的结构来实现简单Java类。
- 在数据库之中实际上是提供有若干个数据表的,那么每一张实体数据表都可以描述出一些具体的事物概念。
- 程序类的定义形式实际上和这些实体表的差别并不大,所以在实际的项目开发之中数据表与简单Java类的基本映射关系如下:
- 数据实体表设计 = 类的定义;
- 表中的字段 = 类的成员属性;
- 表的外键关联 = 引用关联;
- 表的一行记录 = 类的一个实例化对象 ;
- 表的多行数据 = 对象数组
- 上图中对应数据表关系:
- 一个部门有多个雇员
- 一个雇员属于一个部门
- 一个雇员有一个领导
/*
1. 根据部门信息获得以下内容:
|- 一个部门的完整信息
|- 一个部门所有雇员的的完整信息
|- 一个雇员对应的领导信息
2. 根据雇员信息获得以下内容:
|- 一个雇员所在的部门信息
|- 一个雇员对应的领导信息
对于数据表与简单Java类之间的映射解决步骤:先抛开所有的关联字段不看,写出类的基本组成,而后再通过引用配置关联字段的关系
第一步:分别定义Emp、Dept两个实体类
第二步:配置所有关联字段
**/
/*
第一步:根据表的结构关系进行对象配置
第二步:根据要求通过结构获取数据
**/
class Dept {
private long deptno;
private String dname;
private String loc;
private Emp emps []; //多个雇员信息
public Dept() {}
public Dept(long deptno, String dname, String loc) {
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public String getInfo() {
return "【部门信息】部门编号:" + this.deptno + "、【部门信息】部门名称:" + this.dname + "、【部门信息】部门位置:" + this.loc;
}
public void setEmps(Emp [] emps) {
this.emps = emps;
}
public Emp [] getEmps() {
return this.emps;
}
public void setDeptno(long deptno) {
this.deptno = deptno;
}
public void setDname(String dname) {
this.dname = dname;
}
public void setLoc(String loc) {
this.loc = loc;
}
public long getDeptno() {
return this.deptno;
}
public String getDname() {
return this.dname;
}
public String getLoc() {
return this.loc;
}
}
class Emp {
private long empno;
private String ename;
private String job;
private double sal;
private double comm;
private Dept dept; //所属部门
private Emp mgr; //所属领导
public Emp() {}
public Emp(long empno, String ename, String job, double sal, double comm)
{
this.empno = empno;
this.ename = ename;
this.job = job;
this.sal = sal;
this.comm = comm;
}
public String getInfo() {
return "【雇员信息】雇员编号:" + this.empno + "、【雇员信息】雇员姓名:" + this.ename + "、【雇员信息】雇员职位:" + this.job + "、【雇员信息】雇员工资:" + this.sal + "、【雇员信息】雇员佣金:" + this.comm;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public void setMgr(Emp mgr) {
this.mgr = mgr;
}
public Dept getDept() {
return this.dept;
}
public Emp getMgr() {
return this.mgr;
}
public void setEmpne(long empno) {
this.empno = empno;
}
public void setEname(String ename) {
this.ename = ename;
}
public void setJob(String job) {
this.job = job;
}
public void setSal(double sal) {
this.sal = sal;
}
public void setComm(double comm) {
this.comm = comm;
}
public long getEmpno() {
return this.empno;
}
public String getEname() {
return this.ename;
}
public String getJob() {
return this.job;
}
public double getSal() {
return this.sal;
}
public double getComm() {
return this.comm;
}
}
public class JavaStudy {
public static void main(String [] args) {
//根据关系进行类的定义
Dept dept = new Dept(10L, "财务部", "上海");
Emp empA = new Emp(7369L, "SMITH", "CLERK", 800.00, 0.0);
Emp empB = new Emp(7566L, "FORD", "MANAGER", 2450.00, 0.0);
Emp empC = new Emp(7839L, "KING", "PRESIDENT", 5000.00, 0.0);
//需要为对象进行关联设置
empA.setDept(dept);
empB.setDept(dept);
empC.setDept(dept);
empA.setMgr(empB);
empB.setMgr(empC);
dept.setEmps(new Emp [] {empA, empB, empC});
//根据关系获取数据
System.out.println(dept.getInfo());
for(int x = 0; x < dept.getEmps().length; x++) {
System.out.println("\t|-" + dept.getEmps() [x].getInfo());
if(dept.getEmps() [x].getMgr() != null) {
System.out.println("\t\t|-" + dept.getEmps() [x].getMgr().getInfo());
}
}
System.out.println("-------------------------");
System.out.println(empA.getDept().getInfo());
System.out.println(empA.getMgr().getInfo());
System.out.println(empB.getDept().getInfo());
System.out.println(empB.getMgr().getInfo());
System.out.println(empC.getDept().getInfo());
}
}
数据表与简单Java类(一对多)
- 按照表的要求将表的结构转换为类结构,同时可以获取如下信息:
- 获取一个分类的完整信息;
- 可以根据分类获取其对应的所有子分类的信息;
class Item {
private long iid;
private String title;
private Subitem subitems [];
public Item() {}
public Item(long iid, String title) {
this.iid = iid;
this.title = title;
}
public String getInfo() {
return "【分类信息】iid=" + this.iid + "、title=" + this.title;
}
public void setSubitems(Subitem subitems []) {
this.subitems = subitems;
}
public void setIid(long iid) {
this.iid = iid;
}
public void setTitle(String title) {
this.title = title;
}
public Subitem [] getSubitems() {
return this.subitems;
}
public long getIid() {
return this.iid;
}
public String getTitle() {
return this.title;
}
}
class Subitem {
private long sid;
private String title;
private Item item;
public Subitem() {}
public Subitem(long sid, String title) {
this.sid = sid;
this.title = title;
}
public String getInfo() {
return "【子分类信息】sid=" + this.sid + "、title=" + this.title;
}
public void setItem(Item item) {
this.item = item;
}
public void setSid(long sid) {
this.sid = sid;
}
public void setTitle(String title) {
this.title = title;
}
public Item getItem() {
return this.item;
}
public long getSid() {
return this.sid;
}
public String getTitle() {
return this.title;
}
}
public class JavaStudy {
public static void main(String [] args) {
//根据结构设置对象数据
Item item = new Item(1L, "图书分类");
Subitem subitems [] = new Subitem [] {
new Subitem(10L, "编程类图书"),
new Subitem(10L, "图形图像类图书")
};
item.setSubitems(subitems);
for(int x = 0; x < subitems.length; x++) {
subitems[x].setItem(item);
}
//根据要求获取数据
System.out.println(item.getInfo());
for(int x = 0; x < item.getSubitems().length; x++) {
System.out.println("\t|-" + item.getSubitems() [x].getInfo());
}
}
}
数据表与简单Java类(多对多)
- 将以上的结构转换为类结构,并且可以获取如下信息:
- 获取一个用户访问所有商品的详细信息;
- 获取一个商品被浏览过的全部用户信息;
class Member {
private String mid;
private String name;
private Product products [];
public Member() {}
public Member(String mid, String name) {
this.mid = mid;
this.name = name;
}
public String getInfo() {
return "【用户信息】mid:" + this.mid + "、name:" + this.name;
}
public void setProducts(Product products []) {
this.products = products;
}
public void setMid(String mid) {
this.mid = mid;
}
public void setName(String name) {
this.name = name;
}
public Product [] getProducts() {
return this.products;
}
public String getMid() {
return this.mid;
}
public String getName(String name) {
return this.name;
}
}
class Product {
private long pid;
private String title;
private double price;
private Member members [];
public Product() {}
public Product(long pid, String title, double price) {
this.pid = pid;
this.title = title;
this.price = price;
}
public String getInfo() {
return "【商品信息】pid:" + this.pid + "、商品名称:" + this.title + "、商品价格:" + this.price;
}
public void setMembers(Member members []) {
this.members = members;
}
public void setPid(long pid) {
this.pid = pid;
}
public void setTitle(String title) {
this.title = title;
}
public void setPrice(double price) {
this.price = price;
}
public Member [] getMembers() {
return this.members;
}
public long getPid() {
return this.pid;
}
public String getTitle() {
return this.title;
}
public double getPrice() {
return this.price;
}
}
public class JavaStudy {
public static void main(String [] args) {
//根据结构设置对象数据
Member memberA = new Member("javascript", "张三");
Member memberB = new Member("java", "李四");
Product productA = new Product(1L, "Java放弃", 99.9);
Product productB = new Product(2L, "Java从入门到放弃", 89.8);
Product productC = new Product(3L, "Java从菜鸟到放弃", 79.7);
memberA.setProducts(new Product [] {productA, productB});
memberB.setProducts(new Product [] {productB, productC});
productA.setMembers(new Member [] {memberA});
productB.setMembers(new Member [] {memberA, memberB});
productC.setMembers(new Member [] {memberB});
//根据要求获取数据
System.out.println("----------------根据用户查看浏览商品信息-------------------");
System.out.println(memberA.getInfo());
for(int x = 0; x < memberA.getProducts().length; x++) {
System.out.println("\t|-" + memberA.getProducts()[x].getInfo());
}
System.out.println("----------------根据商品查看被浏览记录-------------------");
System.out.println(productA.getInfo());
for(int x = 0; x < productA.getMembers().length; x++) {
System.out.println("\t|-" + productA.getMembers()[x].getInfo());
}
}
}
数据表与简单Java类(角色与权限)
- 最常见的用户权限管理的表结构设计:
- 一个用户可以拥有多个角色,一个角色可以有多个用户;
-
一个角色可以拥有多个权限;
- 要求实现如下功能:
- 可以根据一个用户找到该用户对应的所有角色,以及每个角色对应的所有权限信息;
- 可以根据一个角色找到该角色下的所有权限,以及拥有此角色的全部用户信息;
- 可以根据一个权限找到具备此权限的所有用户信息;
class Member {
private String mid;
private String name;
private Role roles [];
public Member() {}
public Member(String mid, String name) {
this.mid = mid;
this.name = name;
}
public String getInfo() {
return "【用户信息】mid:" + this.mid + "、name:" + this.name;
}
public void setRoles(Role roles []) {
this.roles = roles;
}
public void setMid(String mid) {
this.mid = mid;
}
public void setName(String name) {
this.name = name;
}
public Role [] getRoles() {
return this.roles;
}
public String getMid() {
return this.mid;
}
public String getName() {
return this.name;
}
}
class Role {
private long rid;
private String title;
private Member members [];
private Privilege privileges [];
public Role() {}
public Role(long rid, String title) {
this.rid = rid;
this.title = title;
}
public String getInfo() {
return "【角色信息】rid:" + this.rid + "、title:" + this.title;
}
public void setMembers(Member members []) {
this.members = members;
}
public void setPrivileges(Privilege privileges []) {
this.privileges = privileges;
}
public void setRid(long rid) {
this.rid = rid;
}
public void setTitle(String title) {
this.title = title;
}
public Member [] getMembers() {
return this.members;
}
public Privilege [] getPrivileges() {
return this.privileges;
}
public long getRid() {
return this.rid;
}
public String getTitle() {
return this.title;
}
}
class Privilege {
private long pid;
private String title;
private Role role;
public Privilege() {}
public Privilege(long pid, String title) {
this.pid = pid;
this.title = title;
}
public String getInfo() {
return "【权限信息】pid:" + this.pid + "、title:" + this.title;
}
public void setRole(Role role) {
this.role = role;
}
public void setPid(long pid) {
this.pid = pid;
}
public void setTitle(String title) {
this.title = title;
}
public Role getRole() {
return this.role;
}
public long getPid() {
return this.pid;
}
public String getTitle() {
return this.title;
}
}
public class JavaStudy {
public static void main(String [] args) {
//根据结构设置对象数据
Member memberA = new Member("001", "张三");
Member memberB = new Member("002", "李四");
Role roleA = new Role(1L, "系统配置");
Role roleB = new Role(2L, "备份管理");
Role roleC = new Role(3L, "人事管理");
Privilege privilegeA = new Privilege(1000L, "系统初始化");
Privilege privilegeB = new Privilege(1001L, "系统还原");
Privilege privilegeC = new Privilege(1002L, "系统环境修改");
Privilege privilegeD = new Privilege(2000L, "备份员工数据");
Privilege privilegeE = new Privilege(2001L, "备份部门数据");
Privilege privilegeF = new Privilege(2002L, "备份公文数据");
Privilege privilegeG = new Privilege(3000L, "增加员工");
Privilege privilegeH = new Privilege(3001L, "编辑员工");
Privilege privilegeI = new Privilege(3002L, "浏览员工");
Privilege privilegeJ = new Privilege(3003L, "员工离职");
//角色权限关系
roleA.setPrivileges(new Privilege [] {privilegeA, privilegeB, privilegeC});
roleB.setPrivileges(new Privilege [] {privilegeD, privilegeE, privilegeF});
roleC.setPrivileges(new Privilege [] {privilegeG, privilegeH, privilegeI, privilegeJ});
//权限角色关系
privilegeA.setRole(roleA);
privilegeB.setRole(roleA);
privilegeC.setRole(roleA);
privilegeD.setRole(roleB);
privilegeE.setRole(roleB);
privilegeF.setRole(roleB);
privilegeG.setRole(roleC);
privilegeH.setRole(roleC);
privilegeI.setRole(roleC);
privilegeJ.setRole(roleC);
//用户角色关系
memberA.setRoles(new Role [] {roleA, roleB});
memberB.setRoles(new Role [] {roleA, roleB, roleC});
//角色用户关系
roleA.setMembers(new Member [] {memberA, memberB});
roleB.setMembers(new Member [] {memberA, memberB});
roleC.setMembers(new Member [] {memberB});
//根据要求获取数据
System.out.println("----------------根据用户查找信息-----------------");
System.out.println(memberB.getInfo());
for(int x = 0; x < memberB.getRoles().length; x++) {
System.out.println("\t|-" + memberB.getRoles() [x].getInfo());
for(int y = 0; y < memberB.getRoles() [x].getPrivileges().length; y++) {
System.out.println("\t\t|-" + memberB.getRoles() [x].getPrivileges() [y].getInfo());
}
}
System.out.println("----------------根据角色查找信息-----------------");
System.out.println(roleB.getInfo());
System.out.println("\t|- 此角色下的所有权限信息");
for(int x = 0; x < roleB.getPrivileges().length; x++) {
System.out.println("\t\t|-" + roleB.getPrivileges() [x].getInfo());
}
System.out.println("\t|- 此角色下的所有用户信息");
for(int x = 0; x < roleB.getMembers().length; x++) {
System.out.println("\t\t|-" + roleB.getMembers() [x].getInfo());
}
System.out.println("----------------根据权限查找信息-----------------");
System.out.println(privilegeB.getInfo());
for(int x = 0; x < privilegeB.getRole().getMembers().length; x++) {
System.out.println("\t|-" + privilegeB.getRole().getMembers() [x].getInfo());
}
}
}