Java——簡單Java類深入(數據表與簡單Java類、一對多映射、雙向一對多映射、多對多映射)

目錄

1、數據表與簡單Java類的映射

2、一對多數據映射

3、雙向一對多映射

4、多對多數據映射


1、數據表與簡單Java類的映射

簡單Java類是整個項目開發的靈魂,其有嚴格的開發標準,最爲重要的是它要與數據表完全對應。由於目前沒有接觸過多的程序設計功能,所以對於此處的訪問就有了一些限制,目前要求可以完成如下兩個操作:

  • 根據數據表的結構關係進行數據以及引用的設置;
  • 根據數據表的結構可以取出所需要的數據。

選用熟悉的數據結構:dept、emp,實現這樣的轉換操作。現在開發要求如下:

1)使用以下的數據表與表中的字段:

  • 僱員表emp:empno、ename、job、sal、comm、mgr、deptno;
  • 部門表dept:deptno、dname、loc。

2)數據操作要求:

  • 根據表結構完整的設置僱員、經理、部門的關係;
  • 可以完成如下輸出:

             --可以輸出一個僱員的完整信息,包括僱員 的領導、以及所在的部門信息;
             --可以輸出一個部門的完整信息,以及這個部門的所有僱員信息,以及這個僱員的領導信息。

【第一步】:寫出基本字段的映射轉換

  • 僱員表emp:empno、ename、job、sal、comm、mgr、deptno;
  • 部門表dept:deptno、dname、loc。
class Emp{
    private int empno;
    private String name;
    private String job;
    private double sal;
    private double comm;

    public Emp(){}

    public Emp(int empno, String name, String job, double sal, double comm) {
        this.empno = empno;
        this.name = name;
        this.job = job;
        this.sal = sal;
        this.comm = comm;
    }
    //setter getter暫時省略
    public String getInfo(){
        return "僱員編號:"+this.empno +",姓名:"+this.name +",職位:"+this.job+",佣金:"+this.sal+",佣金:"+this.comm;
    }
}

class Dept{
    private int deptno;
    private String dname;
    private String loc;

    public Dept(){}

    public Dept(int deptno, String dname, String loc) {
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
    }
    //setter getter暫時省略
    public String getInfo(){
        return "部門編號:"+this.deptno +",名稱:"+this.dname +",位置:"+this.loc;
    }
}

【第二步】:設計關係字段

本程序存在兩個關係:

  • 自身關聯:mgr字段,mgr也是一個僱員;
  • 外鍵關聯:deptno字段;
class Emp{
    private int empno;
    private String name;
    private String job;
    private double sal;
    private double comm;
    private Emp mgr;//領導,一個僱員一個領導
    private Dept dept;//部門,一個僱員屬於一個部門

    public Emp(){}

    public Emp(int empno, String name, String job, double sal, double comm) {
        this.empno = empno;
        this.name = name;
        this.job = job;
        this.sal = sal;
        this.comm = comm;
    }

    public Emp getMgr() {
        return mgr;
    }

    public void setMgr(Emp mgr) {
        this.mgr = mgr;
    }

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    //setter getter暫時省略
    public String getInfo(){
        return "僱員編號:"+this.empno +",姓名:"+this.name +",職位:"+this.job+",佣金:"+this.sal+",佣金:"+this.comm;
    }
}

class Dept{
    private int deptno;
    private String dname;
    private String loc;
    private Emp[] emps;

    public Dept(){}
    
    public Dept(int deptno, String dname, String loc) {
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
    }
    //setter getter暫時省略
    public Emp[] getEmps() {
        return emps;
    }

    public void setEmps(Emp[] emps) {
        this.emps = emps;
    }

    public String getInfo(){
        return "部門編號:"+this.deptno +",名稱:"+this.dname +",位置:"+this.loc;
    }
}

