Java面向对象编程-数据表与简单Java类的映射关系

Java面向对象编程-数据表与简单Java类的映射关系,第1张

数据表与Java类映射 1、数据表与简单Java类的映射转换

简单Java类是面向对象设计的主要分析的基础,但是在实际开发之中,简单Java类的来的来源都是根据数据表的结构来实现简单Java类的定义

在数据库中,提供有若干个数据表,在数据表中都可以描述出实际的信息,例如 部门信息表就是描述部门信息的,而雇员信息表是用来描述雇员信息的
数据表与简单Java类的映射关系:

  • 数据实体表设计 = 类的定义

  • 表的字段 = 类的成员属性

  • 表的一行记录 = 类的一个实例化对象

  • 表的多行记录 = 对象数组

  • 表得外键关联= 引用关联

部门

  • 部门编号 number(2) 主键

  • 部门名称 varchar(50)

  • 部门位置 varchar(50)

雇员

  • 雇员编号 number(4) 主键

  • 部门编号 number(2) 外键

  • 姓名 varchar(20)

  • 职位 varchar(10)

  • 基本工资 number(7,2)

  • 佣金 number(7,2)

  • 领导编号 number(4)

在以上的数据表中有如下关系:

  • 一个部门有多个雇员

  • 一个雇员属于一个部门

  • 一个雇员有一个领导

获取如下信息:

  • 一个部门的完整信息

  • 一个部门之中所有的完整信息

  • 一个雇员对应的领导信息

根据雇员信息获取一下信息

  • 一个雇员对应的部门信息

  • 一个雇员对应的领导信息

对于数据表与简单Java类之间的映射最好的解决方案是,先抛开所有的外键字段不看,写出类的基本组成,在设置关联字段的关系(非主函数类)

  • 第一步:写出核心代码

  • 第二步:配置关联字段

在实际开发之中分两步实现(主函数)

  • 第一步:根据表的结构关系进行对象的配置

  • 第二步:根据要求通过结构获取数据

部门类

public class Department {
    private long deptno ;
    private String dname ;
    private String loc ;

    private Employee [] employees ;
    
    public Department(){}
    
    public Department(long deptno,String dname,String loc){
        this.deptno = deptno ;
        this.dname = dname ;
        this.loc = loc ;
    }
    
    public String getInfo(){
        return "【部门信息】 部门标号:" + deptno + "  部门名称:" + dname + "  部门位置:" + loc;
    }
    
    public void setEmployees(Employee[] employees) {
        this.employees = employees;
    }
    
    public Employee[] getEmployees() {
        return employees;
    }
    
    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 deptno;
    }
    public String getLoc() {
        return loc;
    }
    public String getDname() {
        return dname;
    }

}

雇员类

public class Employee {
    private long empno ;
    private String ename ;
    private String job ;
    private double sal ;
    private double comm ;

    private Department department ;
    private Employee mgr ;
    
    public Employee(){}
    
    public Employee(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 "【雇员信息】 雇员编号:" + empno + "    姓名:" + ename + " 职位:" + job + " 基本工资:" + sal + "   佣金:" + comm;
    }
    
    public void setDepartment(Department department) {
        this.department = department;
    }
    public void setMgr(Employee mgr) {
        this.mgr = mgr;
    }
    
    public Department getDepartment() {
        return department;
    }
    public Employee getMgr() {
        return mgr;
    }
    
    public void setEmpno(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 empno;
    }
    public String getEname() {
        return ename;
    }
    public String getJob() {
        return job;
    }
    public double getSal() {
        return sal;
    }
    public double getComm() {
        return comm;
    }

}

主函数

