本文最后更新于2022年6月1日,已超过 1 年没更新!内容可能已失效,请自行测试。
数据表与简单Java类映射转换
简单Java类是面向对象设计的主要分析基础,但是对于实际的开发之中简单Java类定义来源依据一般都是数据库表
数据库提供若干个数据表,每一张实体数据表都可以描述一些具体的事物概念
数据表与简单Java类映射基本关系如下
- 数据实体表设计=类的定义
- 表中字段=类的成员属性
- 表的一行记录=类的一个实例化对象
- 累的多行记录=对象数组
- 表的外键关联=引用关联
以上对应数据表的关系之中可以发现有如下的关联存在
- 一个部门有多个雇员
- 一个雇员属于一个部门
- 一个雇员有一个领导
将以上数据表转为简单Java类形式
- 根据部门信息表获得
- 一个部门的完整信息
- 一个部门之中所有雇员的完整信息
- 一个雇员对应的领导信息
- 根据雇员信息表获得
- 一个雇员所在部门信息
- 一个雇员对应的领导信息
先抛开所有的关联字段,写出类的基本组成,而后通过应用配置关联字段的关系
第一步:分别定义EMP,DEPT类
class DEPT {
private long deptno; // 部门编号
private String dname; // 部门名称
private String loc; // 部门位置
private EMP emps[]; // 一个部门有多个雇员
public DEPT(long enpto, String ename, String loc) {
this.deptno = enpto;
this.dname = ename;
this.loc = loc;
}
public void setEmps(EMP[] emps) {
this.emps = emps;
}
public EMP[] getEmps() {
return emps;
}
public String getInfo() {
return "部门信息表-部门编号=" + this.deptno + " " + "部门名称 " + this.dname + " " + "部门位置 " + this.loc;
}
// set/get省略
}
class EMP {
private long empno; // 编号
private String ename; // 姓名
private String job; // 职位
private double sal; // 基本工资
private double comm; // 奖金
private DEPT deptno; // 雇员属于哪个部门
private EMP mgr; // 领导
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 void setDeptno(DEPT deptno) {
this.deptno = deptno;
}
public DEPT getDeptno() {
return this.deptno;
}
public void setMgr(EMP mgr) {
this.mgr = mgr;
}
public EMP getMgr() {
return this.mgr;
}
public String getInfo() {
return "雇员信息 雇员编号="
+ this.empno
+ " "
+ "雇员姓名 "
+ this.ename
+ " "
+ "雇员工作 "
+ this.job
+ " "
+ "雇员工资 "
+ this.sal
+ " "
+ "雇员奖金 "
+ this.comm;
}
// set/get省略
}
第二步:配置所有的关联字段
public class TableDemo {
public static void main(String[] args) {
// 根据关系进行类的定义
DEPT dept = new DEPT(1, "总部", "上海");
EMP empA = new EMP(1, "老三", "普通职员", 8000, 500.00);
EMP empB = new EMP(2, "老二", "经理", 5000, 300.00);
EMP empC = new EMP(3, "老大", "领导", 200, 100.00);
empA.setDeptno(dept); // 雇员与部门的关联
empB.setDeptno(dept); // 雇员与部门的关联
empC.setDeptno(dept); // 雇员与部门的关联
empA.setMgr(empB); // 雇员与领导的关联
empB.setMgr(empC); // 雇员与领导的关联
dept.setEmps(new EMP[] {empA, empB, 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].getMgr() != null) {
System.out.println("\t\t|-" + dept.getEmps()[i].getMgr().getInfo());
}
}
System.out.println(
"--------------------------------------------------------------------------------------------");
System.out.println(empA.getDeptno().getInfo()); // 根据雇员来取部门信息
System.out.println(empA.getMgr().getInfo()); // 根据雇员获取领导信息
}
}
一对多映射
根据表的要求将表的结构转换为类结构,同时获取下列信息
- 获取一个分类的完整信息
- 可以根据分类获取其对应的所有子分类的信息
class Classify {
private long number; // 分类ID
private String name; // 分类名称
private Classify2 sclass[]; // 子分类
public Classify(long number, String name) {
this.number = number;
this.name = name;
}
public void setSclass(Classify2[] sclass) {
this.sclass = sclass;
}
public Classify2[] getSclass() {
return sclass;
}
public String getInfo() {
return "分类表信息 分类ID为 " + this.number + " " + "分类名称 " + this.name;
}
// get,set省略
}
class Classify2 {
private long snumber; // 子分类ID
private String name; // 子分类名称
private Classify fclass; // 分类
public Classify2(long snumber, String name) {
this.snumber = snumber;
this.name = name;
}
public void setFclass(Classify fclass) {
this.fclass = fclass;
}
public Classify getFclass() {
return fclass;
}
public String getInfo() {
return "子分类表信息 子分类ID为 " + this.snumber + " " + "分类名称 " + this.name;
}
// get,set省略
}
public class TableDemo2 {
public static void main(String[] args) {
Classify class1 = new Classify(1, "母分类");
Classify2 class2 = new Classify2(11, "子分类1");
Classify2 class3 = new Classify2(22, "子分类2");
Classify2 class4 = new Classify2(33, "子分类3");
System.out.println(class1.getInfo());
System.out.println(class2.getInfo());
System.out.println(class3.getInfo());
System.out.println(class4.getInfo());
class1.setSclass(new Classify2[] {class2, class3, class4});
System.out.println("--------------------------");
for (int i = 0; i < class1.getSclass().length; i++) {
System.out.println("\t-|" + class1.getSclass()[i].getInfo());
}
System.out.println("--------------------------");
class2.setFclass(class1);
class3.setFclass(class1);
class4.setFclass(class1);
System.out.println(class2.getFclass().getInfo());
System.out.println(class3.getFclass().getInfo());
System.out.println(class4.getFclass().getInfo());
}
}
多对多映射
根据表的要求将表的结构转换为类结构,同时获取下列信息
- 获取一个用户访问的所有商品的详细信息
- 获取一个商品被浏览过的全部的用户的信息
class SUser {
private long userid;
private String username;
private Shop shop[];
public SUser(long userid, String username) {
this.userid = userid;
this.username = username;
}
public String getInfo() {
return "用户信息 用户ID " + this.userid + " " + "用户姓名 " + this.username;
}
public void setShop(Shop[] shop) {
this.shop = shop;
}
public Shop[] getShop() {
return shop;
}
}
class Shop {
private long shopid;
private String shopname;
private double shopprice;
private SUser users[];
public Shop(long shopid, String shopname, double shopprice) {
this.shopid = shopid;
this.shopname = shopname;
this.shopprice = shopprice;
}
public String getInfo() {
return "商品信息 商品ID "
+ this.shopid
+ " "
+ "商品名称 "
+ this.shopname
+ " "
+ "商品价格"
+ this.shopprice;
}
public void setUsers(SUser[] users) {
this.users = users;
}
public SUser[] getUsers() {
return users;
}
}
public class TableDemo3 {
public static void main(String[] args) {
SUser userA = new SUser(1, "老大");
SUser userB = new SUser(2, "老二");
SUser userC = new SUser(3, "老三");
Shop shop1 = new Shop(11, "薯条", 5);
Shop shop2 = new Shop(22, "面包", 10);
Shop shop3 = new Shop(33, "牛奶", 20);
userA.setShop(new Shop[] {shop1, shop2});
userB.setShop(new Shop[] {shop2, shop3});
userC.setShop(new Shop[] {shop1, shop3});
shop1.setUsers(new SUser[] {userA, userC});
shop2.setUsers(new SUser[] {userA, userB});
shop3.setUsers(new SUser[] {userB, userC});
System.out.println("根据商品查看用户");
for (int i = 0; i < shop1.getUsers().length; i++) {
System.out.println(shop1.getUsers()[i].getInfo());
}
System.out.println("------------------------------------------");
for (int i = 0; i < shop2.getUsers().length; i++) {
System.out.println(shop2.getUsers()[i].getInfo());
}
System.out.println("------------------------------------------");
for (int i = 0; i < shop3.getUsers().length; i++) {
System.out.println(shop3.getUsers()[i].getInfo());
}
System.out.println("------------------------------------------");
System.out.println("根据用户查看商品");
for (int i = 0; i < userA.getShop().length; i++) {
System.out.println(userA.getShop()[i].getInfo());
}
System.out.println("------------------------------------------");
for (int i = 0; i < userB.getShop().length; i++) {
System.out.println(userB.getShop()[i].getInfo());
}
System.out.println("------------------------------------------");
for (int i = 0; i < userC.getShop().length; i++) {
System.out.println(userC.getShop()[i].getInfo());
}
}
}
复杂多对多映射
- 一个用户可以拥有多个角色,一个角色可以有多个用户
- 一个角色可以拥有多个权限
class RUser {
private long userid;
private String username;
private Role roles[];
public RUser(long userid, String username) {
this.userid = userid;
this.username = username;
}
public void setRoles(Role[] roles) {
this.roles = roles;
}
public Role[] getRoles() {
return roles;
}
public String getInfo() {
return "用户信息 用户id" + this.userid + "用户名称" + this.username;
}
}
class Role {
private long roleid;
private String rolename;
private Power powers[];
private RUser users[];
public Role(long roleid, String rolename) {
this.roleid = roleid;
this.rolename = rolename;
}
public void setPowers(Power[] powers) {
this.powers = powers;
}
public Power[] getPowers() {
return powers;
}
public void setUsers(RUser[] users) {
this.users = users;
}
public RUser[] getUsers() {
return users;
}
public String getInfo() {
return "角色信息 角色id" + this.roleid + "角色名称" + this.rolename;
}
}
class Power {
private long powerid;
private String powername;
private Role Role;
public Power(long powerid, String powername) {
this.powerid = powerid;
this.powername = powername;
}
public String getInfo() {
return "权限信息 权限id" + this.powerid + "权限名称" + this.powername;
}
public void setRole(Role role) {
Role = role;
}
public Role getRole() {
return Role;
}
}
public class TableDemo4 {
public static void main(String[] args) {
RUser userA = new RUser(1, "老大");
RUser userB = new RUser(2, "老二");
Role roleA = new Role(1L, "系统配置");
Role roleB = new Role(2L, "备份服务");
Role roleC = new Role(3L, "人事管理");
Power powerA = new Power(1001L, "系统初始化");
Power powerB = new Power(1002L, "系统还原");
Power powerC = new Power(1003L, "系统环境修改");
Power powerD = new Power(2001L, "备份员工数据");
Power powerE = new Power(2002L, "系统部门数据");
Power powerF = new Power(3001L, "系统公文数据");
Power powerG = new Power(4001L, "增加员工");
Power powerH = new Power(4002L, "浏览员工");
Power powerI = new Power(4003L, "编辑员工");
Power powerJ = new Power(4004L, "员工离职");
roleA.setPowers(new Power[] {powerA, powerB, powerC});
roleB.setPowers(new Power[] {powerD, powerE, powerF});
roleC.setPowers(new Power[] {powerG, powerH, powerI, powerJ});
powerA.setRole(roleA);
powerB.setRole(roleA);
powerC.setRole(roleA);
powerD.setRole(roleB);
powerE.setRole(roleB);
powerF.setRole(roleB);
powerG.setRole(roleC);
powerH.setRole(roleC);
powerI.setRole(roleC);
powerJ.setRole(roleC);
userA.setRoles(new Role[] {roleA, roleB});
userB.setRoles(new Role[] {roleA, roleB, roleC});
roleA.setUsers(new RUser[] {userA, userB});
roleB.setUsers(new RUser[] {userA, userB});
roleC.setUsers(new RUser[] {userB});
System.out.println("通过用户查找信息");
System.out.println(userA.getInfo());
System.out.println(userB.getInfo());
System.out.println("-----------------------");
for (int i = 0; i < userB.getRoles().length; i++) {
System.out.println("\t-|" + userB.getRoles()[i].getInfo());
for (int j = 0; j < userB.getRoles()[i].getPowers().length; j++) {
System.out.println("\t\t-|" + userB.getRoles()[i].getPowers()[i].getInfo());
}
}
System.out.println("通过角色查找信息");
System.out.println(roleB.getInfo());
System.out.println("\t-浏览此角色下的所有权限信息");
for (int i = 0; i < roleB.getPowers().length; i++) {
System.out.println("\t\t-|" + roleB.getPowers()[i].getInfo());
}
System.out.println("\t-浏览此角色下的所有用户信息");
for (int i = 0; i < roleB.getUsers().length; i++) {
System.out.println("\t\t-|" + roleB.getUsers()[i].getInfo());
}
System.out.println("通过权限查找信息");
System.out.println(powerA.getInfo());
for (int i = 0; i < powerA.getRole().getUsers().length; i++) {
System.out.println("\t-|" + powerA.getRole().getUsers()[i].getInfo());
}
}
}
Comments | NOTHING