【第三步】:執行數據操作

  • 設置數據關係;
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //第一步:根據已有的表結構設置數據
        //1、準備好所有獨立的類對象
        Dept dept =new Dept(10,"技術部","China");
        Emp empA = new Emp(100,"張三","硬件工程師",800.0,0.0);
        Emp empB = new Emp(100,"李四","軟件工程師",900.0,0.0);
        //2、設置關係
        empA.setMgr(empB);//設置僱員與領導關係
        empA.setDept(dept);//設置僱員與部門關係
        empB.setDept(dept);//設置僱員與部門關係
        dept.setEmps(new Emp[]{empA,empB});//一個部門包含多個僱員
    }
}
  • 取出僱員完整數據;
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //第一步:根據已有的表結構設置數據
        //1、準備好所有獨立的類對象
        Dept dept =new Dept(10,"技術部","China");
        Emp empA = new Emp(100,"張三","硬件工程師",800.0,0.0);
        Emp empB = new Emp(100,"李四","軟件工程師",900.0,0.0);
        //2、設置關係
        empA.setMgr(empB);//設置僱員與領導關係
        empA.setDept(dept);//設置僱員與部門關係
        empB.setDept(dept);//設置僱員與部門關係
        dept.setEmps(new Emp[]{empA,empB});//一個部門包含多個僱員
        //第二步:根據表結構,利用引用關係取出數據
        System.out.println("僱員信息:"+empA.getInfo()); //輸出僱員基本信息
        System.out.println("領導信息:"+empA.getMgr().getInfo()); //輸出僱員領導信息
        System.out.println("部門信息:"+empA.getDept().getInfo()); //輸出僱員部門信息

    }
}

  • 取出部門的完整信息
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //第一步:根據已有的表結構設置數據
        //1、準備好所有獨立的類對象
        Dept dept =new Dept(10,"技術部","China");
        Emp empA = new Emp(100,"張三","硬件工程師",800.0,0.0);
        Emp empB = new Emp(100,"李四","軟件工程師",900.0,0.0);
        //2、設置關係
        empA.setMgr(empB);//設置僱員與領導關係
        empA.setDept(dept);//設置僱員與部門關係
        empB.setDept(dept);//設置僱員與部門關係
        dept.setEmps(new Emp[]{empA,empB});//一個部門包含多個僱員
        //第二步:根據表結構,利用引用關係取出數據
        System.out.println("僱員信息:"+empA.getInfo()); //輸出僱員基本信息
        System.out.println("領導信息:"+empA.getMgr().getInfo()); //輸出僱員領導信息
        System.out.println("部門信息:"+empA.getDept().getInfo()); //輸出僱員部門信息
        System.out.println("==================================");
        System.out.println(dept.getInfo());//部門信息
        for(int x=0;x<dept.getEmps().length;x++){
            System.out.println(dept.getEmps()[x].getInfo());
            if(dept.getEmps()[x].getMgr()!=null)
            System.out.println(dept.getEmps()[x].getMgr().getInfo());

        }
    }
}

2、一對多數據映射

【舉例】:課程分類

一個課程分類有多個子分類,要求:

  • 利用簡單Java類實現數據表的還原;
  • 進行如下輸出:

                --可以輸出一個子分類的信息,同時輸出它所對應的分類信息;

                --可以輸出一個分類的信息,及所包含的所有子分類信息;

第一步:實現基本字段

class Item{
    private int iid;
    private String title;
    private String note;
    //setter  getter  無參省略
    public Item(int iid, String title, String note) {
        this.iid = iid;
        this.title = title;
        this.note = note;
    }

    public String getInfo(){
        return "id:"+this.iid+",名稱:"+this.title+",簡介:"+this.note;
    }

}

class SubItem{
    private int sid;
    private String title;
    private String note;

    public SubItem(int sid, String title, String note) {
        this.sid = sid;
        this.title = title;
        this.note = note;
    }

    public String getInfo(){
        return "id:"+this.sid+",名稱:"+this.title+",簡介:"+this.note;
    }

}

