SpringBoot+mybatis多数据源配置

 1.需要alibaba 的数据源的maven

 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>

2.在yml或properties配置数据源(我的是yml)

server:
  port: 8085


spring:
  datasource:
    files:
      driverClassName: com.mysql.jdbc.Driver
      username: root
      password: root
      # spring2.0此处为jdbc-url
      jdbc-url: jdbc:mysql:///hpt_files?useUnicode=yes&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
      type: com.alibaba.druid.pool.DruidDataSource
    center:
      driverClassName: com.mysql.jdbc.Driver
      username: root
      password: root
      # spring2.0此处为jdbc-url
      jdbc-url: jdbc:mysql:///hpt_controlcenter?useUnicode=yes&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
      type: com.alibaba.druid.pool.DruidDataSource
  mvc:

3.因为有两个数据源,所以我们需要把:mapper.xml,mapper(也是以前的dao接口),放入到不同的目录下,创建两个数据源的datasource

4.在datasource中,第一个数据源配置(必须指定主数据源!)

package com.wom.file.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;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
@Configuration//注解到spring容器中
@MapperScan(basePackages ="com.wom.file.mapper.files", sqlSessionTemplateRef  = "hptFilesSqlSessionTemplate")
public class FilesDataSource {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.files")
    @Primary
    public DataSource hptFilesDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public SqlSessionFactory hptFilesSqlSessionFactory(@Qualifier("hptFilesDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        // 开启驼峰命名
        //bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/files/*.xml"));
        return bean.getObject();
    }

    @Bean
    @Primary
    public DataSourceTransactionManager db1TransactionManager(@Qualifier("hptFilesDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    @Primary
    public SqlSessionTemplate hptFilesSqlSessionTemplate(@Qualifier("hptFilesSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

5.配置第二个数据源:

package com.wom.file.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;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration//注解到spring容器中
@MapperScan(basePackages ="com.wom.file.mapper.controlcenter", sqlSessionTemplateRef  = "centerSqlSessionTemplate")
public class ControlcenterDataSource {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.center")
    public DataSource centerDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public SqlSessionFactory centerSqlSessionFactory(@Qualifier("centerDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        // 开启驼峰命名
       // bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/controlcenter/*.xml"));
        return bean.getObject();
    }

    @Bean
    public DataSourceTransactionManager db2TransactionManager(@Qualifier("centerDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public SqlSessionTemplate centerSqlSessionTemplate(@Qualifier("centerSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

6.到此就结束。佛祖保佑,永不BUG!

原文地址:https://www.cnblogs.com/www-yang-com/p/15252143.html