【mybatis】多数据源实现

Mybatis多数据库兼容

mysql+oracle..


1.mybatis配置两个数据库

xff.mysql.datasource.driver=com.mysql.jdbc.Driver
#xff.mysql.datasource.url=jdbc:mysql://122.***.9.**:19087/
xff.mysql.datasource.url=jdbc:mysql://10.45.**.**:3306/testdb?useUnicode=true&characterEncoding=utf-8&useSSL=false
xff.mysql.datasource.username=root
xff.mysql.datasource.password=root

xff.oracle.datasource.driver=oracle.jdbc.driver.OracleDriver
xff.oracle.datasource.url=jdbc:oracle:thin:@10.45.**.**:1521:testDb
xff.oracle.datasource.username=root
xff.oracle.datasource.password=root

在mybatis配置文件配置两个数据源

<bean id="datasource1" parent="parentDataSource" init-method="init" destroy-method="close">
    <property name="url" value="${xff.mysql.datasource.url}"/>
    <property name="username" value="${xff.mysql.datasource.username}"/>
    <property name="password" value="${xff.mysql.datasource.password}"/>
    <property name="driverClassName" value="${xff.mysql.datasource.driver}"/>
</bean>

<bean id="datasource2" parent="parentOracleDataSource" init-method="init" destroy-method="close">
    <property name="url" value="${xff.oracle.datasource.url}"/>
    <property name="username" value="${xff.oracle.datasource.username}"/>
    <property name="password" value="${xff.oracle.datasource.password}"/>
    <property name="driverClassName" value="${xff.oracle.datasource.driver}"/>
</bean>

<bean id="dataSource" class="com.xff.simulation.common.bean.MultiDataSource">
    <property name="targetDataSources">
        <map key-type="java.lang.String">
            <!-- 选择数据源 -->
            <entry value-ref="datasource2" key="oracle"/>
        </map>
    </property>
    <!-- 选择数据源 -->
    <property name="defaultTargetDataSource" ref="datasource2"/>
</bean>

2.配置databaseIdProvider

<bean id="sqlSessionFactory"
      class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="vfs" value="org.mybatis.spring.boot.autoconfigure.SpringBootVFS"/>
    <!-- 自动扫描Mapping.xml文件 -->
    <property name="mapperLocations">
        <array>
            <value>classpath*:/mapper/*.xml</value>
        </array>
    </property>
    <property name="databaseIdProvider" ref="databaseIdProvider" />
    <property name="configLocation" value="classpath:mybatis/mybatisConfig.xml"/>
</bean>
<bean id="vendorProperties"
      class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <props>
            <prop key="Oracle">oracle</prop>
            <prop key="MySQL">mysql</prop>
        </props>
    </property>
</bean>
<!-- 配置databaseIdProvider-->
<bean id="databaseIdProvider" class="org.apache.ibatis.mapping.VendorDatabaseIdProvider">
    <property name="properties" ref="vendorProperties"/>
</bean>

3.使用:如果只是简单的增删改查,没有用到函数,一套sql可以共用,不必加databaseId,如果负载sql需要区分

<select id="qryAllUserInfo" databaseId="oracle" parameterType="****" >
    select * from sys_user
</select>
<select id="qryAllUserInfo" databaseId="mysql" parameterType="****" >
    select * from sys_user
</select>

另一种多数据库配置方式:

https://www.cnblogs.com/Donnnnnn/p/12073424.html


使用Mybatis-plus的dynamic-datasource-spring-boot-starter包实现多数据源

引入依赖

<!--dynamic-datasource-多数据源-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
    <version>${dynamic.datasource.boot.version}</version>
</dependency>

配置如下

#数据库配置
spring:
  autoconfigure:
    exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure  # 为了某些版本的springboot  @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}) 无法生效
  datasource:
    dynamic:
      primary: master #设置默认的数据源或者数据源组,默认值即为master,如果只是单数据源只需要注释掉slave相关配置即可
      datasource:
        master:
          url: jdbc:oracle:thin:@localhost:1521:ORCL
          username: xxx
          password: password
          driverClassName: oracle.jdbc.OracleDriver
        #多数据源使用场景
        #slave:
          #url: jdbc:oracle:thin:@localhost:1521:ORCL
          #username: xxx
          #password: password
          #driverClassName: oracle.jdbc.driver.OracleDriver
        #secure:
          #url: jdbc:oracle:thin:@192.168.2.16:1521:ORCL
          #username: xxx
          #password: password
          #driverClassName: oracle.jdbc.OracleDriver

配置好后,在对应的mapper接口上添加@DS(“xxx”)注解可以指定该接口使用哪个数据源来查询数据。
默认不添加或者找不到对应的数据源的情况下都会使用默认的primary指定的主数据库。

数据库方言可以设置autoRuntimeDialect为true