Spring tomcat jdbc pool mybatis 集成配置

最近项目用到了Spring、Tomcat7自带的数据库连接池tomcat jdbc pool 、mybatis,现将三者集成的配置记录一下,以备不时之需。
所需的jar包:

spring框架需要的jar包(这里就不详细说了)
mybatis-3.1.1.jar
mybatis-spring-1.1.1.jar
mysql-connector-java-5.1.15-bin.jar
tomcat-jdbc.jar
tomcat-juli.jar

配置文件如下:

<?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:p="http://www.springframework.org/schema/p" 
       xmlns:aop="http://www.springframework.org/schema/aop" 
       xmlns:tx="http://www.springframework.org/schema/tx" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 
 
    <!--tomcat jdbc pool数据源配置-->           
    <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">   
        <property name="poolProperties"> 
            <bean class="org.apache.tomcat.jdbc.pool.PoolProperties">   
                <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
                <property name="url" value="jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8"/>   
                <property name="username" value="root"/>   
                <property name="password" value=""/> 
                <property name="jmxEnabled" value="true"/> 
                <property name="testWhileIdle" value="true"/> 
                <property name="testOnBorrow" value="true"/>   
                <property name="testOnReturn" value="false"/>   
                <property name="validationInterval" value="30000"/>   
                <property name="validationQuery" value="SELECT 1"/>   
                <property name="timeBetweenEvictionRunsMillis" value="30000"/>   
                <property name="maxActive" value="100"/>   
                <property name="initialSize" value="10"/>   
                <property name="maxWait" value="10000"/>   
                <property name="minEvictableIdleTimeMillis" value="30000"/>   
                <property name="minIdle" value="10"/>   
                <property name="logAbandoned" value="false"/>   
                <property name="removeAbandoned" value="true"/>   
                <property name="removeAbandonedTimeout" value="60"/> 
                <property name="jdbcInterceptors" value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"/>   
            </bean>   
        </property>   
    </bean>   
   
    <!-- 配置事务管理器,注意这里的dataSource和SqlSessionFactoryBean的dataSource要一致,不然事务就没有作用了 --> 
    <bean id="transactionManager" 
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
        <property name="dataSource" ref="dataSource" /> 
    </bean> 
 
    <!-- 定义拦截器,用来指定事务属性、级别和异常处理 --> 
    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">   
        <property name="transactionManager" ref="transactionManager"></property>   
        <property name="transactionAttributes">   
            <props>   
                <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop> 
                <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop> 
                <prop key="delete*"> PROPAGATION_REQUIRED,-Exception</prop> 
                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> 
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> 
            </props>   
        </property>   
    </bean> 
     
    <!-- 用来定义哪些类需要事务管理 spring事务动态代理类 BeanNameAutoProxyCreator 根据类名自动代理,接受表达式 -->   
    <bean id="BeanProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">   
        <property name="beanNames">   
            <!-- 对类名以Service结尾的类进行代理 -->   
            <value>*Service</value>   
        </property>   
        <!-- 对代理类进行加载拦截器(实现通知的过程) -->   
        <property name="interceptorNames">   
            <list>   
                <value>transactionInterceptor</value>   
            </list>   
        </property>   
    </bean>   
 
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
        <property name="configLocation" value="classpath:mybatis-config.xml" /> 
        <property name="dataSource" ref="dataSource" /> 
    </bean> 
   
    <bean id="authorityDao" class="com.hongdian.ithings.authoritymanagement.dao.AuthorityDaoImpl"> 
        <property name="sessionFactory" ref="sqlSessionFactory"/> 
    </bean> 
   
    <bean id="authorityService" class="com.hongdian.ithings.authoritymanagement.service.AuthorityServiceImpl"> 
        <property name="authorityDao" ref="authorityDao"/> 
    </bean> 
     
    <bean class="com.hongdian.ithings.authoritymanagement.common.SpringContext"></bean> 
     
    <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">   
        <!--线程池活跃的线程数-->   
        <property name="corePoolSize" value="5" /> 
        <!--线程池最大活跃的线程数--> 
        <property name="maxPoolSize" value="50" /> 
        <!--任务队列的最大容量--> 
        <property name="queueCapacity" value="5" /> 
    </bean>   
     
    <bean id="authorityComponent" class="com.hongdian.ithings.authoritymanagement.communication.AuthorityComponent"> 
        <property name="taskExecutor" ref="taskExecutor"/> 
    </bean> 
     
</beans> 

 

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享