阿里云【名师课堂】Java面向对象开发43:【第03个代码模型】综合案例:数据表与简单Java类(角色与权限)

阿里云【名师课堂】Java面向对象开发43:【第03个代码模型】综合案例:数据表与简单Java类(角色与权限)

角色与权限

在这里插入图片描述

  • 要求可以根据一个员工找到他所对应的部门,以及该部门对应的角色,以及每个角色对应的所有权限;
  • 可以根据一个角色找到具备此角色的所有部门,以及该部门下的所有员工;
  • 根据—个权限列出具备有该权限的所有的角色以及每一个角色下对应的所有部门,以及每个部门中的所有员工。

1、先将所有的基础字段转化为类,暂时不考虑所有的关系

class Department{  // 部门信息
	private int did ;  // 部门编号
	private String dname ;  // 部门名称
	
	public Department(int did,String dname) {
		this.did = did ;
		this.dname = dname ;
	}
	
	public String getInfo() {
		return "【DEPARTMENT】id = " + this.did + ",name = " + this.dname ;
	}
}

class Employee{  // 员工信息
	private int eid ;  // 员工工号
	private String ename ;  // 员工姓名
	
	public Employee(int eid,String ename) {
		this.eid = eid ;
		this.ename = ename ;
	}
	
	public String getInfo() {
		return "【EMPLOYEE】id = " + this.eid + ",name = " + this.ename ;
	}
}

class Role{  // 角色信息
	private int rid ;
	private String title ;
	
	public Role(int rid,String title) {
		this.rid = rid ;
		this.title = title ;
	}
	
	public String getInfo() {
		return "【ROLE】id = " + this.rid + ",title = " + this.title ;
	}
	
}

class Authority{  // 权限
	private int aid ;
	private String title ;
	private String flag ;
	
	public Authority(int aid,String title,String flag) {
		this.aid = aid ;
		this.title = title ;
		this.flag = flag ;
	}
	
	public String getInfo() {
		return "【AUTHORITY】id = " + this.aid + ",title = " + this.title + ",flag = " + this.flag ;
	}
}

2、进行关系描述,列出数据表中对应的关系

注意:

  • 角色_权限表里两个字段全是外键字段(角色ID是角色的外键字段,权限ID是权限的外键字段)
  • 而不像上节课讲解的学生选课表中除了两个外键字段还有一个成绩字段
  • 因此这里不需要再为角色_权限表单独建类

Department类中添加:

	private Employee [] emps ;  // 一个部门有多个雇员
	private Role role ;  // 一个部门属于一个角色
	
	public void setEmps(Employee [] emps){
		this.emps = emps ;
	}
	public Employee [] getEmps(){
		return this.emps ;
	}
	public void setRole(Role role){
		this.role = role ;
	}
	public Role getRole(){
		return this.role ;
	}

Employee类中添加:

	private Department dept ;  // 一个雇员属于一个部门
	
	public void setDept(Department dept){
		this.dept = dept ;
	}
	public Department getDept(){
		return this.dept ;
	}

Role类中添加:

	private Department [] depts ;  // 多个部门具备此角色
	private Authority [] auth ;  // 一个部门有多个权限
	
	public void setDepts(Department [] depts){
		this.depts = depts ;
	}
	public Department [] getDepts(){
		return this.depts ;
	}
	public void setAuth(Authority [] auth){
		this.auth = auth ;
	}
	public Authority [] getAuth(){
		return this.auth ;
	}

3、根据开发需求设计

根据关系进行测试数据的编写,完成指定的输出。

  • 要求可以根据一个员工找到他所对应的部门, 以及该部门对应的角色, 以及每个角色对应的所有权限;
  • 可以根据一个角色找到具备此角色的所有部门, 以及该部门下的所有员工;
  • 根据一个权限列出具备有该权限的所有的角色以及每一个角色下对应的所有部门, 以及每个部门中的所有员工。
class Department{  // 部门信息
	private int did ;  // 部门编号
	private String dname ;  // 部门名称
	private Employee [] emps ;  // 一个部门有多个雇员
	private Role role ;  // 一个部门属于一个角色
	
	public Department(int did,String dname) {
		this.did = did ;
		this.dname = dname ;
	}
	
	public void setEmps(Employee [] emps){
		this.emps = emps ;
	}
	public Employee [] getEmps(){
		return this.emps ;
	}
	public void setRole(Role role){
		this.role = role ;
	}
	public Role getRole(){
		return this.role ;
	}
	public String getInfo() {
		return "【DEPARTMENT】id = " + this.did + ",name = " + this.dname ;
	}
}

class Employee{  // 员工信息
	private int eid ;  // 员工工号
	private String ename ;  // 员工姓名
	private Department dept ;  // 一个雇员属于一个部门
	
	public Employee(int eid,String ename) {
		this.eid = eid ;
		this.ename = ename ;
	}
	
	public void setDept(Department dept){
		this.dept = dept ;
	}
	public Department getDept(){
		return this.dept ;
	}
	public String getInfo() {
		return "【EMPLOYEE】id = " + this.eid + ",name = " + this.ename ;
	}
}

class Role{  // 角色信息
	private int rid ;
	private String title ;
	private Department [] depts ;  // 多个部门具备此角色
	private Authority [] auth ;  // 一个部门有多个权限
	
	public Role(int rid,String title) {
		this.rid = rid ;
		this.title = title ;
	}
	
	public void setDepts(Department [] depts){
		this.depts = depts ;
	}
	public Department [] getDepts(){
		return this.depts ;
	}
	public void setAuth(Authority [] auth){
		this.auth = auth ;
	}
	public Authority [] getAuth(){
		return this.auth ;
	}
	public String getInfo() {
		return "【ROLE】id = " + this.rid + ",title = " + this.title ;
	}
	
}

