• JPA 使用


    本文以JPA+Hibernate 角色与权限示例说明。

    角色实体定义:

    @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;
        }

    资源实体定义

    @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;
        }
    }

    说明:角色和资源之间为多对多的关系,通过@ManyToMany注解表示。ManyToMany的属性mappedBy指明关系的维护方,哪个实体@ManyToMany指定mappedBy说明此实体为关系的被维护方。以上角色和资源中,角色即为关系维护方,资源为被维护方。

    数据库配置:

        <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的监控统计功能 -->
            <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>
  • 相关阅读:
    BZOJ 1008 [HNOI2008]越狱 (简单排列组合 + 快速幂)
    BZOJ 1007 [HNOI2008]水平可见直线 (栈)
    Java Date,long,String 日期转换
    android学习---- WindowManager 接口 (
    ListView 使用详解
    @synchronized (object)使用详解
    Android View坐标getLeft, getRight, getTop, getBottom
    Android:Layout_weight的深刻理解
    onTouch事件试验(覆写onTouchEvent方法,同时设置onTouchListener)
    FragmentPagerAdapter与FragmentStatePagerAdapter区别
  • 原文地址:https://www.cnblogs.com/tyb1222/p/4299881.html
Copyright © 2020-2023  润新知