第二步:設置關聯

class Item{
    private int iid;
    private String title;
    private String note;
    private SubItem[] subItems;

    public Item(int iid, String title, String note) {
        this.iid = iid;
        this.title = title;
        this.note = note;
    }
    //其他 setter  getter  無參省略
    public SubItem[] getSubItems() {
        return subItems;
    }

    public void setSubItems(SubItem[] subItems) {
        this.subItems = subItems;
    }

    public String getInfo(){
        return "id:"+this.iid+",名稱:"+this.title+",簡介:"+this.note;
    }

}

class SubItem{
    private int sid;
    private String title;
    private String note;
    private Item item;

    public SubItem(int sid, String title, String note) {
        this.sid = sid;
        this.title = title;
        this.note = note;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public String getInfo(){
        return "id:"+this.sid+",名稱:"+this.title+",簡介:"+this.note;
    }

}

第三步:設置並取得數據

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //準備出所有獨立對象
        Item item = new Item(100,"化工專業","搞化學的");
        SubItem subItemA = new SubItem(1001,"煉油","煉油的");
        SubItem subItemB = new SubItem(1002,"提純","提純的");
        //設置彼此引用關係
        item.setSubItems(new SubItem[]{subItemA,subItemB});
        subItemA.setItem(item);
        subItemB.setItem(item);
        //取出數據
        System.out.println(subItemA.getInfo());
        System.out.println(subItemA.getItem().getInfo());
        System.out.println(item.getInfo());
        for(int i=0;i<item.getSubItems().length;i++){
            System.out.println(item.getSubItems()[i].getInfo());
        }
    }
}

以上是基本功,以後開發都要基於此步驟。

3、雙向一對多映射

【舉例】:用戶-課程-考試成績

要求:

  • 根據數據表結構進行簡單Java類轉換;
  • 實現如下的信息輸出:

           --根據課程取得全部參與該課程的用戶信息及考試成績;

           --用戶可取得自己參加的所有課程信息及考試成績;

【關係分析】:一個用戶可參加多個課程,每個課程可以有多個用戶參加,每個用戶對於每個課程都會有成績,此時最麻煩的是用戶課程關係表中除了關聯字段外,還有其他字段,這樣的表一個要單獨定義成一個實體類,所以,以上需要三個類。

第一步:完成基本字段

class User{
    private int uid;
    private String name;

    public User(int uid, String name) {
        this.uid = uid;
        this.name = name;
    }

    public String getInfo(){
        return "用戶編號:"+this.uid+",姓名:"+this.name;
    }
}

class Course{
    private int cid;
    private String title;
    private int num;
    private String note;

    public Course(int cid, String title, int num, String note) {
        this.cid = cid;
        this.title = title;
        this.num = num;
        this.note = note;
    }
    public String getInfo(){
        return "課程編號:"+this.cid+",名稱:"+this.title +",課時:"+this.num+",簡介:"+this.note;
    }
}

第二步:進行字段關聯,一般以外鍵爲主;

爲了進行關聯,需要引入一個新的類:要保存用戶、課程等信息的聯繫;

class User{
    private int uid;
    private String name;
    private UserCourse ucs[];

    public User(int uid, String name) {
        this.uid = uid;
        this.name = name;
    }

    public String getInfo(){
        return "用戶編號:"+this.uid+",姓名:"+this.name;
    }

    public UserCourse[] getUcs() {
        return ucs;
    }

    public void setUcs(UserCourse[] ucs) {
        this.ucs = ucs;
    }
}

class Course{
    private int cid;
    private String title;
    private int num;
    private String note;
    private UserCourse ucs[];
    public Course(int cid, String title, int num, String note) {
        this.cid = cid;
        this.title = title;
        this.num = num;
        this.note = note;
    }
    public String getInfo(){
        return "課程編號:"+this.cid+",名稱:"+this.title +",課時:"+this.num+",簡介:"+this.note;
    }

