MENU

数据表与简单java映射转换(二)

• November 14, 2020 • 面向对象编程

数据表与简单java映射转换

要求实现如下查询功能

  1. 可以根据一个用户找到该用户对应的所有角色,以及每一个角色对应的所有权限信息
  2. 可以根据一个角色找到该角色下的所有权限,以及拥有此角色的全部用户信息
  3. 可以根据一个权限找到具备有此权限的所有用户信息

代码实现步骤

一、根据数据表先定义简单java类
二、根据数据表的关联关系对简单java类进行关系设置
三、在主方法中根据结构设置对象数据
四、根据要求获取数据


源代码

class Member{
    private long id;
    private String name;
    private Role roles[];
    
    public Role[] getRoles() {
        return roles;
    }
    public void setRoles(Role[] roles) {
        this.roles = roles;
    }
    public Member(long id, String name) {
        this.id = id;
        this.name = name;
    }
    public String getInfo(){
        return "【用户信息】编号:"+this.id+"   姓名:"+this.name;
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

class Role{
    private long id;
    private String title;
    private Member members[];
    private Privilege privileges[];
    
    public Role(long id, String title) {
        this.id = id;
        this.title = title;
    }
    public Member[] getMembers() {
        return members;
    }
    public void setMembers(Member[] members) {
        this.members = members;
    }
    public Privilege[] getPrivileges() {
        return privileges;
    }
    public void setPrivileges(Privilege[] privileges) {
        this.privileges = privileges;
    }
    public String getInfo(){
        return "【角色信息】编号:"+this.id+"   名称:"+this.title;
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
}

class  Privilege{
    private long id;
    private String title;
    private Role role;
    
    public Privilege(long id, String title) {
        this.id = id;
        this.title = title;
    }
    public String getInfo(){
        return "【权限信息】编号:"+this.id+"   名称:"+this.title;
    }
    public Role getRole() {
        return role;
    }
    public void setRole(Role role) {
        this.role = role;
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
}

public class Main {
    public static void main(String[] args) {
        //三、根据结构设置对象数据
        Member memberA = new Member(10101L,"张三");
        Member memberB = new Member(10102L,"李四");
        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.setPrivileges(new Privilege[]{privilegeA,privilegeB,privilegeC});
        roleB.setPrivileges(new Privilege[]{privilegeD,privilegeE,privilegeF});
        roleC.setPrivileges(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,roleB,roleC});
        //增加角色与用户的对应关系
        roleA.setMembers(new Member[]{memberA,memberB});
        roleB.setMembers(new Member[]{memberA,memberB});
        roleC.setMembers(new Member[]{memberB});
        //四、根据要求获取数据
        //1. 根据一个用户找到该用户对应的所有角色,以及每一个角色对应的所有权限信息
        System.out.println("\n\n----------通过用户查找信息------------");
        System.out.println(memberA.getInfo());
        for (int i = 0; i < memberA.getRoles().length; i++) {
            System.out.println("\t|-"+memberA.getRoles()[i].getInfo());
            for (int j = 0; j < memberA.getRoles()[i].getPrivileges().length; j++) {
                System.out.println("\t\t|-"+memberA.getRoles()[i].getPrivileges()[j].getInfo());
            }
        }
        //2. 根据一个角色找到该角色下的所有权限,以及拥有此角色的全部用户信息
        System.out.println("\n\n----------通过角色查找信息------------");
        System.out.println(roleB.getInfo());
        System.out.println("\t|-"+"此角色下的所有权限信息:");
        for (int i = 0; i < roleB.getPrivileges().length; i++) {
            System.out.println("\t\t|-"+roleB.getPrivileges()[i].getInfo());
        }
        System.out.println("\t|-"+"此角色下的所有用户信息:");
        for (int i = 0; i < roleB.getMembers().length; i++) {
            System.out.println(roleB.getMembers()[i].getInfo());
        }
        //3. 可以根据一个权限找到具备有此权限的所有用户信息
        System.out.println("\n\n----------通过权限查找信息------------");
        System.out.println(privilegeA.getInfo());
        for (int i = 0; i < privilegeA.getRole().getMembers().length; i++) {
            System.out.println(privilegeA.getRole().getMembers()[i].getInfo());
        }
    }
}

运行结果:


代码编译器:IntelliJ IDEA 2020.02

Archives QR Code Tip
QR Code for this page
Tipping QR Code