學習整合hibernate springmvc spring的 心得(1)

懷着對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 authoritypath) {
			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>






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章