    public UserCourse[] getUcs() {
        return ucs;
    }

    public void setUcs(UserCourse[] ucs) {
        this.ucs = ucs;
    }
}

class UserCourse{
    private User user;
    private Course course;
    private String note;
    private double score;

    public UserCourse(User user, Course course, String note, double score) {
        this.user = user;
        this.course = course;
        this.note = note;
        this.score = score;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }
}

第三步:測試程序

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //第一步:設置類與類之間的關係
        //1、定義單獨類對象
        User userA = new User(100,"張三");
        User userB = new User(101,"李四");
        User userC = new User(102,"王五");
        Course courseA = new Course(1,"Oracle",10,"-");
        Course courseB = new Course(2,"Java",50,"-");
        //2、設置關聯
        UserCourse uca = new UserCourse(userA,courseA,"無評價",100.0);
        UserCourse ucb = new UserCourse(userA,courseB,"無評價",90.0);
        UserCourse ucc = new UserCourse(userB,courseA,"無評價",99.0);
        UserCourse ucd = new UserCourse(userC,courseA,"無評價",79.0);
        UserCourse uce = new UserCourse(userC,courseB,"無評價",77.0);
        //3、在用戶中設置關係
        userA.setUcs(new UserCourse[]{uca,ucb});
        userB.setUcs(new UserCourse[]{ucc});
        userC.setUcs(new UserCourse[]{ucd,uce});
        courseA.setUcs(new UserCourse[]{uca,ucc,ucd});
        courseB.setUcs(new UserCourse[]{ucb,uce});
        //第二部:獲取數據
        //輸出課程信息
        System.out.println(courseA.getInfo());
        System.out.println(courseB.getInfo());
        //輸出參與該課程的用戶信息
        for(int i=0;i<courseA.getUcs().length;i++){
            System.out.println("參與課程的用戶信息:"+courseA.getUcs()[i].getUser().getInfo()+",考試成績:"+courseA.getUcs()[i].getScore());
        }
        //輸出用戶信息及參與的所有課程
        System.out.println(userA.getInfo());
        for(int i=0;i<userA.getUcs().length;i++){
            System.out.println("參與課程:"+userA.getUcs()[i].getCourse().getInfo()+",考試成績:"+userA.getUcs()[i].getScore());
        }
    }
}

與上一個程序相比,唯一麻煩的是中間關係表上有其他字段,代碼鏈是本次程序的重點所在。

4、多對多數據映射

【舉例】:權限-權限組-用戶-角色-角色權限組

要求:

  • 1、將數據還原爲簡單Java類;
  • 2、數據輸出:

           --根據一個用戶,輸出其對應的角色以及每個角色對應的權限,以及包含的具體的權限詳情;

           --一個權限可以輸出具備此權限的角色,以及具備此角色的所有管理員,同時輸出該權限的所有權限詳情;

           --一個角色可以輸出它所包含的管理員,每個管理員對應的具體權限,以及權限詳情;

【第一步】:數據錶轉換爲簡答Java類

//用戶
class User{
    private String userid;
    private String name;
    private String password;

    public User(String userid, String name, String password) {
        this.userid = userid;
        this.name = name;
        this.password = password;
    }

    public String getInfo(){
        return  "用戶ID:"+this.userid+",姓名:"+this.name+",密碼"+this.password;
    }
}
//角色
class Role{
    private int rid;
    private String title;

    public Role(int rid, String title) {
        this.rid = rid;
        this.title = title;
    }

    public String getInfo(){
        return "角色編號:"+this.rid+",名稱:"+this.title;
    }
}
//權限組
class Group{
    private int gid;
    private String title;

    public Group(int gid, String title) {
        this.gid = gid;
        this.title = title;
    }

    public String getInfo(){
        return "權限組編號:"+this.gid+",組名稱:"+this.title;
    }

}
//權限
class Action{
    private int aid;
    private String title;
    private String url;

