开发者社区> 问答> 正文

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

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

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

来支持dao实例了吗?

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

展开
收起
a123456678 2016-03-17 11:23:13 2482 0
1 条回答
写回答
取消 提交回答
  • <?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>
    2019-07-17 19:04:56
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Boot 2.5开发实战 立即下载
Java Spring Boot开发实战系列课程【第6讲】:Spring Boot 2.0实战MyBatis与优化(Java面试题) 立即下载
Java Spring Boot开发实战系列课程【第15讲】:Spring Boot 2.0 API与Spring REST Docs实战 立即下载

相关实验场景

更多