public class JavaDemo {
    public static void main(String[] args) {
        //第一步 :根据关系进行类的定义
            //定义出各个 实例化对象 ,并没有任何的关联定义
        Department department = new Department(05,"信息部","郑州") ;
        Employee employeeB = new Employee(10080,"嘿嘿嘿","程序员",5000.00,500.00) ;
        Employee employeeC = new Employee(10086,"哈哈哈","软件架构师",15000.00,2000.00) ;
        Employee employeeA = new Employee(10086,"沈梦琳","主管",10000.00,1000.00) ;
            //为对象进行关联的设置
                //设置雇员与部门的关联
        employeeA.setDepartment(department);
        employeeB.setDepartment(department);
        employeeC.setDepartment(department);
                //设置雇员与领导的关联
        employeeA.setMgr(employeeB);
        employeeB.setMgr(employeeC);
                    //部门与雇员
        department.setEmployees(new Employee[] {employeeA,employeeB,employeeC});
        //第二步:根据关系获取数据
            //获取部门信息
        System.out.println(department.getInfo());
            //通过部门获取雇员信息和领导信息
        for (int x = 0 ; x < department.getEmployees().length ; x ++){
                //获取雇员信息
            System.out.println("\t|-" + department.getEmployees()[x].getInfo());
                //获取领导信息
            if (department.getEmployees()[x].getMgr() != null){ //只获取雇员领导不为空的领导
                System.out.println("\t\t|-" + department.getEmployees()[x].getMgr().getInfo());
            }

        }
        System.out.println("-------------------------------------------------------------------------------------");
            //通过雇员获取部门和领导
        System.out.println(employeeA.getDepartment().getInfo());
        System.out.println(employeeA.getMgr().getInfo());
        System.out.println();
    }

}

数据表简单Java类是进入开发的第一步,必须熟练掌握。

2、一对多映射

分类

分类IDnumber
名称varchar(50)

子分类

子分类IDnumber
分类IDnumber
名称varchar(50)

按照表的要求将表的结构转换为类的结构,同时可以获取如下信息:

  • 获取一个分类的完整信息

  • 可以根据分类获取其对应的所有子类的信息

实现代码:

public class Item {
    private long iid ;
    private String title ;
    private Subitem subitem [] ;

    public Item(long iid,String title){
        this.iid = iid ;
        this.title = title ;
    }
    
    public String getInfo(){
        return "【分类信息】 iid = " + iid + "     title = " + title ;
    }
    
    public void setSubitem(Subitem[] subitem) {
        this.subitem = subitem;
    }
    public Subitem[] getSubitem() {
        return subitem;
    }
    
    public void setIid(long iid) {
        this.iid = iid;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    
    public long getIid() {
        return iid;
    }
    public String getTitle() {
        return title;
    }

}
public class Subitem {
    private long sid ;
    private String title ;
    private Item item ;

    public Subitem(long sid,String title){
        this.sid = sid ;
        this.title = title ;
    }
    
    public String getInfo(){
        return "【子分类信息】 sid = " + sid + "     title = " + title ;
    }
    
    public void setItem(Item item) {
        this.item = item;
    }
    public Item getItem() {
        return item;
    }
    
    public void setSid(long sid) {
        this.sid = sid;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    
    public long getSid() {
        return sid;
    }
    public String getTitle() {
        return title;
    }

}
public class JavaDemo {
    public static void main(String[] args) {
        // 第一步:根据结构设置对象数据
        Item item = new Item(1L,"计算机图书") ;
        Subitem subitem [] = new Subitem [] {
                new Subitem(10L,"Java"),
                new Subitem(10L,"数据库")
        } ;
        item.setSubitem(subitem);
        for(int x = 0 ; x < subitem.length ; x ++){
            subitem[x].setItem(item);
        }
        //第二步:根据要求获取信息
        System.out.println("\t|-" + item.getInfo());
        for (int x = 0 ; x < item.getSubitem().length ; x ++){

            System.out.println("\t\t|-" + item.getSubitem()[x].getInfo());
        }
    }

}
3、多对多映射

用户信息

用户IDvarchar(50)
姓名varchar(50)

访问记录

用户IDvarchar(50)
商品number

商品

商品IDnumber
商品名称varchar(50)
商品价格number

将以上的结构转换为类结构,并且可以获取如下的信息:

  • 获取一个用户访问的所有商品的详细信息

  • 获取一个商品被浏览过的全部用户信息

对于此时的程序只需要去考虑实体表的设计,对于中间的访问记录的信息表不要求进行转换处理

实现代码:

public class Member {
    private String mid ;
    private String name ;
    private Product product [] ;

