首注:本学习教程为传智播客汤阳光讲师所公布的免费OA项目视频我的文字版实践笔记,本人用此来加强巩固自己开发知识,如有网友转载,请注明。谢谢。
一 实体设计:
1.权限实体设计:
1)属性设计:
主键:id
关联属性:Set<Role> roles,Set<Privilege> privileges,Privilege parent,Set<Privilege> children
一般属性:name,url
特殊属性:暂无
2)涉及到3个实体:User(用户),Role(岗位角色),Privilege(权限)。其关系映射如下图:
二 javabean,映射文件,表的创建:
1.javabean:Privilege.java
1 package cn.clear.oa.domain; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 /** 6 * 权限实体类 7 * @ClassName: Privilege 8 * @Description: TODO 9 * @author: clear 10 * @date: 2014-10-29 下午4:45:21 11 */ 12 public class Privilege { 13 14 private Long id; 15 private String name; 16 private String url; 17 private Privilege parent; 18 private Set<Role> roles = new HashSet<Role>(); 19 private Set<Privilege> children = new HashSet<Privilege>(); 20 public Long getId() { 21 return id; 22 } 23 public void setId(Long id) { 24 this.id = id; 25 } 26 public String getName() { 27 return name; 28 } 29 public void setName(String name) { 30 this.name = name; 31 } 32 public String getUrl() { 33 return url; 34 } 35 public void setUrl(String url) { 36 this.url = url; 37 } 38 public Set<Role> getRoles() { 39 return roles; 40 } 41 public void setRoles(Set<Role> roles) { 42 this.roles = roles; 43 } 44 public Privilege getParent() { 45 return parent; 46 } 47 public void setParent(Privilege parent) { 48 this.parent = parent; 49 } 50 public Set<Privilege> getChildren() { 51 return children; 52 } 53 public void setChildren(Set<Privilege> children) { 54 this.children = children; 55 } 56 57 }
Privilege.java
2.Privilege.hbm.xml:
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping package="cn.clear.oa.domain"> 7 <class name="Privilege" table="oa_privilege"> 8 <id name="id"> 9 <generator class="native" /> 10 </id> 11 <property name="name" /> 12 <property name="url" /> 13 <!-- roles属性,本类与Role的多对多 --> 14 <set name="roles" table="oa_privilege_role"> 15 <key column="privilegeId"></key> 16 <many-to-many class="Role" column="roleId"></many-to-many> 17 </set> 18 <!-- parent属性,本类与Privilege(上级)的多对一 --> 19 <many-to-one name="parent" class="Privilege" column="parentId"></many-to-one> 20 <!-- children属性,本类与Privilege(下级级)的一对多 --> 21 <set name="children" cascade="delete" order-by="id ASC"> 22 <key column="parentId"></key> 23 <one-to-many class="Privilege"/> 24 </set> 25 </class> 26 </hibernate-mapping>
Privilege.hbm.xml
3.在Role.java中加入和权限的关联属性:
private Set<Privilege> privileges = new HashSet<Privilege>();
//get与set方法
public Set<Privilege> getPrivileges() {
return privileges;
}
public void setPrivileges(Set<Privilege> privileges) {
this.privileges = privileges;
}
4.在Role.hbm.xml中加入和权限的关联关系属性:
<!-- privileges属性,本类与Privilege的多对多 -->
<set name="privileges" table="oa_privilege_role">
<key column="roleId"></key>
<many-to-many class="Privilege" column="privilegeId"></many-to-many>
</set>
5.将Privilege.hbm.xml加入hibernate.cfg.xml中:
<mapping resource="cn/clear/oa/domain/Privilege.hbm.xml" />
6.测试建表。