zoukankan      html  css  js  c++  java
  • spring 配置多个数据源的文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Repository and Service layers -->
    <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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

        <!-- ========================= Repository RESOURCE DEFINITIONS ========================= -->
        <bean id="slave" class="org.apache.tomcat.jdbc.pool.DataSource">
            <property name="driverClassName" value="${jdbc.mysql.Driver}"/>
            <property name="url" value="${slave.connection}"/>
            <property name="username" value="${slave.username}"/>
            <property name="password" value="${slave.password}"/>
            <!--公共配置属性 -->
            <property name="maxActive" value="100"/>
            <property name="initialSize" value="10"/>
            <property name="minIdle" value="10"/>
            <property name="jdbcInterceptors" value="${tomcat.jdbc.pool.jdbcInterceptors}"/>
            <property name="testWhileIdle" value="true"/>
            <property name="testOnBorrow" value="true"/>
            <property name="validationQuery" value="select 1"/>
            <property name="testOnReturn" value="false"/>
            <property name="validationInterval" value="30000"/>
            <property name="timeBetweenEvictionRunsMillis" value="5000"/>
            <property name="maxWait" value="15000"/>
            <property name="removeAbandoned" value="true"/>
            <property name="removeAbandonedTimeout" value="60"/>
            <property name="logAbandoned" value="false"/>
            <property name="minEvictableIdleTimeMillis" value="30"/>
        </bean>

        <bean id="master" class="org.apache.tomcat.jdbc.pool.DataSource">
            <property name="driverClassName" value="${jdbc.mysql.Driver}"/>
            <property name="url" value="${master.connection}"/>
            <property name="username" value="${master.username}"/>
            <property name="password" value="${master.password}"/>
            <!--公共配置属性 -->
            <property name="maxActive" value="100"/>
            <property name="initialSize" value="10"/>
            <property name="minIdle" value="10"/>
            <property name="jdbcInterceptors" value="${tomcat.jdbc.pool.jdbcInterceptors}"/>
            <property name="testWhileIdle" value="true"/>
            <property name="testOnBorrow" value="true"/>
            <property name="validationQuery" value="select 1"/>
            <property name="testOnReturn" value="false"/>
            <property name="validationInterval" value="30000"/>
            <property name="timeBetweenEvictionRunsMillis" value="5000"/>
            <property name="maxWait" value="15000"/>
            <property name="removeAbandoned" value="true"/>
            <property name="removeAbandonedTimeout" value="60"/>
            <property name="logAbandoned" value="false"/>
            <property name="minEvictableIdleTimeMillis" value="30"/>
        </bean>

        <bean id="oldmemberdb" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="${oldmemberdb.connection}"/>
            <property name="username" value="${oldmemberdb.username}" />
            <property name="password" value="${oldmemberdb.password}" />
        </bean>


        <bean id="dataSource" class="db.DynamicDataSource">
            <property name="master" ref="master"/>
            <property name="slaves">
                <list>
                    <ref bean="slave"/>
                </list>
            </property>
        </bean>

        <!-- ibatis3 工厂类 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
            <property name="mapperLocations"
                      value="classpath:/jdbc/uc/dao/*.xml"/>
            <property name="typeAliasesPackage" value="uc.jdbc.uc.po"/>
        </bean>

        <bean id="sqlSessionFactory_1" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="oldmemberdb"/>
            <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
            <property name="mapperLocations"
                      value="classpath:/uc/jdbc/uc/dao/TestMapper.xml"/>
            <property name="typeAliasesPackage" value="uc.jdbc.uc.po"/>
        </bean>

        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="uc.jdbc.uc.olddao"/>
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_1"></property>
        </bean>


        <bean class="MapperScannerConfigurer">
            <property name="basePackage" value="uc.jdbc.uc.dao"/>
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        </bean>

        <!-- 定义单个jdbc数据源的事务管理器 -->
        <bean id="transactionManager"
              class="DynamicDataSourceTransactionManager">
            <!-- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   -->
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <!-- 以 @Transactional 标注来定义事务 -->
        <tx:annotation-driven transaction-manager="trasactionManager"
                              proxy-target-class="true"/>

        <bean id="transactionTemplate"
              class="org.springframework.transaction.support.TransactionTemplate">
            <property name="transactionManager" ref="transactionManager"/>
            <!--ISOLATION_DEFAULT 表示由使用的数据库决定  -->
            <property name="isolationLevelName" value="ISOLATION_DEFAULT"/>
            <property name="propagationBehaviorName" value="PROPAGATION_REQUIRED"/>
            <property name="timeout" value="300"/>
        </bean>

        <bean id="transactionTemplateNew"
              class="org.springframework.transaction.support.TransactionTemplate">
            <property name="transactionManager" ref="transactionManager"/>
            <!--ISOLATION_DEFAULT 表示由使用的数据库决定  -->
            <property name="isolationLevelName" value="ISOLATION_DEFAULT"/>
            <property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW"/>
            <property name="timeout" value="300"/>
        </bean>

    </beans>

  • 相关阅读:
    Java暑期学习第三十一天日报
    使用dataadapter和dataset更新数据库
    一些很酷的.Net技
    一个阴历阳历互相转化的类(c#农历)
    详细讲解简洁、明晰!数据库设计三大范式应用实例
    WinForm下ListBox控件“设置DataSource属性后无法修改项集合”的问题解决方案
    CONVERT函数方法大全(经验总结)
    使用C#将字符串转换成十六进制以及其逆转换
    List myList = new List()有关的方法及属性
    C#实现P2P之UDP穿透NAT及其原理讲解11
  • 原文地址:https://www.cnblogs.com/cxlings/p/6043168.html
Copyright © 2011-2022 走看看