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 role;
}
public String getInfo(){
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 setUser(User users[]){
this.users = users;
}
public void setGroups(Group[] groups) {
this.groups = groups;
}
public User[] getUser(){
return users;
}
public Group[] getGroups() {
return this.groups;
}
public String getInfo(){
return "角色id"+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 setActions(Action actions[]){
this.actions = actions;
}
public void setRoles(Role[] roles) {
this.roles = roles;
}
public Action[] getActions() {
return this.actions;
}
public Role[] getRoles() {
return this.roles;
}
public String getInfo(){
return "权限组id"+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 getInfo(){
return "权限id"+this.aid+"权限名"+this.title+"权限路径"+this.url;
}
}
public class JavaDemo {
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,"新闻管理","http://");
Action act2 = new Action(2,"用户管理","http://");
Action act3 = new Action(3,"备份管理","http://");
Action act4 = new Action(4,"缓存管理","http://");
//3.定义权限组信息
Group g1 = new Group(1,"管理员");
Group g2 = new Group(1,"信息管理员");
//4.定义角色信息
Role r1 = new Role(10,"超级管理员角色");
Role r2 = new Role(20,"信息管理员角色");
//5 设置权限组与权限的关系,一对多关系
act1.setGroup(g1);//权限与权限组的关系
act2.setGroup(g1);
act3.setGroup(g2);
act4.setGroup(g2);
g1.setActions(new Action[]{act1,act2});
g2.setActions(new Action[]{act3,act4});
//6.权限组与角色关系
r1.setGroups(new Group[]{g1,g2});
r2.setGroups(new Group[]{g2});
g1.setRoles(new Role[]{r1});
g2.setRoles(new Role[]{r1,r2});
//7.定义用户与角色的关系
ua.setRole(r1);
ub.setRole(r1);
ub.setRole(r2);
r1.setUser(new User[]{ua,ub});
r2.setUser(new User[]{ub});
System.out.println(ua.getInfo());
System.out.println("\t[角色]"+ua.getRole().getInfo());
//一个权限可以输出具备此权限的角色
System.out.println(act1.getInfo());
for (int x =0;x<act1.getGroup().getRoles().length;x++){
System.out.println("\t\t-【角色】"+act1.getGroup().getRoles()[x].getInfo());
for (int y=0;y<act1.getGroup().getRoles()[x].getUser().length;y++){
System.out.println("\t\t\t-【用户】"+act1.getGroup().getRoles()[x].getUser()[y].getInfo());
}
}
}
}