springBoot搭建多数据源

先看一看项目的目录结构

 

看完整体的项目结构后,下面开始介绍搭建springboot的多数据源搭建步骤。

步骤1:在application.properties中配置数据源

#第一个数据库
spring.datasource.first.driver-class-name = oracle.jdbc.OracleDriver
spring.datasource.first.jdbc-url = jdbc:oracle:thin:@127.0.0.1:1521:orcl
spring.datasource.first.username = admin
spring.datasource.first.password = admin
#第二个数据库
spring.datasource.second.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.second.jdbc-url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
spring.datasource.second.username=admin
spring.datasource.second.password=admin

步骤2:在程序入口类Application.java中添加以下代码

/**
* 配置多数据源
*/
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    // TODO Auto-generated method stub
    return builder.sources(MonitorApplication.class);
}

步骤3:项目中更改或增加如图红色部分包和文件夹路径

 

注意:1和2的名称要一致

步骤4:编写DataSourceConfigFirst.java和DataSourceConfigSecond.java

DataSourceConfigFirst.java源代码

import javax.sql.DataSource;

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.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

//表示这个类为一个配置类
@Configuration
//配置mybatis的接口类放的地方
@MapperScan(basePackages = "com.XXX.mapper.first", sqlSessionFactoryRef = "firstSqlSessionFactory")
public class DataSourceConfigFirst {
  // 将这个对象放入Spring容器中
  @Bean(name = "firstDataSource")
  // 表示这个数据源是默认数据源
  @Primary
  // 读取application.properties中的配置参数映射成为一个对象
  // prefix表示参数的前缀
  @ConfigurationProperties(prefix = "spring.datasource.first")
  public DataSource getDateSource1() {
      return DataSourceBuilder.create().build();
  }
  @Bean(name = "firstSqlSessionFactory")
  // 表示这个数据源是默认数据源
  @Primary
  // @Qualifier表示查找Spring容器中名字为firstDataSource的对象
  public SqlSessionFactory firstSqlSessionFactory(@Qualifier("firstDataSource") DataSource datasource)
          throws Exception {
      SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
      bean.setDataSource(datasource);
      //-------加入的代码开始:解决mybatis返回map时key值为空不返回的情况----
      org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
      configuration.setCallSettersOnNulls(true);
             bean.setConfiguration(configuration);
      //-------加入的代码结束---------------------------------------
      bean.setMapperLocations(
      // 设置mybatis的xml所在位置
      new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/first/*.xml"));
      return bean.getObject();
  }
  @Bean("firstSqlSessionTemplate")
  // 表示这个数据源是默认数据源
  @Primary
  public SqlSessionTemplate firstsqlsessiontemplate(
          @Qualifier("firstSqlSessionFactory") SqlSessionFactory sessionfactory) {
      return new SqlSessionTemplate(sessionfactory);
  }
}

  

DataSourceConfigSecond.java源代码

import javax.sql.DataSource;

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;
@Configuration
@MapperScan(basePackages = "com.XXX.mapper.second", sqlSessionFactoryRef = "secondSqlSessionFactory")
public class DataSourceConfigSecond {
    @Bean(name = "secondDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.second")
    public DataSource getDateSource2() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "secondSqlSessionFactory")
    public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/second/*.xml"));
        return bean.getObject();
    }
    @Bean("secondSqlSessionTemplate")
    public SqlSessionTemplate secondsqlsessiontemplate(
            @Qualifier("secondSqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }
}

Springboot多数据源配置完成,现在就可以开发了。开发的时候第一个数据源的mapper接口类和xml文件要写到first目录中,第二个数据源的mapper接口类和xml文件要写到second目录中。

posted @ 2019-03-01 17:05  麦田守护梦  阅读(615)  评论(0编辑  收藏  举报