爲什麼要整合Hibernate?
1、使用Spring的IOC功能管理SessionFactory對象
LocalSessionFactoryBean
2、使用Spring管理Session對象
HibernateTemplate
3、使用Spring的功能實現聲明式的事務管理整合Hibernate的步驟:
1、配置SessionFactory(可以自動完成)
<beanid="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>2、配置HibernateTemplate,用於完成數據操作
<bean id="hibernateTemplete" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory"ref="sessionFactory"></property>
</bean>3、讓Common繼承HibernateDaoSupport類,該類提供了HibernateTemplate的getter和setter方法。
4、將hibernateTemplete注入到Common中
<bean id="common" class="com.aptech.common.Common">
<property name="hibernateTemplate"ref="hibernateTemplete"></property>
</bean>5、將Common的方法修改成hibernateTemplete的操作。
package com.aptech.common;import java.sql.SQLException;
import java.util.List;import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.aptech.exception.CommonException;
import com.aptech.htm.HibernateSessionFactory;
/**
* 通用類,不再負責事務處理
* 目標對象
* @author 李贊紅
*
* @param <POJO>
*/
public class Common<POJO> extends HibernateDaoSupport implementsICommon<POJO> {
public void insertObject(POJO pojo) throws CommonException {
try {
this.getHibernateTemplate().save(pojo);
} catch (DataAccessException e) {
e.printStackTrace();
throw new CommonException(e);
}
}public void updateObject(POJO pojo) throws CommonException{
try {
this.getHibernateTemplate().update(pojo);
} catch (DataAccessException e) {
e.printStackTrace();
throw new CommonException(e);
}
}public void deleteObject(Class theClass, long id) throwsCommonException {
try {
Object obj = this.getHibernateTemplate().load(theClass, id);
this.getHibernateTemplate().delete(obj);
} catch (DataAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new CommonException(e);
}
}public POJO loadObject(Class theClass, long id) throwsCommonException {
try {
Object obj = this.getHibernateTemplate().load(theClass, id);
return (POJO) obj;
} catch (DataAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new CommonException(e);
}
}public List queryObjects(final String hql) throwsCommonException {
class Hc implements HibernateCallback{
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
return session.createQuery(hql).list();
}
}
try {
return this.getHibernateTemplate().executeFind(new Hc());
} catch (DataAccessException e) {
e.printStackTrace();
throw new CommonException(e);
}
}public List queryObjects(Class theClazz) throwsCommonException {
return this.queryObjects("from " +theClazz.getSimpleName());
}
}
6、配置事務
<!-- 事務管理器,相當於TransactionProxy,定義事務的開啓、提交、回滾 -->
<bean id="htm"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="ti"class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="htm"/>
</property>
<property name="transactionAttributes">
<props>
<!-- key:方法名稱 -->
<propkey="insert*">PROPAGATION_REQUIRED</prop>
<propkey="update*">PROPAGATION_REQUIRED</prop>
<propkey="delete*">PROPAGATION_REQUIRED</prop>
<propkey="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- 自動代理,代理業務對象 -->
<beanclass="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>common</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>ti</value>
</list>
</property>
</bean>7、將common注入Dao
<bean id="baseDao" abstract="true">
<property name="common">
<ref bean="common"/>
</property>
</bean>
<bean id="udao"class="com.aptech.dao.impl.UserDao"parent="baseDao"></bean>
<bean id="rdao"class="com.aptech.dao.impl.RoleDao"parent="baseDao"></bean>
8、將Dao注入Service
<bean id="grantService"class="com.aptech.service.impl.GrantService">
<property name="rdao"ref="rdao"></property>
<property name="udao"ref="udao"></property>
</bean>最近整理了學習材料,有需要的請下載,我放微信裏面了,方便下載,還能交流,掃描我的二維碼頭像即可。