• spring mybatis 多数据源配置


    项目上遇到多数据源的问题,分别是阿里的DRDS和ADB数据库,他们的语法都是一样通用mysql语法。

    不同点:DRDS有事务,,ADB无事务。

    所以,数据源需要针对调用不同的接口来来进行切换。

    整个处理过程,经历了比较坎坷的过程。

    1.使用spring的切面+注解@DataSource来解决。

    问题:在同一线程中抛出异常后,后续的异常处理方法,切换数据源失败。

    2.使用注解+切面,在BO层进行处理。

    问题:代码侵入性高,需要解决事务问题。

    3.使用mybatis的多数据源,多MapperScan,多sqlSessionFactory配置。

    虽然也有确定,但是这个不需要多个数据源在切面来控制,需要改的有两点:

     A:   Mapper.xml所在的文件需要分开指定

     B : Mapper.java所在包需要分开指定

    具体配置如下:

    1.数据源dataSource

     <bean id="ims-druidDataSource-drds" class="com.alibaba.druid.pool.DruidDataSource"
              init-method="init" destroy-method="close" primary="true">
            <!-- 基本属性 url、user、password -->
            <property name="url" value="${spring.datasource.drds.url}"/>
            <property name="username" value="${spring.datasource.drds.username}"/>
            <property name="password" value="${spring.datasource.drds.password}"/>
            <property name="driverClassName" value="${spring.datasource.driver-class-name}"/>
            <!-- 配置初始化大小、最小、最大 -->
            <property name="initialSize" value="5"/>
            <property name="minIdle" value="5"/>
            <property name="maxActive" value="100"/>
            <!-- 配置获取连接等待超时的时间 -->
            <property name="maxWait" value="60000"/>
            <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
            <property name="timeBetweenEvictionRunsMillis" value="60000"/>
            <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
            <property name="minEvictableIdleTimeMillis" value="300000"/>
            <property name="validationQuery" value="SELECT 'x'"/>
            <!--单位:秒,检测连接是否有效的超时时间,底层调用jdbc Statement对象的void setQueryTimeout(int seconds)方法-->
            <property name="validationQueryTimeout" value="30"/>
            <property name="testWhileIdle" value="true"/>
            <property name="testOnBorrow" value="true"/>
            <property name="testOnReturn" value="false"/>
            <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
            <property name="poolPreparedStatements" value="false"/>
            <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
            <property name="asyncInit" value="false"/>
    
        </bean>
       <bean id="ims-druidDataSource-adb" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close" primary="false">
           <property name="url" value="${spring.datasource.adb.url}"/>
           <property name="username" value="${spring.datasource.adb.username}"/>
           <property name="password" value="${spring.datasource.adb.password}"/>
           <property name="driverClassName" value="${spring.datasource.driver-class-name}"/>
           <property name="initialSize" value="1"/>
           <property name="minIdle" value="1"/>
           <property name="maxActive" value="20"/>
           <property name="maxWait" value="60000"/>
           <property name="timeBetweenEvictionRunsMillis" value="60000"/>
           <property name="minEvictableIdleTimeMillis" value="300000"/>
           <property name="validationQuery" value="SELECT 'x'"/>
           <property name="validationQueryTimeout" value="30"/>
           <property name="testWhileIdle" value="true"/>
           <property name="testOnBorrow" value="false"/>
           <property name="testOnReturn" value="false"/>
           <property name="poolPreparedStatements" value="false"/>
           <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
           <property name="asyncInit" value="false"/>
       </bean>

    2.SqlSessionFactoryBean 配置

     <bean id="drdsSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="ims-druidDataSource-drds"/>
            <property name="configLocation" value="${mybatis.config-location}"/>
            <property name="typeAliasesPackage" value="cn.hsa.ims.*.entity"/>
            <property name="mapperLocations">
                <array>
                    <value>classpath*:sql/mysql/**/*Mapper.xml</value>
                </array>
            </property>
            <property name="plugins">
                <array>
                    <bean class="com.github.pagehelper.PageInterceptor">
                        <property name="properties">
                            <value>
                                helperDialect=${pagehelper.helperDialect}
                            </value>
                        </property>
                    </bean>
                </array>
            </property>
            <property name="databaseIdProvider" ref="databaseIdProvider"/>
        </bean>
    
        <bean id="adbSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="ims-druidDataSource-adb"/>
            <property name="configLocation" value="${mybatis.config-location}"/>
            <property name="typeAliasesPackage" value="cn.hsa.ims.*.entity"/>
            <property name="mapperLocations">
                <array>
                    <value>classpath*:sql/adb/**/*Mapper.xml</value>
                </array>
            </property>
            <property name="plugins">
                <array>
                    <bean class="com.github.pagehelper.PageInterceptor">
                        <property name="properties">
                            <value>
                                helperDialect=${pagehelper.helperDialect}
                            </value>
                        </property>
                    </bean>
                </array>
            </property>
            <property name="databaseIdProvider" ref="databaseIdProvider"/>
        </bean>
    <!--配置一个databaseIdProvider sql如果有方言,自己增加databaseId属性-->
        <bean id="databaseIdProvider" class="org.apache.ibatis.mapping.VendorDatabaseIdProvider">
            <property name="properties">
                <props>
                    <prop key="MySQL">mysql</prop>
                    <prop key="Oracle">oracle</prop>
                    <prop key="SQL Server">sqlserver</prop>
                    <prop key="DB2">db2</prop>
                </props>
            </property>
        </bean>

    3.对应的mapperScan配置

    <bean id="drdsMapperScan" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="sqlSessionFactoryBeanName" value="drdsSqlSessionFactory"/>
            <property name="basePackage" value="cn.hsa.ims.*.dao"/>
        </bean>
    
        <bean id="adbMapperScan" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="sqlSessionFactoryBeanName" value="adbSqlSessionFactory"/>
            <property name="basePackage" value="cn.hsa.ims.*.adb.dao"/>
        </bean>

    注意:

    如果使用springboot,需要在启动的时候扫描的@MapperSacan去除。

  • 相关阅读:
    45到数据库查询题
    Error: Could not link: /usr/local/share/doc/homebrew
    根据两点坐标,计算连线与坐标轴间的夹角(弧度、角度)
    excel2json
    Mac下的unity兼容问题,打开项目提示错误:!GetPersistentManager().IsStreamLoaded(assetPath)
    Linker Command failed with exit code 1
    module.exports与exports区别
    Nginx配置SSL证书部署HTTPS方法
    Option path is not valid. Please refer to the README.
    javascript中call()、apply()、bind()的用法终于理解
  • 原文地址:https://www.cnblogs.com/sloveling/p/mulit_datasource.html
Copyright © 2020-2023  润新知