springboot多数据源配置(分包方法)

yml 配置

spring:
   datasource:
      cmgr: 
        type: com.alibaba.druid.pool.DruidDataSource
        username: root
        password: 123456
        url: jdbc:sqlserver://localhost;Databasename=CMGR
        driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      gwchatuser: 
        type: com.alibaba.druid.pool.DruidDataSource
        username: root
        password: 123456
        url: jdbc:sqlserver://localhost;Databasename=GWCHATUSER
        driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver

主数据库代码配置

package com.centaline.sqlserver.config;

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.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
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.centaline.sqlserver.dao.cmgr", sqlSessionFactoryRef = "cmgrSqlSessionFactory")
public class DateSourceCMGRConfig {
    @Bean(name = "cmgrDataSource")
    // 表示这个数据源是默认数据源
    @Primary
    // 读取application.properties中的配置参数映射成为一个对象
    // prefix表示参数的前缀
    @ConfigurationProperties(prefix = "spring.datasource.cmgr")
    public DataSource getDateSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "cmgrSqlSessionFactory")
    // 表示这个数据源是默认数据源
    @Primary
    // @Qualifier表示查找Spring容器中名字为cmgrDataSource的对象
    public SqlSessionFactory sqlSessionFactory(@Qualifier("cmgrDataSource") DataSource datasource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver()
                        .getResources("classpath:com/centaline/sqlserver/mapper/cmgr/*Mapper.xml"));
        return bean.getObject();
    }

    @Bean("cmgrSqlSessionTemplate")
    // 表示这个数据源是默认数据源
    @Primary
    public SqlSessionTemplate sqlsessiontemplate(@Qualifier("cmgrSqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }
}

第二数据库代码配置

package com.centaline.sqlserver.config;

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.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

//表示这个类为一个配置类
@Configuration
// 配置mybatis的接口类放的地方
@MapperScan(basePackages = "com.centaline.sqlserver.dao.gwchatuser", sqlSessionFactoryRef = "gwchatuserSqlSessionFactory")
public class DateSourceGWCHATUSERConfig {
    @Bean(name = "gwchatuserDataSource")
    // 读取application.properties中的配置参数映射成为一个对象
    // prefix表示参数的前缀
    @ConfigurationProperties(prefix = "spring.datasource.gwchatuser")
    public DataSource getDateSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "gwchatuserSqlSessionFactory")
    // @Qualifier表示查找Spring容器中名字为gwchatuserDataSource的对象
    public SqlSessionFactory SqlSessionFactory(@Qualifier("gwchatuserDataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver()
                        .getResources("classpath:com/centaline/sqlserver/mapper/gwchatuser/*Mapper.xml"));
        return bean.getObject();
    }

    @Bean("gwchatuserSqlSessionTemplate")
    public SqlSessionTemplate sqlsessiontemplate(
            @Qualifier("gwchatuserSqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }
}

你可能感兴趣的:(springboot多数据源配置(分包方法))