如果你不知道怎么配置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>

解决方案 »

  1.   

    这个是一个简单的业务类的接口和实现:public interface UserManagementService {    /**
         * 根据用户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;
        }
    }
      

  2.   

    这个是Dao对象的接口和实现:public interface BaseDao<T> { /**
     * 根据对象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;
    }
    });
    }
    }
      

  3.   

    好了,最后是Dao和Service的Spring配置文件:
    <?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>
      

  4.   

    5楼的例子如何+上AOP怎么写???
      

  5.   

    这个已经有AOP了,不过是经典的配置方式,没有使用Spring2的新风格。对于Spring2风格的配置,在transactionManager bean之前的部分都一样,之后的加上下面这个就可以了:
        <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> 
      

  6.   

    当然了,要使用tx 和 aop的命名空间的东西,你的bean配置文件的声明上也要添加点新东西的:
    <?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:.. 了