OA权限模块实体映射和数据初始化

    OA权限系统再一次回归了之前做过的权限系统,对于权限系统的业务逻辑有了更加深一步的认识,这篇博文就介绍一下OA系统中权限系统实体映射和数据初始化。


    一、实体映射


    先看实体之间的类图:

          

    总类图中我们可以清晰的看出,用户和角色,角色和权限都是多对多的关系,并且权限和自己有一对多的关系,下面我们就一块来根据类图来把实体的代码和映射文件写一下。


    1.User实体

package cn.tgb.oa.domain;

import java.util.HashSet;
import java.util.Set;

import cn.tgb.oa.domain.Department;
import cn.tgb.oa.domain.Role;

public class User {
	private Long id;
	private Department department;
	private Set<Role> roles = new HashSet<Role>();

	private String loginName; // 登录名
	private String password; // 密码
	private String name; // 真实姓名
	private String gender; // 性别
	private String phoneNumber; // 电话号码
	private String email; // 电子邮件
	private String description; // 说明
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}
	public Set<Role> getRoles() {
		return roles;
	}
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}
	public String getLoginName() {
		return loginName;
	}
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getPhoneNumber() {
		return phoneNumber;
	}
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}

	
}

    User实体的映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">


<hibernate-mapping package="cn.tgb.oa.domain">

	<class name="User" table="t_user">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="loginName" />
		<property name="password" />
		<property name="name" />
		<property name="gender" />
		<property name="phoneNumber" />
		<property name="email" />
		<property name="description" />
		
		<!-- department属性,本类与Department的多对一 -->
		<many-to-one name="department" class="Department" column="departmentId"></many-to-one>
		
		<!-- roles属性,本类与Role的多对多 -->
		<set name="roles" table="t_user_role">
			<key column="userId"></key>
			<many-to-many class="Role" column="roleId"></many-to-many>
		</set>
	</class>
	
</hibernate-mapping>

    2.角色实体

package cn.tgb.oa.domain;

import java.util.HashSet;
import java.util.Set;

import cn.tgb.oa.domain.Privilege;
import cn.tgb.oa.domain.User;

/*
 * 岗位
 */
public class Role {
	
	private Long id;
	private String name;
	private String description;
	private Set<User> users = new HashSet<User>();
	private Set<Privilege> privileges = new HashSet<Privilege>();

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Set<User> getUsers() {
		return users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Set<Privilege> getPrivileges() {
		return privileges;
	}

	public void setPrivileges(Set<Privilege> privileges) {
		this.privileges = privileges;
	}
		
}

    角色实体映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.tgb.oa.domain">

	<class name="Role" table="t_role">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"></property>
		<property name="description"></property>
		
		<!-- 用户和角色多对多映射 -->
		<set name="users" table="t_user_role">
			<key column="roleId"></key>
			<many-to-many class="User" column="userId"></many-to-many>
		</set>
		
		<!-- 用户和权限多对多映射 -->
		<set name="privileges" table="t_role_privilege">
			<key column="roleId"></key>
			<many-to-many class="Privilege" column="privilegeId"></many-to-many>
		</set>
	</class>
	
</hibernate-mapping>

    3.权限实体

package cn.tgb.oa.domain;

import java.util.HashSet;
import java.util.Set;

/**
 * 权限
 * 
 * @author tyg
 * 
 */
public class Privilege {
	private Long id;
	private String url;
	private String name; // 权限名称
	private Set<Role> roles = new HashSet<Role>();
	private Privilege parent; // 上级权限
	private Set<Privilege> children = new HashSet<Privilege>(); // 下级权限

	public Privilege() {
	}

	public Privilege(String name, String url, Privilege parent) {
		this.name = name;
		this.url = url;
		this.parent = parent;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	public Privilege getParent() {
		return parent;
	}

	public void setParent(Privilege parent) {
		this.parent = parent;
	}

	public Set<Privilege> getChildren() {
		return children;
	}

	public void setChildren(Set<Privilege> children) {
		this.children = children;
	}

}

    权限实体映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.tgb.oa.domain">

	<class name="Privilege" table="t_privilege">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="url"></property>
		<property name="name"></property>
		
		<!-- 与role的多对多关系 -->
		<set name="roles" table="t_role_privilege">
			<key column="privilegeId"></key>
			<many-to-many class="Role" column="roleId"></many-to-many>
		</set>
		
		<!-- 与上级多对一的关系 -->
		<many-to-one name="parent" class="Privilege" column="parentId"></many-to-one>
		
		<!-- 与下级一对多的关系 -->
		<set name="children">
			<key column="parentId"></key>
			<one-to-many class="Privilege"/>
		</set>
	</class>
	
</hibernate-mapping>

    二、初始化数据


    在这里,我自己编写了一个工具类,来初始化权限系统的数据,默认有超级管理员,以及超级管理员对应的所有权限页面,具体代码如下:

package cn.tgb.oa.uitl;

import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import cn.tgb.oa.domain.Privilege;
import cn.tgb.oa.domain.User;


/**
 * 初始化权限
 * @author LUCKY
 *
 */
@Component
public class Installer {
	
	@Resource
	private SessionFactory sessionFactory;
	
	@Transactional
	public void install(){
		
		//1.获得session
		Session session = sessionFactory.getCurrentSession();
		
		//2.保存超级管理员用户
		User user = new User();
		user.setName("超级管理员");
		user.setLoginName("admin");
		user.setPassword("admin");
		session.save(user);//保存
		
		//3.保存权限数据
		Privilege menu,menu1,menu2,menu3;
		
		menu = new Privilege("系统管理",null,null);
		menu1 = new Privilege("岗位管理","/role_list",menu);
		menu2 = new Privilege("部门管理","/department_list",menu);
		menu3 = new Privilege("用户管理","/user_list",menu);
		session.save(menu);
		session.save(menu1);
		session.save(menu2);
		session.save(menu3);
		
		session.save(new Privilege("岗位列表", "/role_list", menu1));
		session.save(new Privilege("岗位删除", "/role_delete", menu1));
		session.save(new Privilege("岗位添加", "/role_add", menu1));
		session.save(new Privilege("岗位修改", "/role_edit", menu1));
		
		session.save(new Privilege("部门列表", "/department_list", menu2));
		session.save(new Privilege("部门删除", "/department_delete", menu2));
		session.save(new Privilege("部门添加", "/department_add", menu2));
		session.save(new Privilege("部门修改", "/department_edit", menu2));
		
		session.save(new Privilege("用户列表", "/user_list", menu3));
		session.save(new Privilege("用户删除", "/user_delete", menu3));
		session.save(new Privilege("用户添加", "/user_add", menu3));
		session.save(new Privilege("用户修改", "/user_edit", menu3));
		session.save(new Privilege("初始化密码", "/user_initPassword", menu3));
		
		session.save(menu);
		session.save(menu1);
		session.save(menu2);
		session.save(menu3);
	}
	
	public static void main(String[] args){
		//执行方法初始化数据
		ApplicationContext aContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Installer installer = (Installer) aContext.getBean("installer");
		installer.install();
	}
}

    三、执行结果


    用户表:

    


    权限表:

    


    至此,OA权限系统的数据初始化完成,下一篇为大家介绍登录和注销的相关功能。



    

    

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值