最近學習中寫了一個簡單的spring 跟hibernate的配合使用,所以總結一下:
(1):spring配置文件中配置hibernate。spring orm模塊提供了對hibernate的支持。hibernate不需要在寫配置文件,只需要配置在spring配置文件中就可以。hibernate可以說只是spring容器中的bean。
(2):spring中hibernate的事務管理。
(3):dao層繼承HibernateDaoSupport
(一)datasource主要是用來配置連接數據庫的屬性,比如數據庫驅動類,數據庫url,數據庫用戶名,數據庫密碼。sessionFactory可以指定datasource,還有配置實體類位置以及一些hibernate配置。
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置數據源,連接數據庫的必須信息 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
<property name="jdbcUrl"
value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="user" value="system"/>
<property name="password" value="system" />
<property name="maxPoolSize" value="20" />
<property name="minPoolSize" value="1" />
<property name="initialPoolSize" value="1" />
<property name="maxIdleTime" value="20" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 實體類位置-->
<property name="annotatedClasses">
<list>
<value>Entity.LogTest</value>
<value>Entity.LUser</value>
</list>
</property>
<!-- 掃描包下面的java文件 -->
<!-- <property name="packagesToScan">
<list>
<value>Entity</value>
</list>
</property>
-->
<!-- 配置 hibernate 中的信息 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
</beans>
(二)hibernate事務配置也寫在spring配置文件中。主要步驟是:先定義一個事物管理器,定義事務管理規則(就是確定哪個方法執行完成後提交事務),定義事務管理代理類(我一般配置service成爲一個代理類,也可以配置一個dao作爲代理類)。
<!-- 配置事務 -->
<!-- 事物管理器,有一個屬性是sessionfactory,表示事物管理器管理這個sessionFactory創立的鏈接 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置 事務管理規則 properties表示需要事務提交的方法,這裏是所有方法-->
<bean id="hibernateTransactionAttributeSource"
class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<!-- 所有方法事務自動提交 -->
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 配置服務層 -->
<bean id="addLUserService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 配置事務管理器 -->
<property name="transactionManager" ref="transactionManager" /> <!-- 被包裝的bean -->
<property name="target">
<bean class="service.AddLUserService" >
<property name="dao" ref="LUserDao"></property>
</bean>
</property>
<!-- 事務提交規則 -->
<property name="transactionAttributeSource" ref="hibernateTransactionAttributeSource"></property>
</bean>
(三)dao層繼承支持接口。在dao層繼承HibernateDaoSupport就可以通過this.getSessionFactory()獲取到一個sessionFactory。然後通過這個連接工廠類創建連接實現數據庫操作。值得注意的是:一個hibernate開啓了事務管理器,openSession()不能成功提交事務,而getCurrentSession()可以在開啓事務管理器的時候完成提交。
public class LogDao extends HibernateDaoSupport implements ILogDao {
@Override
public void addLog(LogTest log) {
this.getSessionFactory().getCurrentSession().persist(log);
}
@Override
public void deleteLog(LogTest log) {
this.getSessionFactory().openSession().delete(log);
}
@Override
public void updateLog(LogTest log) {
this.getSessionFactory().openSession().update(log);
}
@Override
public LogTest findByTitle(String title) {
return (LogTest) this.getSessionFactory().openSession().createQuery("select l from Log l where title="+title);
}
@Override
public List<LogTest> findMyLog(LUser user) {
List<LogTest> list = this.getSessionFactory().getCurrentSession().createQuery("from LogTest where pid = '"+user.getId()+"'").list();
if(list.size()==0){
return null;
}
return list;
}
}
學習總結肯定有很多不足,希望多多指點。