    public Member(String mid  ,String name){
        this.mid = mid ;
        this.name = name ;
    }
    
    public String geyInfo(){
        return "【用户信息】  mid = " + mid + "     name = " +  name ;
    }
    public void setProduct(Product[] product) {
        this.product = product;
    }
    public Product[] getProduct() {
        return product;
    }
    
    public void setMid(String mid) {
        this.mid = mid;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    public String getMid() {
        return mid;
    }
    public String getName() {
        return name;
    }

}
public class Product {
    private long pid ;
    private String title ;
    private  double price ;
    private Member member [] ;

    public Product(long pid,String title,double price){
        this.pid = pid ;
        this.title = title ;
        this.price = price ;
    }
    
    public String getInfo(){
        return "【产品信息】  pid = " + pid + "     title = " + title + "     price = " + price ;
    }
    
    public void setMember(Member[] member) {
        this.member = member;
    }
    public Member[] getMember() {
        return member;
    }
    
    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 long getPid() {
        return pid;
    }
    public String getTitle() {
        return title;
    }
    public double getPrice() {
        return price;
    }

}
public class JavaDemo {
    public static void main(String[] args) {
        //第一步:根据结构设置对象数据
        Member memberA = new Member("2000","沈梦琳");
        Member memberB = new Member("2001","嘿嘿嘿");
        Member memberC = new Member("2002","哈哈哈");
        Product productA = new Product(1L,"Java",79.8) ;
        Product productB = new Product(2L,"SQL",68.0) ;
        Product productC = new Product(3L,"编程常识",99.0) ;
        memberA.setProduct(new Product[] {productA,productB,productC});
        memberB.setProduct(new Product[] {productA});
        memberC.setProduct(new Product[] {productB,productC});
        productA.setMember(new Member[] {memberA,memberB,memberC});
        productB.setMember(new Member[] {memberB});
        productC.setMember(new Member[] {memberA,memberC});

        //第二步:根据要求获取数据
        System.out.println("-------------------------根据用户查看浏览商品信息-------------------------");
        System.out.println(memberA.geyInfo());
        for(int x = 0 ; x < memberA.getProduct().length ; x ++){
            System.out.println("\t\t|- " + memberA.getProduct()[x].getInfo());
        }
    
        System.out.println("-------------------------根据用户查看浏览商品信息-------------------------");
        System.out.println(productA.getInfo());
        for (int x = 0 ; x < productA.getMember().length ; x ++){
            System.out.println("\t\t|- " + productA.getMember()[x].geyInfo());
        }
    }

}
4、复杂多对多映射

在复杂多对多映射中,应当参考一对多和多对多

在进行实际项目开发过程之中,对于用户的授权管理是一项重要的任务,下面给出了一个最为常见的用户权限管理的设计、基本的关系如下:

  • 一个用户可以拥有多个角色,一个角色可能有多个用户

  • 一个角色可以拥有多个权限;

权限

权限IDvarchar(50)
角色IDvarchar(50)
名称varchar(50)

角色

角色IDvarchar(50)
名称varchar(50)

用户-角色

用户IDvarchar(50)
角色IDvarchar(50)

用户

用户IDvarchar(50)
姓名varchar(50)

要求实现如下查询功能:

  • 可以根据一个用户找到该用户对应的所有角色,以及每一个角色对应的所有权限信息;

  • 可以根据一个角色找到该角色下的所有权限,以及拥有此角色的全部用户信息;

  • 可以根据一个权限找到具备有此权限的所有用户信息。

代码如下:

public class Member {
    private String mid ;
    private String name ;
    private Role roles [] ;

    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 "【用户信息】 mid = " + mid + "     name = " + name ;
    }
    
    public void setMid(String mid) {
        this.mid = mid;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    public String getMid() {
        return mid;
    }
    public String getName() {
        return name;
    }

}
public class Role {
    private long rid ;
    private String title ;
    private Member member [] ;
    private Privilege privilege [] ;

    public Role(long rid,String title){
        this.rid = rid ;
        this.title = title ;
    }
    
