ssm使用双数据源

工作中需要接入其他公司业务系统的数据进行分析,于是接入它们的db。

使用双数据源配置感觉如下:

  database、sessionFactory、扫描器、事务管理器等双份。 听说如果两个数据源需要一起使用加事务的时候有点恶心,没有遇到该业务问题所以没有深入。

 

具体配置:

  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="com.kunpu.appopm.dal.config.JDBCPlaceholderConfigurer">
        <property name="order" value="2"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="location" value="file:/opt/configs/appopm/datasource.properties"/>
    </bean>

    <bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        <property name="driverClassName" value="${datasource.driverClassName}"/>
        <property name="url" value="${datasource1.url}"/>
        <property name="username" value="${datasource1.username}"/>
        <property name="password" value="${datasource1.password}"/>
        <property name="connectionProperties" value="${datasource.connectionProperties}"/>
        <property name="initialSize" value="${datasource.initialsize}"/>
        <property name="maxActive" value="${datasource.maxActive}"/>
        <property name="minIdle" value="${datasource.minIdle}"/>
        <property name="maxWait" value="${datasource.maxWait}"/>
        <property name="validationQuery" value="SELECT 1 FROM DUAL"/>
        <property name="testOnBorrow" value="${datasource.testOnBorrow}"/>
        <property name="testOnReturn" value="${datasource.testOnReturn}"/>
        <property name="testWhileIdle" value="${datasource.testWhileIdle}"/>
        <property name="timeBetweenEvictionRunsMillis"
                  value="${datasource.timeBetweenEvictionRunsMillis}"/>
        <property name="minEvictableIdleTimeMillis"
                  value="${datasource.minEvictableIdleTimeMillis}"/>
        <property name="removeAbandoned" value="${datasource.removeAbandoned}"/>
        <property name="removeAbandonedTimeout" value="${datasource.removeAbandonedTimeoutMillis}"/>
        <property name="logAbandoned" value="${datasource.logAbandoned}"/>
        <property name="filters" value="${datasource.filters}"/>
        <property name="poolPreparedStatements" value="${datasource.poolPreparedStatements}"/>
        <property name="maxPoolPreparedStatementPerConnectionSize"
                  value="${datasource.maxPoolPreparedStatementPerConnectionSize}"/>
    </bean>

    <bean id="sessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource1"/>
        <property name="configLocation" value="classpath:/configs/mybatis-config.xml"/>
        <property name="mapperLocations">
            <list>
                <value>classpath:/mapper1/**/*.xml</value>
            </list>
        </property>
        <property name="typeAliasesPackage" value="com.kunpu.appopm.dal.model"/>
        <property name="plugins">
            <list>
                <bean class="com.kunpu.appopm.common.util.log.SQLLogInterceptor"/>
                <bean class="com.kunpu.frameworks.cat.util.CatDBInterceptor"/>
                <bean class="com.kunpu.frameworks.db.pagination.PaginationInterceptor">
                    <property name="dialect">
                        <bean class="com.kunpu.frameworks.db.pagination.MySqlDialect"/>
                    </property>
                </bean>
            </list>
        </property>
    </bean>

    <bean id="scanner1" class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sessionFactory1"/>
        <property name="basePackage" value="com.kunpu.appopm.dal"/>
        <property name="annotationClass" value="com.kunpu.appopm.dal.config.BaseDB"/>
        <property name="properties">
            <value>
                mappers=tk.mybatis.mapper.common.Mapper
                IDENTITY=MYSQL
                notEmpty=true
            </value>
        </property>
    </bean>

    <bean id="sqlSessionTemplate1" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sessionFactory1"/>
    </bean>

    <bean id="transactionManager1"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource1"/>
    </bean>

    <!-- 事务模板 -->
    <bean id="transactionTemplate1"
          class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="transactionManager1"/>
        <constructor-arg name="transactionManager" ref="transactionManager1"/>
    </bean>

    <bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        <property name="driverClassName" value="${datasource.driverClassName}"/>
        <property name="url" value="${datasource2.url}"/>
        <property name="username" value="${datasource2.username}"/>
        <property name="password" value="${datasource2.password}"/>
        <property name="connectionProperties" value="${datasource.connectionProperties}"/>
        <property name="initialSize" value="${datasource.initialsize}"/>
        <property name="maxActive" value="${datasource.maxActive}"/>
        <property name="minIdle" value="${datasource.minIdle}"/>
        <property name="maxWait" value="${datasource.maxWait}"/>
        <property name="validationQuery" value="SELECT 1 FROM DUAL"/>
        <property name="testOnBorrow" value="${datasource.testOnBorrow}"/>
        <property name="testOnReturn" value="${datasource.testOnReturn}"/>
        <property name="testWhileIdle" value="${datasource.testWhileIdle}"/>
        <property name="timeBetweenEvictionRunsMillis"
                  value="${datasource.timeBetweenEvictionRunsMillis}"/>
        <property name="minEvictableIdleTimeMillis"
                  value="${datasource.minEvictableIdleTimeMillis}"/>
        <property name="removeAbandoned" value="${datasource.removeAbandoned}"/>
        <property name="removeAbandonedTimeout" value="${datasource.removeAbandonedTimeoutMillis}"/>
        <property name="logAbandoned" value="${datasource.logAbandoned}"/>
        <property name="filters" value="${datasource.filters}"/>
        <property name="poolPreparedStatements" value="${datasource.poolPreparedStatements}"/>
        <property name="maxPoolPreparedStatementPerConnectionSize"
                  value="${datasource.maxPoolPreparedStatementPerConnectionSize}"/>
    </bean>

    <bean id="scanner2" class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sessionFactory2"/>
        <property name="basePackage" value="com.kunpu.appopm.dal"/>
        <property name="annotationClass" value="com.kunpu.appopm.dal.config.BaseDB2"/>
        <property name="properties">
            <value>
                mappers=tk.mybatis.mapper.common.Mapper
                IDENTITY=MYSQL
                notEmpty=true
            </value>
        </property>
    </bean>

    <bean id="sessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource2"/>
        <property name="configLocation" value="classpath:/configs/mybatis-config.xml"/>
        <property name="mapperLocations">
            <list>
                <value>classpath:/mapper2/**/*.xml</value>
            </list>
        </property>
        <property name="typeAliasesPackage" value="com.kunpu.appopm.dal.model"/>
        <property name="plugins">
            <list>
                <bean class="com.kunpu.appopm.common.util.log.SQLLogInterceptor"/>
                <bean class="com.kunpu.frameworks.cat.util.CatDBInterceptor"/>
                <bean class="com.kunpu.frameworks.db.pagination.PaginationInterceptor">
                    <property name="dialect">
                        <bean class="com.kunpu.frameworks.db.pagination.MySqlDialect"/>
                    </property>
                </bean>
            </list>
        </property>
    </bean>

    <bean id="sqlSessionTemplate2" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sessionFactory2"/>
    </bean>

    <bean id="transactionManager2"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource2"/>
    </bean>

    <!-- 事务模板 -->
    <bean id="transactionTemplate2"
          class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="transactionManager1"/>
        <constructor-arg name="transactionManager" ref="transactionManager1"/>
    </bean>
View Code

这里扫描指定dao使用的是basePage配合指定注解,应该是一点亮点吧。

 

posted @ 2018-09-26 15:17  guodaxia  阅读(535)  评论(0编辑  收藏  举报