• 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配合指定注解,应该是一点亮点吧。

  • 相关阅读:
    sql中的并、交、差
    白水的sql需求:每个病人的对应最小诊断类别|partition|
    |转|oracle行转列点评oracle11g sql新功能pivot/unpivot
    oracle wm_concat(column)函数的使用不同公司支付同一客商(行转列)|转|
    记录那些删除的伴我成长的触发器
    PL/SQL Developer使用技巧|F=Forwarding|
    手工创建数据库 ocp课程 wait for
    Oracle数据库监听配置|转|
    最新解决的sql:病人相邻两次看病小于3天
    tns的一些常见错误分析实例 |转|
  • 原文地址:https://www.cnblogs.com/aigeileshei/p/9707148.html
Copyright © 2020-2023  润新知