    public String getInfo(){
        return "【角色信息】 rid = " + rid + "     title = " + title ;
    }
    public void setMember(Member member []){
        this.member = member ;
    }
    public Member [] getMember(){
        return member ;
    }
    
    public void setPrivilege(Privilege privilege []){
        this.privilege = privilege ;
    }
    public Privilege [] getPrivilege(){
    return privilege ;
    }
    
    public void setRid(long rid) {
        this.rid = rid;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    
    public long getRid() {
        return rid;
    }
    public String getTitle() {
        return title;
    }

}
public class Privilege {
    private long pid ;
    private String title ;
    private Role role ;

    public Privilege(long pid, String title){
        this.pid = pid ;
        this.title = title ;
    }
    
    public String getInfo(){
        return "【权限信息】 pid = " + pid + "     title = " + title ;
    }
    
    public void setRole(Role role) {
        this.role = role;
    }
    public Role getRole() {
        return role;
    }
    
    public void setPid(long pid) {
        this.pid = pid;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    
    public long getPid() {
        return pid;
    }
    public String getTitle() {
        return title;
    }

}
public class JavaDemo {
    public static void main(String[] args) {
        //第一步:根据结构设置对象数据
        Member memberA = new Member("201901","沈梦琳") ;
        Member memberB = new Member("201902","嘿嘿嘿") ;
        Member memberC = new Member("201903","哈哈哈") ;

        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(1003L,"备份员工数据");
        Privilege privilegeE = new Privilege(1004L,"备份部门数据");
        Privilege privilegeF = new Privilege(1005L,"备份公文数据");
        Privilege privilegeG = new Privilege(1006L,"增加员工");
        Privilege privilegeH = new Privilege(1007L,"编辑员工");
        Privilege privilegeI = new Privilege(1008L,"浏览员工");
        Privilege privilegeJ = new Privilege(1009L,"员工离职");
    
        //增加角色与权限的对应关系
        roleA.setPrivilege(new Privilege[] {privilegeA,privilegeB,privilegeC});
        roleB.setPrivilege(new Privilege[] {privilegeD,privilegeE,privilegeF});
        roleC.setPrivilege(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,roleC});
        memberC.setRoles(new Role[] {roleA,roleB,roleC});
    
        roleA.setMember(new Member[] {memberA,memberB,memberC});
        roleB.setMember(new Member[] {memberA,memberC});
        roleC.setMember(new Member[] {memberB,memberC});
    
