您的位置:首页 > 编程语言 > Java开发

学习整合hibernate springmvc spring的 心得(1)

2016-06-21 16:00 453 查看
怀着对java的热情,在学习基本的servlet和jsp jdbc以后 学习了hibernate springmvc spring下面讲述一下心得

这里以最简单的角色控制权限,通过队伍管理,为例子 ,正常的是用户和角色多对多,角色和权限多对多,5张表 但是处理的逻辑比较简单就直接用户和角色多对1,角色和权限多对多 ,然后用户和队伍多对多实现,hibernate采用xml开发,这里不用注解

这里主要是这几个类 User类 Role角色类 Authority权限类 这里写了information为User类的详细信息 Group为User所在的队伍

这里省略information和Group这2个类

首先是User类

package com.caicai.entity;

import java.io.Serializable;

public class User implements Serializable{
private Integer uid;
private String uname;
private String upassword;
private String uphoto;
private Role urole;
private Group ugroup;
private Information uinformation;

public Information getUinformation() {
return uinformation;
}
public void setUinformation(Information uinformation) {
this.uinformation = uinformation;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpassword() {
return upassword;
}
public void setUpassword(String upassword) {
this.upassword = upassword;
}
public String getUphoto() {
return uphoto;
}
public void setUphoto(String uphoto) {
this.uphoto = uphoto;
}
public Role getUrole() {
return urole;
}
public void setUrole(Role urole) {
this.urole = urole;
}
public Group getUgroup() {
return ugroup;
}
public void setUgroup(Group ugroup) {
this.ugroup = ugroup;
}
public User(Integer uid, String uname, String upassword, String uphoto,
Role urole, Group ugroup) {
super();
this.uid = uid;
this.uname = uname;
this.upassword = upassword;
this.uphoto = uphoto;
this.urole = urole;
this.ugroup = ugroup;
}
public User() {
super();
}
public User(String uname, String upassword) {
super();
this.uname = uname;
this.upassword = upassword;
}
public User(Integer uid, String uname, String upassword, String uphoto,
Role urole, Group ugroup, Information uinformation) {
super();
this.uid = uid;
this.uname = uname;
this.upassword = upassword;
this.uphoto = uphoto;
this.urole = urole;
this.ugroup = ugroup;
this.uinformation = uinformation;
}

}


User类的xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.caicai.entity.User" table="user" catalog="ssh_user">
<id name="uid" type="java.lang.Integer">
<column name="uid" />
<generator class="identity" />
</id>
<property name="uname" type="java.lang.String">
<column name="uname" />
</property>
<property name="upassword" type="java.lang.String">
<column name="upassword" />
</property>
<property name="uphoto" type="java.lang.String">
<column name="uphoto" />
</property>

<many-to-one name="urole" class="com.caicai.entity.Role" cascade="save-update" >
<column name="r_id" ></column>
</many-to-one>

<many-to-one name="ugroup" class="com.caicai.entity.Group" cascade="save-update">
<column name="g_id"></column>
</many-to-one>

<one-to-one name="uinformation" class="com.caicai.entity.Information" cascade="all"></one-to-one>
</class>
</hibernate-mapping>
其次是角色类 Role类

package com.caicai.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

@Entity
public class Role implements Serializable{
private int rid;
private String rname;
private Set<User> users=new HashSet<User>();
private Set<Authority> authoritys=new HashSet<Authority>();
public int getRid() {
return rid;
}
public void setRid(int rid) {
this.rid = rid;
}
public String getRname() {
return rname;
}
public void setRname(String rname) {
this.rname = rname;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
public Set<Authority> getAuthoritys() {
return authoritys;
}
public void setAuthoritys(Set<Authority> authoritys) {
this.authoritys = authoritys;
}
public Role(int rid, String rname, Set<User> users,
Set<Authority> authoritys) {
super();
this.rid = rid;
this.rname = rname;
this.users = users;
this.authoritys = authoritys;
}
public Role(String rname) {
super();
this.rname = rname;
}
public Role() {
super();
}

}
Role类的xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class catalog="ssh_user" name="com.caicai.entity.Role" table="role">
<id name="rid" type="java.lang.Integer">
<column name="rid"/>
<generator class="increment"/>
</id>
<property generated="never" name="rname" type="java.lang.String">
<column name="rname"/>
</property>
<set cascade="save-update" inverse="true" name="users" sort="unsorted" table="user">
<key>
<column name="r_id"/>
</key>
<one-to-many class="com.caicai.entity.User"/>
</set>

<set cascade="save-update" name="authoritys" sort="unsorted" table="authority_role">
<key>
<column name="r_id"/>
</key>
<many-to-many class="com.caicai.entity.Authority" unique="false">
<column name="a_id"/>
</many-to-many>
</set>
</class>
</hibernate-mapping>
这里是Authority类
package com.caicai.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

@Entity
public class Authority implements Serializable{
private int authorityid;
private String authorityname;
private String authoritybelong;
private String authoritypath;

public Authority(int authorityid, String authorityname,
String authoritybelong, String authoritypath, Set<Role> roles) {
super();
this.authorityid = authorityid;
this.authorityname = authorityname;
this.authoritybelong = authoritybelong;
this.authoritypath = authoritypath;
this.roles = roles;
}
public String getAuthoritybelong() {
return authoritybelong;
}
public void setAuthoritybelong(String authoritybelong) {
this.authoritybelong = authoritybelong;
}
public Authority(int authorityid, String authorityname,
String authoritypath, Set<Role> roles) {
super();
this.authorityid = authorityid;
this.authorityname = authorityname;
this.authoritypath = authoritypath;
this.roles = roles;
}
public String getAuthoritypath() {
return authoritypath;
}
public void setAuthoritypath(String authoritypat
4000
h) {
this.authoritypath = authoritypath;
}
private Set<Role> roles=new HashSet<Role>();
public int getAuthorityid() {
return authorityid;
}
public void setAuthorityid(int authorityid) {
this.authorityid = authorityid;
}
public String getAuthorityname() {
return authorityname;
}
public void setAuthorityname(String authorityname) {
this.authorityname = authorityname;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public Authority(int authorityid, String authorityname, Set<Role> roles) {
super();
this.authorityid = authorityid;
this.authorityname = authorityname;
this.roles = roles;
}
public Authority() {
super();
}
public Authority(String authorityname) {
super();
this.authorityname = authorityname;
}
public Authority(String authorityname, String authoritybelong,
String authoritypath) {
super();
this.authorityname = authorityname;
this.authoritybelong = authoritybelong;
this.authoritypath = authoritypath;
}

}
Authority的xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class catalog="ssh_user" name="com.caicai.entity.Authority" table="authority">
<id name="authorityid" type="java.lang.Integer">
<column name="authorityid"/>
<generator class="increment"/>
</id>

<property name="authorityname" type="java.lang.String">
<column name="authorityname"/>
</property>
<property name="authoritybelong" type="java.lang.String">
<column name="authoritybelong"/>
</property>

<property name="authoritypath" type="java.lang.String">
<column name="authoritypath"/>
</property>

<set cascade="save-update" inverse="true" name="roles" table="authority_role">
<key>
<column name="a_id"/>
</key>
<many-to-many class="com.caicai.entity.Role" unique="false">
<column name="r_id"/>
</many-to-many>
</set>

</class>
</hibernate-mapping>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息