    public Action(int aid, String title, String url) {
        this.aid = aid;
        this.title = title;
        this.url = url;
    }

    public String getInfo(){
        return "權限編號:"+this.aid+",權限名稱:"+this.title+",權限路徑:"+this.url;
    }
}

第二步:設置關係

  • 一個角色包含多個用戶,一對多關係;
  • 一個權限組包含多個權限,一對多關係;
  • 一個角色對應有多個權限組,每個權限組可能有多個角色,多對多關係;
//用戶
class User{
    private String userid;
    private String name;
    private String password;
    private Role role;
    public User(String userid, String name, String password) {
        this.userid = userid;
        this.name = name;
        this.password = password;
    }

    public String getInfo(){
        return  "用戶ID:"+this.userid+",姓名:"+this.name+",密碼"+this.password;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }
}
//角色
class Role{
    private int rid;
    private String title;
    private User users[];
    private Group groups[];

    public Role(int rid, String title) {
        this.rid = rid;
        this.title = title;
    }

    public String getInfo(){
        return "角色編號:"+this.rid+",名稱:"+this.title;
    }

    public User[] getUsers() {
        return users;
    }

    public void setUsers(User[] users) {
        this.users = users;
    }

    public Group[] getGroups() {
        return groups;
    }

    public void setGroups(Group[] groups) {
        this.groups = groups;
    }
}
//權限組
class Group{
    private int gid;
    private String title;
    private Action actions[];
    private Role roles[];
    public Group(int gid, String title) {
        this.gid = gid;
        this.title = title;
    }

    public String getInfo(){
        return "權限組編號:"+this.gid+",組名稱:"+this.title;
    }

    public Action[] getActions() {
        return actions;
    }

    public void setActions(Action[] actions) {
        this.actions = actions;
    }

    public Role[] getRoles() {
        return roles;
    }

    public void setRoles(Role[] roles) {
        this.roles = roles;
    }
}
//權限
class Action{
    private int aid;
    private String title;
    private String url;
    private Group group;

    public Action(int aid, String title, String url) {
        this.aid = aid;
        this.title = title;
        this.url = url;
    }

    public String getInfo(){
        return "權限編號:"+this.aid+",權限名稱:"+this.title+",權限路徑:"+this.url;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }
}

【第三步】:定義實例對象,測試程序

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //第一步:根據表結構設置關係
        //1、定義單獨類對象
        User ua = new User("user-a","用戶A","1111");
        User ub = new User("user-b","用戶B","1111");
        User uc = new User("user-c","用戶C","1111");
        //2、定義權限
        Action act1 = new Action(1,"新聞管理","www....");
        Action act2 = new Action(2,"用戶管理","www....");
        Action act3 = new Action(3,"備份管理","www....");
        Action act4 = new Action(4,"緩存管理","www....");
        Action act5 = new Action(5,"數據管理","www....");
        //3、定義權限組
        Group g1 = new Group(1,"數據管理");
        Group g2 = new Group(2,"人事管理");
        Group g3 = new Group(3,"信息管理");
        //4、定義角色信息
        Role r1 = new Role(10,"超級管理員角色");
        Role r2 = new Role(10,"普通管理員角色");
        //5、設置權限組與權限的關係,一對多
        act1.setGroup(g1);
        act2.setGroup(g1);
        act3.setGroup(g2);
        act4.setGroup(g2);
        act5.setGroup(g3);
        g1.setActions(new Action[]{act1,act2});
        g2.setActions(new Action[]{act3,act4});
        g3.setActions(new Action[]{act5});
        //6、權限組與角色的關係
        r1.setGroups(new Group[]{g1,g2,g3});
        r2.setGroups(new Group[]{g2,g3});
        g1.setRoles(new Role[]{r1});
        g2.setRoles(new Role[]{r1,r2});
        g3.setRoles(new Role[]{r1,r2});
        //7、定義用戶與角色關係
        ua.setRole(r1);
        ub.setRole(r2);
        uc.setRole(r2);
        r1.setUsers(new User[]{ua});
        r2.setUsers(new User[]{ub,uc});
    }
}

