分享一个自己以前配置的 hibernate spring 配置文件

0
(0)
<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright (c) 2017 西安才多信息技术有限责任公司。
  ~ 项目名称:dev
  ~ 文件名称:applicationContext-hibernate.xml
  ~ 日期:17-6-28 上午8:14
  ~ 作者:yangyan
  ~
  -->

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:properties/application.properties"/>
    <!-- 数据库连接池 -->
    <!-- 阿里 druid数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <!-- 数据库基本信息配置 -->
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="filters" value="config,stat"/>
        <property name="connectionProperties" value="config.decrypt=false"/>
        <!-- 最大并发连接数 -->
        <property name="maxActive" value="${maxActive}"/>
        <!-- 初始化连接数量 -->
        <property name="initialSize" value="${initialSize}"/>
        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="${maxWait}"/>
        <!-- 最小空闲连接数 -->
        <property name="minIdle" value="${minIdle}"/>
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}"/>
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}"/>
        <property name="validationQuery" value="${validationQuery}"/>
        <property name="testWhileIdle" value="${testWhileIdle}"/>
        <property name="testOnBorrow" value="${testOnBorrow}"/>
        <property name="testOnReturn" value="${testOnReturn}"/>
        <property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}"/>
        <!-- 打开removeAbandoned功能 -->
        <property name="removeAbandoned" value="${removeAbandoned}"/>
        <!-- 1800秒,也就是30分钟 -->
        <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/>
        <!-- 关闭abanded连接时输出错误日志 -->
        <property name="logAbandoned" value="${logAbandoned}"/>
    </bean>


    <!-- hibernate 事务配置 -->
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <!--<bean id="transactionManager"-->
    <!--class="org.springframework.orm.jpa.JpaTransactionManager">-->
    <!--<property name="entityManagerFactory" ref="entityManagerFactory"/>-->
    <!--</bean>-->




    <!-- hibernate 数据库配置 -->
    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan">
            <array>
                <value>cn.firegod.**.entity</value>
                <value>cn.firegod.**.dao</value>
            </array>
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <!--<prop key="hibernate.format_sql">true</prop>-->
                <prop key="connection.autoReconnect">true</prop>
                <prop key="connection.autoReconnectForPools">true</prop>
                <prop key="hibernate.current_session_context_class">
                    org.springframework.orm.hibernate5.SpringSessionContext
                </prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>
    <!-- 备用-->
    <bean class="org.springframework.orm.hibernate5.HibernateTemplate" id="hibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!--Service 方法名事务配置-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="del*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="create*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="modify*" propagation="REQUIRED"/>
            <tx:method name="change*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="remove*" propagation="REQUIRED"/>
            <tx:method name="active*" propagation="REQUIRED"/>
            <tx:method name="send*" propagation="REQUIRED"/>
            <tx:method name="receive*" propagation="REQUIRED"/>

            <tx:method name="pushToRemote*" read-only="false" no-rollback-for="java.lang.RuntimeException"
                       propagation="NOT_SUPPORTED" isolation="READ_COMMITTED"/>
            <!--<tx:method name="*" read-only="false" propagation="SUPPORTS"/>-->


        </tx:attributes>
    </tx:advice>


    <aop:config proxy-target-class="true">
        <!-- 配置事务切入到Service包下的各个方法中-->
        <aop:advisor advice-ref="txAdvice"
                     pointcut="execution(* cn.firegod..service..*.*(..))"
                     order="2"/>
    </aop:config>

    <bean id="openSessionInterceptor" class="org.springframework.orm.hibernate5.support.OpenSessionInterceptor">
        <property name="sessionFactory"
                  ref="sessionFactory"/>
    </bean>


    <!--开启注解事务配置-->
    <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" order="1"/>

    <!--Hibernate Session 控制自动开启-->
    <aop:config proxy-target-class="false">
        <aop:pointcut id="allServiceMethod"
                      expression="execution(* cn.firegod..service..*.*(..)) || execution(* cn.firegod.common.shiro.AdminAuthorizingRealm.*(..)) || execution(* cn.firegod.common.aop.LogAspect.*(..))"/>
        <aop:advisor advice-ref="openSessionInterceptor" pointcut-ref="allServiceMethod"/>
    </aop:config>

</beans>

 

这篇文章有用吗?

平均评分 0 / 5. 投票数: 0

到目前为止还没有投票!成为第一位评论此文章。

很抱歉,这篇文章对您没有用!

让我们改善这篇文章!

告诉我们我们如何改善这篇文章?


了解 工作生活心情记忆 的更多信息

Subscribe to get the latest posts sent to your email.

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据