        //第二步:根据要求获取数据
                //通过用户查找
        System.out.println("-----------通过用户查找信息-----------");
        System.out.println(memberA.getInfo());
        for(int x = 0 ; x < memberA.getRoles().length; x ++){
            System.out.println("\t\t|- " + memberA.getRoles()[x].getInfo());
            for (int y = 0 ; y < memberB.getRoles()[x].getPrivilege().length ; y ++){
                System.out.println("\t\t\t|- " + memberB.getRoles()[x].getPrivilege()[y].getInfo());
            }
        }
        System.out.println();
        System.out.println(memberB.getInfo());
        for(int x = 0 ; x < memberB.getRoles().length; x ++){
            System.out.println("\t\t|- " + memberB.getRoles()[x].getInfo());
            for (int y = 0 ; y < memberB.getRoles()[x].getPrivilege().length ; y ++){
                System.out.println("\t\t\t|- " + memberB.getRoles()[x].getPrivilege()[y].getInfo());
            }
        }
        System.out.println();
        System.out.println(memberC.getInfo());
        for(int x = 0 ; x < memberC.getRoles().length; x ++){
            System.out.println("\t\t|- " + memberC.getRoles()[x].getInfo());
            for (int y = 0 ; y < memberC.getRoles()[x].getPrivilege().length ; y ++){
                System.out.println("\t\t\t|- " + memberC.getRoles()[x].getPrivilege()[y].getInfo());
            }
        }
                //通过用户和权限查找信息
        System.out.println("-----------通过用户和权限查找信息-----------");
        System.out.println(roleA.getInfo());
        System.out.println("\t|- 浏览此用户下的所有权限信息");
        for (int x = 0 ; x < roleA.getPrivilege().length ; x ++){
            System.out.println("\t\t|- " + roleA.getPrivilege()[x].getInfo());
        }
        System.out.println();
        System.out.println(roleB.getInfo());
        System.out.println("\t|- 浏览此用户下的所有权限信息");
        for (int x = 0 ; x < roleB.getPrivilege().length ; x ++){
            System.out.println("\t\t|- " + roleB.getPrivilege()[x].getInfo());
        }
        System.out.println();
        System.out.println(roleC.getInfo());
        System.out.println("\t|- 浏览此用户下的所有权限信息");
        for (int x = 0 ; x < roleC.getPrivilege().length ; x ++){
            System.out.println("\t\t|- " + roleC.getPrivilege()[x].getInfo());
        }
        System.out.println();
        System.out.println("\t|- 浏览此角色下的所有用户信息");
        for (int x = 0 ; x < roleA.getMember().length ; x ++){
            System.out.println("\t\t" + roleA.getMember()[x].getInfo());
        }
        System.out.println();
        System.out.println("\t|- 浏览此角色下的所有用户信息");
        for (int x = 0 ; x < roleB.getMember().length ; x ++){
            System.out.println("\t\t" + roleB.getMember()[x].getInfo());
        }
        System.out.println();
        System.out.println("\t|- 浏览此角色下的所有用户信息");
        for (int x = 0 ; x < roleC.getMember().length ; x ++){
            System.out.println("\t\t" + roleC.getMember()[x].getInfo());
        }
                //通过权限查找信息
        System.out.println("-----------通过权限查找信息-----------");
        System.out.println(privilegeA.getInfo());
        for(int x = 0 ; x < privilegeA.getRole().getMember().length ; x ++){
            System.out.println(privilegeA.getRole().getMember()[x].getInfo());
        }
        System.out.println();
        System.out.println(privilegeB.getInfo());
        for(int x = 0 ; x < privilegeB.getRole().getMember().length ; x ++){
            System.out.println(privilegeB.getRole().getMember()[x].getInfo());
        }
        System.out.println();
        System.out.println(privilegeC.getInfo());
        for(int x = 0 ; x < privilegeC.getRole().getMember().length ; x ++){
            System.out.println(privilegeC.getRole().getMember()[x].getInfo());
        }
        System.out.println();
        System.out.println(privilegeD.getInfo());
        for(int x = 0 ; x < privilegeD.getRole().getMember().length ; x ++){
            System.out.println(privilegeD.getRole().getMember()[x].getInfo());
        }
        System.out.println();
        System.out.println(privilegeE.getInfo());
        for(int x = 0 ; x < privilegeE.getRole().getMember().length ; x ++){
            System.out.println(privilegeE.getRole().getMember()[x].getInfo());
        }
        System.out.println();
        System.out.println(privilegeF.getInfo());
        for(int x = 0 ; x < privilegeF.getRole().getMember().length ; x ++){
            System.out.println(privilegeF.getRole().getMember()[x].getInfo());
        }
        System.out.println();
        System.out.println(privilegeG.getInfo());
        for(int x = 0 ; x < privilegeG.getRole().getMember().length ; x ++){
            System.out.println(privilegeG.getRole().getMember()[x].getInfo());
        }
        System.out.println();
        System.out.println(privilegeH.getInfo());
        for(int x = 0 ; x < privilegeH.getRole().getMember().length ; x ++){
            System.out.println(privilegeH.getRole().getMember()[x].getInfo());
        }
        System.out.println();
        System.out.println(privilegeI.getInfo());
        for(int x = 0 ; x < privilegeI.getRole().getMember().length ; x ++){
            System.out.println(privilegeI.getRole().getMember()[x].getInfo());
        }
        System.out.println();
        System.out.println(privilegeJ.getInfo());
        for(int x = 0 ; x < privilegeJ.getRole().getMember().length ; x ++){
            System.out.println(privilegeJ.getRole().getMember()[x].getInfo());
        }


    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/871650.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存