spring boot 配置多套数据源

问题:需要在多个数据库中查询数据,不适用sql中的use语句。

导包 pom.xml

<!-- mybatis,使用mybatis-plus也行,虽然plus的单表查询很强,在大部分情况下需要编写复杂繁琐的sql语句,用不上plus -->
<!-- 连接池用mybatis或者spring boot自带的就行。 -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.2</version>
</dependency>
<!-- jdbc,连接的数据库是微软的sql server -->
<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>7.4.1.jre8</version>
    <scope>runtime</scope>
</dependency>

配置数据源 application.yml

spring:
  #数据源
  datasource:
    #url1
    db1:
      jdbc-url: jdbc:sqlserver://192.168.2.1:1433;databasename=TonsOfficeA # 高版本的spring使用jdbc-url,不能使用url
      username: sa
      password: xxx
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
    #url2
    db2:
      jdbc-url: jdbc:sqlserver://192.168.100.159:1433;databasename=AgentFlow
      username: sa
      password: xxx
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver

配置类 DataSourceConfig1

package com.tons.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
// 扫描com.tons.mapper.db1包下所有@mapper修饰的类,使用db1SqlSessionFactory创建sqlSession(db1SqlSessionFactory使用了db1DataSource作为数据源,并扫描classpath*:mapper/db1/*.xml的mapper.xml文件)
@MapperScan(basePackages = "com.tons.mapper.db1", sqlSessionFactoryRef = "db1SqlSessionFactory")
public class DataSourceConfig1 {

    // 读取application.yml中的配置参数映射成为 db1DataSource
    @Bean("db1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db1") 
    public DataSource getDb1DataSource(){
        return DataSourceBuilder.create().build();
    }

    // 配置sql会话工厂
    @Bean("db1SqlSessionFactory")
    public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        // 这里配置mapper文件配置,简单说就 mapper/db1/下面的所有.xml使用db1DataSource做数据源
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db1/*.xml"));
        return bean.getObject();
    }

    // 配置sql会话模板
    @Bean("db1SqlSessionTemplate")
    public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

DataSourceConfig2

package com.tons.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.tons.mapper.db2", sqlSessionFactoryRef = "db2SqlSessionFactory")
public class DataSourceConfig2 {

    @Bean("db2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db2")
    public DataSource getDb1DataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean("db2SqlSessionFactory")
    public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db2/*.xml"));
        return bean.getObject();
    }

    @Bean("db2SqlSessionTemplate")
    public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

使用就和单数据源一样,mapper接口和mapper.xml文件对应使用即可

posted @ 2022-12-23 10:52  黑人的乔丹鞋  阅读(65)  评论(0编辑  收藏  举报