【第四步】:輸出信息

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //第一步:根據表結構設置關係
        //1、定義單獨類對象
        User ua = new User("user-a","用戶A","1111");
        User ub = new User("user-b","用戶B","1111");
        User uc = new User("user-c","用戶C","1111");
        //2、定義權限
        Action act1 = new Action(1,"新聞管理","www....");
        Action act2 = new Action(2,"用戶管理","www....");
        Action act3 = new Action(3,"備份管理","www....");
        Action act4 = new Action(4,"緩存管理","www....");
        Action act5 = new Action(5,"數據管理","www....");
        //3、定義權限組
        Group g1 = new Group(1,"數據管理");
        Group g2 = new Group(2,"人事管理");
        Group g3 = new Group(3,"信息管理");
        //4、定義角色信息
        Role r1 = new Role(10,"超級管理員角色");
        Role r2 = new Role(10,"普通管理員角色");
        //5、設置權限組與權限的關係,一對多
        act1.setGroup(g1);
        act2.setGroup(g1);
        act3.setGroup(g2);
        act4.setGroup(g2);
        act5.setGroup(g3);
        g1.setActions(new Action[]{act1,act2});
        g2.setActions(new Action[]{act3,act4});
        g3.setActions(new Action[]{act5});
        //6、權限組與角色的關係
        r1.setGroups(new Group[]{g1,g2,g3});
        r2.setGroups(new Group[]{g2,g3});
        g1.setRoles(new Role[]{r1});
        g2.setRoles(new Role[]{r1,r2});
        g3.setRoles(new Role[]{r1,r2});
        //7、定義用戶與角色關係
        ua.setRole(r1);
        ub.setRole(r2);
        uc.setRole(r2);
        r1.setUsers(new User[]{ua});
        r2.setUsers(new User[]{ub,uc});

        //第二步:取出數據
        //根據一個用戶,輸出其對應的角色以及每個角色對應的權限,以及包含的具體的權限詳情;
        System.out.println(ua.getInfo());
        System.out.println("角色:"+ua.getRole().getInfo());
        for(int x=0;x<ua.getRole().getGroups().length;x++){
            System.out.println("權限組:"+ua.getRole().getGroups()[x].getInfo());
            for(int y=0;y<ua.getRole().getGroups()[x].getActions().length;y++){
                System.out.println("權限:"+ua.getRole().getGroups()[x].getActions()[y].getInfo());
            }
        }
        System.out.println("========================================");
        //一個權限可以輸出具備此權限的角色,以及具備此角色的所有管理員,同時輸出該權限的所有權限詳情;
        System.out.println(act1.getInfo());
        for(int x=0;x<act1.getGroup().getRoles().length;x++){
            System.out.println("角色:"+act1.getGroup().getRoles()[x].getInfo());
            for(int y=0;y<act1.getGroup().getRoles()[x].getUsers().length;y++){
                System.out.println("用戶"+act1.getGroup().getRoles()[x].getUsers()[y].getInfo());
            }
        }
        System.out.println("========================================");
        //一個角色可以輸出它所包含的管理員,每個管理員對應的具體權限,以及權限詳情;
        System.out.println(r1.getInfo());
        for(int x=0;x<r1.getUsers().length;x++){
            System.out.println("用戶:"+r1.getUsers()[x].getInfo());
            for(int y=0;y<r1.getGroups().length;y++){
                System.out.println("權限組:"+r1.getGroups()[y].getInfo());
                for(int z=0;z<r1.getGroups()[y].getActions().length;z++){
                    System.out.println("權限:"+r1.getGroups()[y].getActions()[z].getInfo());
                }
            }
        }

    }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章