如果你不知道怎么配置Spring + hibernate 的话,我可以给你一份参考。这个是我在项目中实际使用的,其中包括OpenSessionInView的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" destroy-method="close">
<property name="jndiName" value="java:comp/env/FiberSchedulerDS"/>
</bean>
<bean id="localSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<description>使用本地数据源,可以考虑使用容器提供的数据源来解决外部切换数据源的问题</description>
<ref bean="jndiDataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.jdbc.batch_size">30</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<description>一次性指定model包下的所有映射文件。假设你需要映射的文件都放在包your.package.path底下</description>
<list>
<value>classpath:/your/pacakge/path</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="localSessionFactory" />
</property>
</bean> <bean id="matchAllWithPropReq" class="org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource">
<property name="transactionAttribute">
<value>PROPAGATION_REQUIRED</value>
</property>
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributeSource">
<ref bean="matchAllWithPropReq"/>
</property>
</bean> <bean id="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory" ref="localSessionFactory" />
</bean> <bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<description>对所有符合名字规则的对象使用指定的Interceptor。也就是说,你的bean必须以Service结尾。比如,"userManagemetnService"</description>
<property name="beanNames">
<value>*Service</value>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean> <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
<property name="transactionInterceptor" ref="transactionInterceptor" />
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" destroy-method="close">
<property name="jndiName" value="java:comp/env/FiberSchedulerDS"/>
</bean>
<bean id="localSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<description>使用本地数据源,可以考虑使用容器提供的数据源来解决外部切换数据源的问题</description>
<ref bean="jndiDataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.jdbc.batch_size">30</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<description>一次性指定model包下的所有映射文件。假设你需要映射的文件都放在包your.package.path底下</description>
<list>
<value>classpath:/your/pacakge/path</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="localSessionFactory" />
</property>
</bean> <bean id="matchAllWithPropReq" class="org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource">
<property name="transactionAttribute">
<value>PROPAGATION_REQUIRED</value>
</property>
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributeSource">
<ref bean="matchAllWithPropReq"/>
</property>
</bean> <bean id="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory" ref="localSessionFactory" />
</bean> <bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<description>对所有符合名字规则的对象使用指定的Interceptor。也就是说,你的bean必须以Service结尾。比如,"userManagemetnService"</description>
<property name="beanNames">
<value>*Service</value>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean> <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
<property name="transactionInterceptor" ref="transactionInterceptor" />
</bean>
</beans>
* 根据用户id查找用户对象。
* @param id 要查找的用户的账号。
* @return 找到的用户对象。如果没有找到,返回 <code>null</code>。
*/
public User findUser(Long id);
/**
* 根据用户账号查找用户对象。
* @param userId 要查找的用户的账号。
* @return 找到的用户对象。如果没有找到,返回 <code>null</code>。
*/
public User findUserByUsreId(String userId);
}public class UserManagementServiceImpl implements UserManagementService {
private UserDao userDao;
@Override
public User findUser(Long id) {
return this.userDao.findById(id);
} @Override
public User findUserByUsreId(String userId) {
return this.userDao.find(userId);
} /**
* @param userDao the userDao to set
*/
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
* 根据对象ID来查询对象。
* @param id 对象ID。
* @return 如果找到对应的对象,则返回该对象。如果不能找到,则返回null。
*/
public T findById(Long id);
/**
* 根据用户ID查找用户
*/
public User find(String userId);
}public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {
@SuppressWarnings("unchecked")
public User findById(Long id) {
return (User) this.getHibernateTemplate().get(User.class, id);
} @SuppressWarnings("unchecked")
public User find(final String userId) {
return (User) this.getHibernateTemplate().execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String hql = "from User user where user.userId = :userId";
Query query = session.createQuery(hql);
query.setString("userId", userId);
User user = (User) query.uniqueResult();
return user;
}
});
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="userDao" class="cn.com.gxlu.fiberscheduler.persistence.hibernate.daoimpl.UserDaoImpl">
<property name="sessionFactory">
<ref bean="localSessionFactory"/>
</property>
</bean> <bean id="userManagementService" class="cn.com.gxlu.fiberscheduler.service.impl.UserManagementServiceImpl">
<property name="userDao">
<ref bean="userDao"/>
</property>
</bean>
</beans>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="list*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 所有名字以"Service"结尾的bean的所有public方法 -->
<aop:pointcut id="allServiceMethodsPointcut" expression="execution(* *.*.*Service.*(..))" />
<!-- 所有名字以"WS"结尾的bean的所有public方法 -->
<aop:pointcut id="allWSMethodsPointcut" expression="execution(* *.*.*WS.*(..))" />
<aop:advisor pointcut-ref="allServiceMethodsPointcut" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="allWSMethodsPointcut" advice-ref="txAdvice" />
</aop:config>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
有了这个,就可以使用 <tx:... 和 <aop:.. 了