springboot-mybatis多数据源配置(超简单!)

25 篇文章 1 订阅
24 篇文章 0 订阅

不知已经遇到多少次,需要多数据源的场景了,今天把常用配置写下来,非常简单。这里两个数据源命名为from和to

一、yml配置

这里springboot默认的数据源配置就不要写了,以免冲突

# 开发环境配置
server:
  # 服务器的HTTP端口,默认为80
  port: 80

# 日志配置
logging:
  level:
    xyz.hashdog: debug

datasynchronism:
  datasource:
    #获取
    from:
      driverClassName: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/from?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
      username: root
      password: root
      #写入
    to:
      driverClassName: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/to?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
      username: root
      password: root

二、第一个数据源 to的configbean配置

package xyz.hashdog.datasynchronism.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
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;

import javax.sql.DataSource;

/**
 * @author th
 * @description: to数据源
 * @projectName hashdog
 * @date 2020/2/1217:41
 */
@Configuration
@MapperScan(basePackages = "xyz.hashdog.datasynchronism.to.dao", sqlSessionFactoryRef = "toFactory")
public class ToDruidConfig {
    /**
     * 封装数据源对象创建, 该方法就已经将数据源的各个数据封装到该对象中
     * @return
     */
    @Bean(name = "toDataSource")
    @Primary //必须要有, 说明该数据源是默认数据源
    @ConfigurationProperties(prefix = "datasynchronism.datasource.to") //读取的数据源前缀, 跟yml文件对应
    public DataSource toDataSource(){
        return new DruidDataSource();
    }

    /**
     * SqlSession对象创建
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Bean(name = "toFactory")
    @Primary//必须要有, 说明该数据源是默认数据源
    public SqlSessionFactory toFactory(@Qualifier("toDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        //指定全限定别名
        bean.setTypeAliasesPackage("xyz.hashdog.datasynchronism.bean");
        bean.setDataSource(dataSource);
        //指定该SqlSession对应的mapper.xml文件位置
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/to/*.xml"));
        //开启驼峰转换
        SqlSessionFactory sqlSessionFactory = bean.getObject();
        org.apache.ibatis.session.Configuration configuration =  sqlSessionFactory.getConfiguration();
        configuration.setMapUnderscoreToCamelCase(true);
        return bean.getObject();
    }



}

三、第二个数据源from的configbean配置

package xyz.hashdog.datasynchronism.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
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;

import javax.sql.DataSource;

/**
 * @author th
 * @description: TODO
 * @projectName hashdog
 * @date 2020/2/1217:38
 */
@Configuration
@MapperScan(basePackages = "xyz.hashdog.datasynchronism.from.dao", sqlSessionFactoryRef = "fromFactory")
class FromDruidConfig {

    @Bean(name = "fromDataSource")
    @ConfigurationProperties(prefix = "datasynchronism.datasource.from")
    public DataSource fromDataSource(){
        return new DruidDataSource();
    }

    @Bean(name = "fromFactory")
    public SqlSessionFactory fromFactory(@Qualifier("fromDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        //指定包别名
        bean.setTypeAliasesPackage("xyz.hashdog.datasynchronism.bean");
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/from/*.xml"));
        SqlSessionFactory sqlSessionFactory = bean.getObject();
        org.apache.ibatis.session.Configuration configuration =  sqlSessionFactory.getConfiguration();
        configuration.setMapUnderscoreToCamelCase(true);
        return bean.getObject();
    }
}

四、项目结构

重点已经框出来了,一定要和配置中的包路径一致

 

五、项目源码

(只实现了多数据源)

https://download.csdn.net/download/corleone_4ever/12152949

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值