class Authority{  // 权限
	private int aid ;
	private String title ;
	private String flag ;
	private Role [] role ;  // 多个角色可以有同一个权限
	
	public Authority(int aid,String title,String flag) {
		this.aid = aid ;
		this.title = title ;
		this.flag = flag ;
	}
	
	public void setRoles(Role [] role){
		this.role = role ;
	}
	public Role [] getRoles(){
		return this.role ;
	}
	public String getInfo() {
		return "【AUTHORITY】id = " + this.aid + ",title = " + this.title + ",flag = " + this.flag ;
	}
}

public class EmpDeptRoleAuth {  // 设置开发需求
	public static void main(String args[]) {
		// 第一步:设置数据间的关系
		
		// 1、分别创建各类的实例化对象
		Department da = new Department(10,"ACCOUNTING") ;
		Department db = new Department(13,"HR") ;
		Department dc = new Department(16,"Directors") ;
		Employee ea = new Employee(4396,"Dexter") ;
		Employee eb = new Employee(4728,"Tsukishima Kei") ;
		Employee ec = new Employee(1230,"Toono Takaki") ;
		Employee ed = new Employee(4321,"Norwegian") ;
		Employee ee = new Employee(4269,"Friday") ;
		Role ra = new Role(101,"Manager") ;
		Role rb = new Role(107,"Staff") ;
		Authority aa = new Authority(9901,"雇员入职","employee:entry") ;
		Authority ab = new Authority(9902,"雇员晋升","employee:boost") ;
		Authority ac = new Authority(9903,"发布公告","employee:announcey") ;
		Authority ad = new Authority(9904,"查看客户信息","customer:display") ;
		Authority ae = new Authority(9905,"回访记录","customer:record") ;
		
		// 2、设置对象间的引用关系
		// 2.1 设置角色与权限的关系
		ra.setAuth(new Authority [] {aa,ab,ac,ad,ae}) ;  // ra的所有权限
		rb.setAuth(new Authority [] {ad,ae}) ;  // rb的所有权限
		// 2.2 设置权限与角色的关系
		aa.setRoles(new Role [] {ra}) ;
		ab.setRoles(new Role [] {ra}) ;
		ac.setRoles(new Role [] {ra}) ;
		ad.setRoles(new Role [] {ra,rb}) ;
		ae.setRoles(new Role [] {ra,rb}) ;
		// 2.3 设置部门与角色的关系
		da.setRole(rb) ;
		db.setRole(ra) ;
		dc.setRole(ra) ;
		// 2.4 设置角色与部门的关系
		ra.setDepts(new Department [] {db,dc}) ;
		rb.setDepts(new Department [] {da}) ;
		// 2.5 设置雇员与部门的关系
		ea.setDept(da) ;
		eb.setDept(db) ;
		ec.setDept(da) ;
		ed.setDept(db) ;
		ee.setDept(dc) ;
		// 2.6 设置部门与雇员的关系
		da.setEmps(new Employee [] {ea,ec}) ;
		db.setEmps(new Employee [] {eb,ed}) ;
		dc.setEmps(new Employee [] {ee}) ;
		
		// 第二步:根据关系取出数据
		
		// 3、要求可以根据一个员工找到他所对应的部门,以及该部门对应的角色,以及每个角色对应的所有权限
		System.out.println("1、员工找部门,部门对应角色,角色的权限") ;
		System.out.println(ea.getInfo()) ;  // 输出雇员的信息
		System.out.println("\t##" + ea.getDept().getInfo()) ;
		System.out.println("\t\t@@" + ea.getDept().getRole().getInfo()) ;
		for(int x = 0 ; x < ea.getDept().getRole().getAuth().length ; x++){
			System.out.println("\t\t\t$$" + ea.getDept().getRole().getAuth()[x].getInfo()) ;
		}
		System.out.println("===============================================================") ;
		
		// 4、可以根据一个角色找到具备此角色的所有部门,以及该部门下的所有员工;
		System.out.println("2、角色找部门,部门下员工") ;
		System.out.println(ra.getInfo()) ;  // 输出部门的信息
		for(int x = 0 ; x < ra.getDepts().length ; x++){
			System.out.println("\t##" + ra.getDepts()[x].getInfo()) ;
			for(int y = 0 ; y < ra.getDepts()[x].getEmps().length ; y++){  // 注意是.getDepts()[x].
				System.out.println("\t\t@@" + ra.getDepts()[x].getEmps()[y].getInfo()) ;
			}
		}
		System.out.println("===============================================================") ;
		
		// 5、根据一个权限列出具备有该权限的所有的角色以及每一个角色下对应的所有部门,以及每个部门中的所有员工
		System.out.println("3、权限找角色,角色下部门,部门下员工") ;
		System.out.println(aa.getInfo()) ;  // 输出权限的信息
		for(int x = 0 ; x < aa.getRoles().length ; x++){
			System.out.println("\t##" + aa.getRoles()[x].getInfo()) ;
			for(int y = 0 ; y < aa.getRoles()[x].getDepts().length ; y++){
				System.out.println("\t\t@@" + aa.getRoles()[x].getDepts()[y].getInfo()) ;
				for(int z = 0 ; z < aa.getRoles()[x].getDepts()[y].getEmps().length ; z++){  // 注意是.getDepts()[x].
					System.out.println("\t\t\t$$" + aa.getRoles()[x].getDepts()[y].getEmps()[z].getInfo()) ;
				}
			}
		}
	}
}

在这里插入图片描述
本程序中包含了基本上可能用到的所有复杂逻辑。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值