JPA 使用
本文以JPA+Hibernate 角色與權(quán)限示例說(shuō)明。
角色實(shí)體定義:
@Entity @Table public class Role { private long id; private String name; private String type; private Timestamp createTime; private Set<Resource> resources=new HashSet<Resource>(); @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public long getId() { return id; } public void setId(long id) { this.id = id; } @Column public String getName() { return name; } public void setName(String name) { this.name = name; } @Column public String getType() { return type; } public void setType(String type) { this.type = type; } @Column public Timestamp getCreateTime() { return createTime; } public void setCreateTime(Timestamp createTime) { this.createTime = createTime; } @ManyToMany @JoinTable(name = "role_resource",joinColumns = {@JoinColumn(name="roleId", referencedColumnName = "id")},inverseJoinColumns = {@JoinColumn(name="resourceId", referencedColumnName = "id")}) public Set<Resource> getResources() { return resources; } public void setResources(Set<Resource> resources) { this.resources = resources; }
資源實(shí)體定義
@Entity @Table public class Resource { private long id; private String name; private String title; private String description; private String icon; private StatisticsType type; private String url; private long orderNumber; private boolean first; private Timestamp createTime; private Set<Role> roles=new HashSet<Role>(); @Id @GeneratedValue(strategy= GenerationType.IDENTITY) public long getId() { return id; } public void setId(long id) { this.id = id; } @Column public String getName() { return name; } public void setName(String name) { this.name = name; } @Column public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } @Column public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Column public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } @Column public StatisticsType getType() { return type; } public void setType(StatisticsType type) { this.type = type; } @Column public String getUrl() { return url; } @Column public void setUrl(String url) { this.url = url; } public long getOrderNumber() { return orderNumber; } public void setOrderNumber(long orderNumber) { this.orderNumber = orderNumber; } @Column public boolean isFirst() { return first; } public void setFirst(boolean first) { this.first = first; } @Column public Timestamp getCreateTime() { return createTime; } public void setCreateTime(Timestamp createTime) { this.createTime = createTime; } @ManyToMany(mappedBy = "resources") public Set<Role> getRoles() { return roles; } public void setRoles(Set<Role> roles) { this.roles = roles; } }
說(shuō)明:角色和資源之間為多對(duì)多的關(guān)系,通過(guò)@ManyToMany注解表示。ManyToMany的屬性mappedBy指明關(guān)系的維護(hù)方,哪個(gè)實(shí)體@ManyToMany指定mappedBy說(shuō)明此實(shí)體為關(guān)系的被維護(hù)方。以上角色和資源中,角色即為關(guān)系維護(hù)方,資源為被維護(hù)方。
數(shù)據(jù)庫(kù)配置:
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <property name="maxActive" value="50" /> <property name="initialSize" value="1" /> <property name="maxWait" value="60000" /> <property name="minIdle" value="1" /> <property name="timeBetweenEvictionRunsMillis" value="3000" /> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x' FROM DUAL" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- mysql 不支持 poolPreparedStatements --> <!--<property name="poolPreparedStatements" value="true" /> --> <!--<property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> --> <!-- 開啟Druid的監(jiān)控統(tǒng)計(jì)功能 --> <property name="filters" value="stat" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>com.vrvwh.wh01.domain</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=${dialect} hibernate.show_sql=${hibernate.show_sql} hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto} cache.provider_class=${hibernate.cache.provider_class} cache.use_second_level_cache=${hibernate.cache.use_second_level_cache} cache.use_query_cache=${hibernate.cache.use_query_cache} hibernate.jdbc.batch_size=${hibernate.jdbc.batch_size} </value> </property> </bean>
浙公網(wǎng)安備 33010602011771號(hào)