1. Spring与Hibernate的整合

1.1. 在Spring容器中创建SessionFactory

为了避免硬编码的资源查找与应用程序对象紧密耦合,Spring允许你在Spring容器中以bean的方式定义诸如JDBC DataSource或者Hibernate SessionFactory 的数据访问资源。任何需要进行资源访问的应用程序对象只需要持有这些事先定义好的实例的引用,下面的代码演示如何创建一个JDBC DataSource 和Hibernate SessionFactory 。

在ApplicationContext.xml文件中进行如下配置:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">

</property>

<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl">

</property>

<property name="username" value="scott"></property>

<property name="password" value="tiger"></property>

</bean>

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource">

<ref bean="dataSource" />

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">

org.hibernate.dialect.Oracle9Dialect

</prop>

</props>

</property>

<property name="mappingResources">

<list>

<value>

com/morris/school/dao/entity/Department.hbm.xml

</value>

</list>

</property>

</bean>

在应用程序中可以如下使用:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(

"applicationContext.xml");

SessionFactory sessionFactory = (SessionFactory) applicationContext

.getBean("sessionFactory");

1.2. 基于Hibernate3的原生API实现DAO

DepartmentDapImpl.java

package com.morris.school.dao.jdbc;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.classic.Session;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.morris.school.dao.IDepartmentDao;

import com.morris.school.dao.entity.Department;

public class DepartmentDapImpl implements IDepartmentDao {

private SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {

return sessionFactory;

}

public void setSessionFactory(SessionFactory sessionFactory) {

this.sessionFactory = sessionFactory;

}

public void addDepartment(Department dept) {

Session session = sessionFactory.openSession();

Transaction transaction = null;

try {

transaction = session.beginTransaction();

session.save(dept);

transaction.commit();

} catch (Exception e) {

if (transaction != null) {

transaction.rollback();

}

e.printStackTrace();

} finally {

session.close();

}

}

}

在spring配置文件中如下配置:

<bean id="deptDao" class="com.morris.school.dao.jdbc.DepartmentDapImpl">

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>

1.3. 基于Spring的DAO实现

HibernateDaoSupport 基类提供了访问与当前事务绑定的 Session 对象的函数,因而能保证在这种情况下异常的正确转化。 类似的函数同样可以在 SessionFactoryUtils 类中找到,但他们以静态方法的形式出现。 值得注意的是,通常将 'false' 作为参数值(表示是否允许创建)传递给 getSession(..) 方法进行调用。 此时,整个调用将在同一个事务内完成(它的整个生命周期由事务控制,避免了关闭返回的 Session 的需要)。

package com.morris.school.dao.jdbc;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.classic.Session;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.morris.school.dao.IDepartmentDao;

import com.morris.school.dao.entity.Department;

public class DepartmentDapImpl extends HibernateDaoSupport implements IDepartmentDao {

public static void main(String[] args) {

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(

"applicationContext.xml");

IDepartmentDao deptDao = (IDepartmentDao) applicationContext

.getBean("deptDao");

deptDao.addDepartment(new Department("4", "后勤部"));

}

public void addDepartment(Department dept) {

this.getHibernateTemplate().save(dept);

}

}

1.4. 单个事务管理的配置

<!-- 目标对象 -->

<bean id="deptDao" class="com.morris.school.dao.jdbc.DepartmentDapImpl">

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>

<!-- 切面 -->

<bean id="transactionManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>

<!-- 代理对象 -->

<bean id="transactionProxy"

class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

<!-- 目标对象 -->

<property name="target" ref="deptDao"></property>

<!-- 切面 -->

<property name="transactionManager" ref="transactionManager"></property>

<property name="proxyTargetClass" value="true">

</property>

<!-- 属性模板 -->

<property name="transactionAttributes">

<props>

<prop key="add*">PROPAGATION_REQUIRED</prop>

<prop key="update*">PROPAGATION_REQUIRED</prop>

<prop key="delete*">PROPAGATION_REQUIRED</prop>

<prop key="insert*">PROPAGATION_REQUIRED</prop>

<prop key="save*">PROPAGATION_REQUIRED</prop>

<prop key="do*">PROPAGATION_REQUIRED</prop>

<prop key="query*">readOnly</prop>

<prop key="find*">readOnly</prop>

<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>

<!--

PROPAGATION_REQUIRED == 支持当前事务,如果当前没有事务则重新创建一个事务。

PROPAGATION_SUPPORTS == 支持当前事务,如果当前没有事务则以非事务方式执行 readonly ==

就是只读,设置操作权限为只读,一般用于查询

-->

</props>

</property>

</bean>

1.5. 公共事务的配置

<!-- 目标对象 -->

<bean id="deptDao" class="com.morris.school.dao.jdbc.DepartmentDapImpl">

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>

<!-- 切面 -->

<bean id="transactionManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>

<!-- 公共事务代理 -->

<bean id="baseTransactionProxy"

class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"

abstract="true" lazy-init="true">

<!-- 切面 -->

<property name="transactionManager" ref="transactionManager"></property>

<!-- 属性模板 -->

<property name="transactionAttributes">

<props>

<prop key="add*">PROPAGATION_REQUIRED</prop>

<prop key="update*">PROPAGATION_REQUIRED</prop>

<prop key="delete*">PROPAGATION_REQUIRED</prop>

<prop key="insert*">PROPAGATION_REQUIRED</prop>

<prop key="save*">PROPAGATION_REQUIRED</prop>

<prop key="do*">PROPAGATION_REQUIRED</prop>

<prop key="query*">readOnly</prop>

<prop key="find*">readOnly</prop>

<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>

<!--

PROPAGATION_REQUIRED == 支持当前事务,如果当前没有事务则重新创建一个事务。

PROPAGATION_SUPPORTS == 支持当前事务,如果当前没有事务则以非事务方式执行 readonly ==

就是只读,设置操作权限为只读,一般用于查询

-->

</props>

</property>

</bean>

<bean id="userDaoProxy" parent="baseTransactionProxy">

<property name="target" ref="deptDao"></property>

</bean>

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