上一篇建立了User類 Role類以及Authority這三個主要類
下面通過spring整合hibernate ,講原來的hibernate的xml交給spring去管理
這是applicationContext並且開啓事物註解
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置數據源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="url"
value="jdbc:mysql://localhost:3306/ssh_user?useUnicode=true&characterEncoding=UTF-8">
</property>
<property name="username" value="root"></property>
<property name="password" value="527090038"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<!-- 初始化3個鏈接 -->
<property name="initialSize" value="3"></property>
<!-- 最多500個鏈接 -->
<property name="maxActive" value="500"></property>
<!-- 最大空閒 值-->
<property name="maxIdle" value="2"></property>
<!-- 最小空閒值 -->
<property name="minIdle" value="1"></property>
</bean>
<!-- 配置會話工廠 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- 接管了hibernate的對象映射文件 -->
<property name="mappingResources">
<list>
<value>com/caicai/entity/User.hbm.xml</value>
<value>com/caicai/entity/Authority.hbm.xml</value>
<value>com/caicai/entity/Role.hbm.xml</value>
<value>com/caicai/entity/Group.hbm.xml</value>
<value>com/caicai/entity/Information.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<!-- <props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>-->
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
</value>
</property>
</bean>
<!-- 配置UseService對象 -->
<bean id="userService" class="com.caicai.service.imp.UserService">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="roleService" class="com.caicai.service.imp.RoleService">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="GroupService" class="com.caicai.service.imp.GroupService">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事務管理器 統一管理事物 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 啓用事務註解 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
</beans>
如上配置的UserService RoleService GroupService 這三個類的關係如下圖所示
具體實現類如下
這裏介紹下各個類接口 之間的關係
有基礎接口 基礎實現類 裏面寫一寫常用的方法
然後用戶接口 用戶實現類
還有角色接口 角色實現類
以及權限接口 權限實現類
關係已用戶爲例子 看下圖
下面是basisinter的代碼
package com.caicai.basis;
import java.util.List;
public interface Basisinter {
//基礎接口.聲明一些常用的方法
//通過id查詢
public Object FindByid(Class c,java.io.Serializable id);
//查詢 通過hql
public List ExcuteQuery(String hql,Object[]ps);
//增加
public void Add(Object obj);
//刪除
public void Delete(Class c,java.io.Serializable id);
//修改
public void Update(Object obj);
//其他再說
}
下面是基礎實現類basisService
package com.caicai.basis;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
//開啓事務用到的註解
@Transactional
public class BasisService implements Basisinter{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Override
public Object FindByid(Class c, Serializable id) {
// TODO Auto-generated method stub
Object obj=this.sessionFactory.getCurrentSession().get(c, id);
return obj;
}
@Override
public List ExcuteQuery(String hql, Object[] ps) {
// TODO Auto-generated method stub
Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
if(ps!=null&&ps.length!=0)
{
for(int i=0;i<ps.length;i++)
{
query.setParameter(i, ps[i]);
}
}
return query.list();
}
@Override
public void Add (Object obj) {
this.sessionFactory.getCurrentSession().save(obj);
// TODO Auto-generated method stub
}
@Override
public void Delete(Class c,Serializable id) {
// TODO Auto-generated method stub
//this.sessionFactory.getCurrentSession().load(c.getClass(),id);
//需要把對象加載出來再刪除
this.sessionFactory.getCurrentSession().delete(this.sessionFactory.getCurrentSession().load(c,id));
}
@Override
public void Update(Object obj) {
// TODO Auto-generated method stub
//必須要有主鍵
this.sessionFactory.getCurrentSession().update(obj);
}
}
下面是UserServiceinter 這個接口
他這裏寫上User才使用到的一些方法
package com.caicai.service.inter;
import java.util.List;
import com.caicai.basis.Basisinter;
import com.caicai.entity.User;
//user的 接口
public interface UserServiceinter extends Basisinter{
public User Userlogin(User u);//驗證登錄
public List<User> UserfindBypage(int start,int size,String uname,Integer rid,Integer gid);
public Number UserfindCount(String uname,Integer rid,Integer gid);
}
至於UserService 這個類 他繼承了基礎類和實現了UserServiceinter
所以他具有基本的方法和自己獨有的方法
package com.caicai.service.imp;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;
import com.caicai.basis.BasisService;
import com.caicai.entity.User;
import com.caicai.service.inter.UserServiceinter;
public class UserService extends BasisService implements UserServiceinter{
@Override//這個方法用來驗證用戶的登錄
public User Userlogin(User u) {
User ru=null;
String hql="from User where uname = ? and upassword= ? ";
Object[] ps={u.getUname(),u.getUpassword()};
List list=this.ExcuteQuery(hql, ps);
Iterator it= list.iterator();
//保證沒有重複的用戶
while(it.hasNext())
{
ru=(User) it.next();
}
return ru;
// TODO Auto-generated method stub
}
//分頁查詢
@Override
public List UserfindBypage(int start, int size, String uname,
Integer rid,Integer gid) {
// TODO Auto-generated method stub
System.out.println("start"+start+"size"+size+"uname"+uname+"rid"+rid);
Criteria cta=this.getSessionFactory().getCurrentSession().createCriteria(User.class);
if(rid!=0)
{
cta.createAlias("urole", "r").add(Restrictions.eq("r.rid", rid));
}
if(gid!=0)
{
cta.createAlias("ugroup", "g").add(Restrictions.eq("g.gid", gid));
}
if(uname!=null&&!uname.equals(""))
{
cta.add(Restrictions.eq("uname", uname));
}
return cta.setFirstResult(start).setMaxResults(size).list();
}
//分頁的記錄條數查詢
@Override
public Number UserfindCount(String uname, Integer rid,Integer gid) {
Criteria cta=this.getSessionFactory().getCurrentSession().createCriteria(User.class);
if(rid!=0)
{
cta.createAlias("urole", "r").add(Restrictions.eq("r.rid", rid));
}
if(gid!=0)
{
cta.createAlias("ugroup", "g").add(Restrictions.eq("g.gid", gid));
}
if(uname!=null&&!uname.equals(""))
{
cta.add(Restrictions.eq("uname", uname));
}
Number t=(Number) cta.setProjection(Projections.rowCount()).uniqueResult();
cta.setProjection(null);
// TODO Auto-generated method stub
return t;
}
}
其他類省略 使用basis 可以節省很多代碼 把具有相同功能的代碼寫在一塊