1. 程式人生 > >菜雞的Java課筆記 多對多對映

菜雞的Java課筆記 多對多對映

要求:
    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;
    }
    public String getlnfo(){
        return     "使用者ID:"+this
.userid +",姓名:"+this.name +",密碼:"+this.password; } } class Role{ // 角色 private int rid; private String title; public User(int rid,String title){ this.rid = rid; this.title = title; } public String getlnfo(){ return "角色編號:"+this
.rid +",名稱:"+this.title; } } class Group{ // 許可權 private int gid; private String title; public User(int gid,String title){ this.gid = gid; this.title = title; } public String getlnfo(){ return "許可權組編號:"+this.gid +",組名稱:"+this.title; } } classAction{// 許可權 private int aid; private String title; private String url; public User(int aid,String title,String url){ this.rid = aid; this.title = title; this.url = url; } public String getlnfo(){ return "許可權編號:"+this.aid +",許可權名稱:"+this.title +",路徑"+this.url; } } public class manyToManyMapping{ public static void main(String args[]){ } }

   
    第二步:設定關係
        一個角色包含有多個使用者,一對多關係
        一個許可權組包含多個許可權,屬於一對多
        一個角色對應有多個許可權組,每個許可權組可能有多個角色,多對多

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 void setRole(Role role){
        this.role = role;
    }
    public Role getRole(){
        return this.role;
    }
    public String getlnfo(){
        return     "使用者ID:"+this.userid
                +",姓名:"+this.name
                +",密碼:"+this.password;
    }
}
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 void setGroups(Group groups[]){
        this.groups = groups;
    }
    public Group[] getGroups(){
        return this.groups;
    }
    public void setUser(User users[]){
        this.users = users;
    }
    public User[] getUser(){
        return this.users;
    }
    public String getlnfo(){
        return     "角色編號:"+this.rid
                +",名稱:"+this.title;
    }
}
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 void setRoles(Role roles[]){
        this.roles = roles;
    }
    public Role[] getRoles(){
        return this.roles;
    }
    public void setActions(Action actions[]){
        this.actions = actions;
    }
    public Action[] getActions(){
        return this.actions;
    }
    public String getlnfo(){
        return     "許可權組編號:"+this.gid
                +",組名稱:"+this.title;
    }
}
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 void setGroup(Group group){
        this.group = group;
    }
    public Group getGroup(){
        return this.group;
    }
    public String getlnfo(){
        return     "許可權編號:"+this.aid
                +",許可權名稱:"+this.title
                +",路徑"+this.url;
    }
}
public class manyToManyMapping{
    public static void main(String args[]){
        
    }
    
}

        分殺完畢。迅速,一定是不需要使用腦子就直接寫出來的
    第三步:設定關係

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 void setRole(Role role){
        this.role = role;
    }
    public Role getRole(){
        return this.role;
    }
    public String getlnfo(){
        return     "使用者ID:"+this.userid
                +",姓名:"+this.name
                +",密碼:"+this.password;
    }
}
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 void setGroups(Group groups[]){
        this.groups = groups;
    }
    public Group[] getGroups(){
        return this.groups;
    }
    public void setUsers(User users[]){
        this.users = users;
    }
    public User[] getUsers(){
        return this.users;
    }
    public String getlnfo(){
        return     "角色編號:"+this.rid
                +",名稱:"+this.title;
    }
}
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 void setRoles(Role roles[]){
        this.roles = roles;
    }
    public Role[] getRoles(){
        return this.roles;
    }
    public void setActions(Action actions[]){
        this.actions = actions;
    }
    public Action[] getActions(){
        return this.actions;
    }
    public String getlnfo(){
        return     "許可權組編號:"+this.gid
                +",組名稱:"+this.title;
    }
}
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 void setGroup(Group group){
        this.group = group;
    }
    public Group getGroup(){
        return this.group;
    }
    public String getlnfo(){
        return     "許可權編號:"+this.aid
                +",許可權名稱:"+this.title
                +",路徑"+this.url;
    }
}
public class manyToManyMapping{
    public static void main(String args[]){
        //第一步:根據表結構設定關係
        //1.定義單獨的類物件
        User ua = new User("user-a","使用者A","hello");
        User ub = new User("user-b","使用者B","hello");
        User uc = new User("user-c","使用者C","hello");
        //2.定義許可權
        Action act1 = new Action(1,"新聞管理","naws");
        Action act2 = new Action(1,"使用者管理","users");
        Action act3 = new Action(1,"備份管理","bks");
        Action act4 = new Action(1,"快取管理","caches");
        Action act5 = new Action(1,"資料管理","datas");
        //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});
        r1.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.getlnfo());
        System.out.println("\t|-【角色】"+ua.getRole().getlnfo());
        for(int x = 0;x<ua.getRole().getGroups().length;x++){
            System.out.println("\t\t|-【許可權組】"+ua.getRole().getGroups()[x].getlnfo());
            for(int y = 0;y<ua.getRole().getGroups()[x].getActions().length;y++){
                System.out.println("\t\t\t|-【許可權】"+ua.getRole().getGroups()[x].getActions()[y].getlnfo());
            }
        }
        System.out.println("***************************");
        //一個許可權可以輸出具備此許可權的角色,以及具備此角色的所有管理員,同時輸出該許可權的所有許可權詳情
        System.out.println(act1.getlnfo());
        //通過許可權找到許可權對應的許可權組,一個許可權組有多個角色
        for(int x = 0;x<act1.getGroup().getRoles().length;x++){
            System.out.println("\t\t|-【角色】 "+act1.getGroup().getRoles()[x].getlnfo());
            for(int y = 0;y<act1.getGroup().getRoles()[x].getUsers().length;y++){
                System.out.println("\t\t\t|-【使用者】 "+act1.getGroup().getRoles()[x].getUsers()[y].getlnfo());
            }
        }
        System.out.println("***************************");
        //一個角色可以輸出它所包含的管理員,每個管理員對應的具體的許可權,以及許可權詳情
        System.out.println(r1.getlnfo());
        for(int x = 0;x<r1.getUsers().length;x++){
            System.out.println("\t|-【使用者】 "+r1.getUsers()[x].getlnfo());
            for(int y = 0;y<r1.getGroups().length;y++){
                System.out.println("\t\t|-【許可權組】 "+r1.getGroups()[y].getlnfo());
                for(int z = 0;z<r1.getGroups()[y].getActions().length;z++){
                    System.out.println("\t\t\t|-【許可權】 "+r1.getGroups()[y].getActions()[z].getlnfo());
                }
            }
        }
    }
}

 


        
        引用,引用,引用 就是java基礎的核心
    
    總結
        這種轉換必須做到零用腦編寫