Hibernate4.2和Spring3.2的整合问题-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

Hibernate4.2和Spring3.2的整合问题

a123456678 2016-03-17 11:23:13 1193

spring3.1以后的版本去掉了HibernateDaoSupport这个工具,也就不存在getTemplate()方法了。

我的疑问是:1.spring关于“sessionFactory”的一系列bean的配置(包括连接池)不是无法使用注入的方式

来支持dao实例了吗?

2.若使用spring3.2和hibernate4.2整合,事务管理应该给哪一方比较好?

分享到
取消 提交回答
全部回答(1)
  • a123456678
    2019-07-17 19:04:56
    <?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:aop="http://www.springframework.org/schema/aop"
            xmlns:context="http://www.springframework.org/schema/context"
            xmlns:tx="http://www.springframework.org/schema/tx"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd ">
     
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
             <property name="driverClass" value="com.mysql.jdbc.Driver"/>
             <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mycms?useUnicode=true&amp;characterEncoding=UTF-8"/>
             <property name="user" value="root"/>
             <property name="password" value="123456"/>
             <property name="maxPoolSize" value="100"/>
             <property name="minPoolSize" value="3"/>
             <property name="initialPoolSize" value="3"/>
             <property name="maxIdleTime" value="120"/>
         </bean>
     
        <bean id="sessionFactory"
               class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
             <property name="dataSource" ref="dataSource"/>
             <property name="hibernateProperties">
                 <props>
                     <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                     <prop key="hibernate.generate_statistics">true</prop>
                     <prop key="hibernate.hbm2ddl.auto">update</prop>
                     <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
                     <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
                     <prop key="hibernate.cache.provider_configuration_file_resource_path">classpath:ehcache.xml</prop>
                     <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
                     <prop key="hibernate.cache.use_query_cache">true</prop>
                     <prop key="hibernate.cache.use_second_level_cache">true</prop>
                     <prop key="hibernate.generate_statistics">true</prop>
                     <prop key="hibernate.max_fetch_depth">2</prop>
                     <prop key="hibernate.order_updates">true</prop>
                     <prop key="hibernate.connection.autocommit">false</prop>
                     <prop key="hibernate.show_sql">true</prop>
                     <prop key="format_sql">true</prop>
                 </props>
             </property>
             <property name="packagesToScan" value="com.codeworker.mycms"/>
         </bean>
     
     
         <context:component-scan base-package="com.codeworker.mycms">
             <context:include-filter type="regex" expression="..*ServiceImpl"/>
         </context:component-scan>
         <context:component-scan base-package="com.codeworker.mycms">
             <context:include-filter type="regex" expression="..*DaoImpl"/>
         </context:component-scan>
     
        <aop:aspectj-autoproxy/>
     
        <!-- 声明使用注解式事务 -->
         <tx:annotation-driven transaction-manager="transactionManager"/>
         <bean id="transactionManager"
               class="org.springframework.orm.hibernate4.HibernateTransactionManager">
             <property name="sessionFactory" ref="sessionFactory"/>
         </bean>
     
        <!-- 将Hibernate异常转变成Spring的DataAccessException异常,保持一致的异常处理方式 -->
         <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
     
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
             <property name="dataSource" ref="dataSource" />
         </bean>
     
    </beans>
    0 0
云计算
使用钉钉扫一扫加入圈子
+ 订阅

时时分享云计算技术内容,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

推荐文章