文章目录
- 1 综合实战:数据表与简单Java类的映射转换
- 2 综合实战:一对多映射
- 3 综合实战:多对多映射
- 4 综合实战:复杂多对多映射
1 综合实战:数据表与简单Java类的映射转换
简单Java类是面向对象设计的主要分析基础。主要根据数据表的结构来实现简单Java类。
在数据库之中实际是提供有若干数据表的。在实际开发中,数据表与简单Java类之间的基本映射关系如下:
- 数据实体表的设计 = 类的定义;
- 表中的字段 = 类的成员属性;
- 表的外键关联 = 引用关联;
- 表的一行记录 = 类的一个实例化对象;
- 表的多行记录 = 对象数组。
以上所对应数据表的关系之中可以发现有如下的关联存在: - 一个部门有多个雇员;
- 一个雇员属于一个部门;
- 一个雇员有一个领导;
下面将以上数据表转化为简单Java类的定义形式,在整个的程序代码之中要求可以获得如下信息:
- 根据部门信息获得以下内容:
(1)一个部门的完整信息;
(2)一个部门之中所有雇员的完整信息;
(3)一个雇员对应的领导的信息; - 根据雇员信息获得以下内容:
(1)一个雇员所在部门信息;
(2)一个雇员对应领导信息;
class Dept{
private long code;
private String name;
private String location;
private Emp emps[];
public Dept(){}
public Dept(long code, String name, String location){
this.code = code;
this.name = name;
this.location = location;
}
public String getInfo(){
return "部门编号: " + this.code + " 部门名称:" + this.name + " 部门地址:" + this.location;
}
public void setEmps(Emp[] emps){
this.emps = emps;
}
public Emp[] getEmps(){
return this.emps;
}
public void setCode(long code){
this.code = code;
}
public void setName(String name){
this.name = name;
}
public void setLocation(String location){
this.location = location;
}
public long getCode(){
return this.code;
}
public String getName(){
return this.name;
}
public String getLocation(){
return this.location;
}
}
class Emp{
private long code;
private String name;
private String job;
private double salary;
private double commission;
private Dept dept;
private Emp manager;
public Emp(){}
public Emp(long code, String name, String job, double salary, double commission){
this.code = code;
this.name = name;
this.job = job;
this.salary = salary;
this.commission = commission;
}
public String getInfo(){
return "雇员编号:" + this.code + " 雇员姓名:" + this.name + " 雇员职位:" + this.job + " 雇员工资:" + this.salary + " 雇员佣金:" + this.commission;
}
public void setDept(Dept dept){
this.dept = dept;
}
public Dept getDept(){
return this.dept;
}
public void setManager(Emp manager){
this.manager = manager;
}
public Emp getManager(){
return this.manager;
}
public void setCode(long code){
this.code = code;
}
public void setName(String name){
this.name = name;
}
public void setJob(String job){
this.job = job;
}
public void setSalary(double salary){
this.salary = salary;
}
public void setCommission(double commission){
this.commission = commission;
}
public long getCode(){
return this.code;
}
public String getName(){
return this.name;
}
public String getJob(){
return this.job;
}
public double getSalary(){
return this.salary;
}
public double getCommission(){
return this.commission;
}
}
public class JavaDemo{
public static void main(String[] args){
Dept dept = new Dept(10L, "软件开发部", "fujian, China");
Emp empA = new Emp(1001L, "zsl", "java", 2000.0, 0.0);
Emp empB = new Emp(1002L, "lks", "java", 3000.0, 0.0);
Emp empC = new Emp(1003L, "hhy", "java", 4000.0, 0.0);
dept.setEmps(new Emp[]{empA, empB, empC});
empA.setDept(dept);
empB.setDept(dept);
empC.setDept(dept);
empA.setManager(empB);
empB.setManager(empC);
System.out.println(dept.getInfo());
for( int i = 0; i < dept.getEmps().length; i++ ){
System.out.println("\t|- " + dept.getEmps()[i].getInfo());
if(dept.getEmps()[i].getManager() != null){
System.out.println("\t\t|- " + dept.getEmps()[i].getManager().getInfo());
}
}
System.out.println(empA.getDept().getInfo());
System.out.println(empA.getManager().getInfo());
}
}
对于数据表与简单Java类之间的映射最好的解决步骤:
3. 先抛开所有的关联字段不看,写出类的基本组成,而后再通过引用配置关联字段的关系。
4. 配置关联字段。
在以后进行实际项目开发的过程之中一定是分两个步骤实现的:
- 第一步:根据表的结构关系进行对象的配置;
- 第二步:根据要求通过结构获取数据。
2 综合实战:一对多映射
按照表的要求将表的结构转换为类结构,同时可以获取如下信息:
- 获取一个分类的完整信息;
- 可以根据分类获取其对应的所有子分类信息。
class Divide{
private long id;
private String name;
private DivideChild dividechild[];
public Divide(){}
public Divide(long id, String name){
this.id = id;
this.name = name;
}
public String getInfo(){
return "分类ID:" + this.id + " 分类名称:" + this.name;
}
public void setDivideChild(DivideChild dividechild[]){
this.dividechild = dividechild;
}
public DivideChild[] getDivideChild(){
return this.dividechild;
}
public void setId(long id){
this.id = id;
}
public void setName(String name){
this.name = name;
}
public long getId(){
return this.id;
}
public String getName(){
return this.name;
}
}
class DivideChild{
private long id;
private String name;
private Divide divide;
public DivideChild(){}
public DivideChild(long id, String name){
this.id = id;
this.name = name;
}
public String getInfo(){
return "子分类ID:" + this.id + " 子分类名称:" + this.name;
}
public void setDivide(Divide divide){
this.divide = divide;
}
public void setId(long id){
this.id = id;
}
public void setName(String name){
this.name = name;
}
public long getId(){
return this.id;
}
public String getName(){
return this.name;
}
}
public class JavaDemo{
public static void main(String[] args){
//first step: 进行对象关系配置
Divide one = new Divide(1L, "book");
DivideChild[] oneA = new DivideChild[]{
new DivideChild(11L, "soft"),
new DivideChild(12L, "hard")
};
one.setDivideChild(oneA);
for( int i =0; i < oneA.length; i++ ){
oneA[i].setDivide(one);
}
//second step:获取数据
System.out.println(one.getInfo());
for( int i =0; i < one.getDivideChild().length; i++ ){
System.out.println("\t|- " + one.getDivideChild()[i].getInfo());
}
}
}
3 综合实战:多对多映射
按照表的要求将表的结构转换为类结构,同时可以获取如下信息:
- 获取一个用户访问的所有商品的详细信息;
- 获取一个商品被浏览过的全部的用户信息。
class Member {
private long mid;
private String name;
private Product products[];
public Member(){}
public Member(long mid, String name){
this.mid = mid;
this.name = name;
}
public void setProducts(Product[] products){
this.products = products;
}
public Product[] getProducts(){
return this.products;
}
public String getInfo(){
return "user' id: " + this.mid + "user's name: " + this.name;
}
public void setMid(long mid){
this.mid = mid;
}
public void setName(String name){
this.name = name;
}
public long getMid(){
return this.mid;
}
public String getName(){
return this.name;
}
}
class Product{
private long pid;
private String name;
private Member members[];
public Product(){}
public Product(long pid, String name){
this.pid = pid;
this.name = name;
}
public void setMembers(Member[] members){
this.members = members;
}
public Member[] getMembers(){
return this.members;
}
public String getInfo(){
return "Product's id: " + this.pid + "product's name: " + this.name;
}
public void setPid(long pid){
this.pid = pid;
}
public void setName(String name){
this.name = name;
}
public long getPid(){
return this.pid;
}
public String getName(){
return this.name;
}
}
public class JavaDemo{
public static void main(String[] args){
//first step: 进行对象关系配置
Member memA = new Member(1001L, "lks");
Member memB = new Member(1002L, "hhy");
Product proA = new Product(1L, "zsl");
Product proB = new Product(2L, "zslll");
Product proC = new Product(3L, "zslllll");
memA.setProducts(new Product[]{proA, proB, proC});
memB.setProducts(new Product[]{proA, proC});
proA.setMembers(new Member[]{memA, memB});
proB.setMembers(new Member[]{memA});
proC.setMembers(new Member[]{memA, memB});
//second step:获取数据
System.out.println("----------------显示用户访问商品信息--------------");
System.out.println(memA.getInfo());
for(int i = 0; i < memA.getProducts().length; i++){
System.out.println(memA.getProducts()[i].getInfo());
}
System.out.println("----------------显示商品访问用户信息--------------");
System.out.println(proA.getInfo());
for(int i = 0; i < proA.getMembers().length; i++){
System.out.println(proA.getMembers()[i].getInfo());
}
}
}
4 综合实战:复杂多对多映射
在进行实际项目开发过程中,对于用户的授权管理是一项重要的任务,下面给出了一个最为常见的用户权限管理的表结构设计,基本关系如下:
- 一个用户可以拥有多个角色,一个角色可能有多个用户;
- 一个角色可以拥有多个权限;
要求实现如下查询功能: - 可以根据一个用户找到该用户对应的所有角色,以及每一个角色对应的所有权限信息;
- 可以根据一个角色找到该角色下的所有权限,以及拥有此角色的全部用户信息;
- 可以根据一个权限找到具备有此权限的所有用户信息。
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 void setRoles(Role[] roles){
this.roles = roles;
}
public Role[] getRoles(){
return this.roles;
}
public String getInfo(){
return "[Member] id: " + this.mid + "; name: " + this.name;
}
public void setMid(String mid){
this.mid = mid;
}
public void setName(String name){
this.name = name;
}
public String getMid(){
return this.mid;
}
public String getName(){
return this.name;
}
}
class Role {
private String rid;
private String name;
private Member members[];
private Privilege privileges[];
public Role(){}
public Role(String rid, String name){
this.rid = rid;
this.name = name;
}
public void setMembers(Member[] members){
this.members = members;
}
public void setPrivileges(Privilege[] privileges){
this.privileges = privileges;
}
public Member[] getMembers(){
return this.members;
}
public Privilege[] getPrivileges(){
return this.privileges;
}
public String getInfo(){
return "[Role] id: " + this.rid + "; name: " + this.name;
}
public void setRid(String rid){
this.rid = rid;
}
public void setName(String name){
this.name = name;
}
public String getRid(){
return this.rid;
}
public String getName(){
return this.name;
}
}
class Privilege {
private String pid;
private String name;
private Role role;
public Privilege(){}
public Privilege(String pid, String name){
this.pid = pid;
this.name = name;
}
public void setRole(Role role){
this.role = role;
}
public Role getRole(){
return this.role;
}
public String getInfo(){
return "[Privilege] id: " + this.pid + "; name: " + this.name;
}
public void setPid(String pid){
this.pid = pid;
}
public void setName(String name){
this.name = name;
}
public String getPid(){
return this.pid;
}
public String getName(){
return this.name;
}
}
public class JavaDemo{
public static void main(String[] args){
//first step: 进行对象关系配置
Member memA = new Member("1001", "lks");
Member memB = new Member("1002", "hhy");
Role roleA = new Role("5201", "system");
Role roleB = new Role("5202", "people");
Role roleC = new Role("5203", "data");
Privilege privA=new Privilege("13141", "system shutdown");
Privilege privB=new Privilege("13142", "system restart");
Privilege privC=new Privilege("13143", "system initlize");
Privilege privD=new Privilege("23141", "people add");
Privilege privE=new Privilege("23142", "people delete");
Privilege privF=new Privilege("23143", "people edit");
Privilege privG=new Privilege("33141", "data copy");
Privilege privH=new Privilege("33142", "data delete");
Privilege privI=new Privilege("33143", "data cut");
Privilege privJ=new Privilege("33144", "data clear");
roleA.setPrivileges(new Privilege[]{privA, privB, privC});
roleB.setPrivileges(new Privilege[]{privD, privE, privF});
roleC.setPrivileges(new Privilege[]{privG, privH, privI, privJ});
privA.setRole(roleA);
privB.setRole(roleA);
privC.setRole(roleA);
privD.setRole(roleB);
privE.setRole(roleB);
privF.setRole(roleB);
privG.setRole(roleC);
privH.setRole(roleC);
privI.setRole(roleC);
privJ.setRole(roleC);
memA.setRoles(new Role[]{roleA, roleB, roleC});
memB.setRoles(new Role[]{roleA, roleC});
roleA.setMembers(new Member[]{memA, memB});
roleB.setMembers(new Member[]{memA});
roleC.setMembers(new Member[]{memA, memB});
//second step:获取数据
System.out.println("-----------Member------------");
System.out.println(memA.getInfo());
for(int i = 0; i < memA.getRoles().length; i++){
System.out.println("\t|- " + memA.getRoles()[i].getInfo());
for(int j = 0; j < memA.getRoles()[i].getPrivileges().length; j++){
System.out.println("\t\t|- " + memA.getRoles()[i].getPrivileges()[j].getInfo());
}
}
System.out.println("-----------Role------------");
System.out.println(roleA.getInfo());
for(int i = 0; i < roleA.getPrivileges().length; i++){
System.out.println("\t|- " + roleA.getPrivileges()[i].getInfo());
}
for(int i = 0; i < roleA.getMembers().length; i++){
System.out.println("\t|- " + roleA.getMembers()[i].getInfo());
}
System.out.println("-----------Privilege------------");
System.out.println(privA.getInfo());
for(int i = 0; i < privA.getRole().getMembers().length; i++){
System.out.println("\t|- " + privA.getRole().getMembers()[i].getInfo());
}
}
}