Springboot+mybatis-plus配置多数据源(连接多个数据库)

2023-03-26 21:28:59

先来整体看一下我的项目结构:

在这里插入图片描述

现在开始说多数据源配置:

1、配置yml

在这里插入图片描述
上图需要注意的是连接用jdbc-url,要不然会报错:
在这里插入图片描述

2、配置数据源的config

在这里插入图片描述
在这里插入图片描述
这里说一下bean.setTypeEnumsPackage(“com.cn.slt.exchange.enums”)这句话,如果这行代码注释了,但是在你的yml里面配置了:
在这里插入图片描述
如上图:这种方法是不生效了,测试一下,查询一个带有枚举类型的实体类,会报错:
在这里插入图片描述
这里一定要注意!!!!

3、启动类要配置扫描包,就是初始化spring的时候能找到你的DataSourceNewsConfig和DataSourceOldConfig文件并且初始化

在这里插入图片描述
下面附上我的两个配置文件代码:

package com.cn.slt.exchange.config;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
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 javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.cn.slt.exchange.dao.old",sqlSessionTemplateRef  = "oldSqlSessionTemplate")
public class DataSourceOldConfig {

    @Bean(name = "oldDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.old")
    @Primary
    public DataSource testDataSource() {
        System.out.println("初始化老的数据源");
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "oldSqlSessionFactory")
    @Primary
    public SqlSessionFactory oldSqlSessionFactory(@Qualifier("oldDataSource") DataSource dataSource) throws Exception {
        //SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setTypeEnumsPackage("com.cn.slt.exchange.enums");
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/old/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "oldSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("oldSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

package com.cn.slt.exchange.config;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
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
@MapperScan(basePackages = "com.cn.slt.exchange.dao.news",sqlSessionTemplateRef  = "newsSqlSessionTemplate")
public class DataSourceNewsConfig {

    @Bean(name = "newsDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.news")
    public DataSource testDataSource() {
        System.out.println("初始化新的数据源");
         return DataSourceBuilder.create().build();
    }

    @Bean(name = "newsSqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("newsDataSource") DataSource dataSource) throws Exception {
        //SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setTypeEnumsPackage("com.cn.slt.exchange.enums");  //多数据源配置了后必须在这里设置enums的指向包,在pom里面设置无效
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/news/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "newsTransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("newsDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "newsSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("newsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

END!!!

  • 作者:王子羽落
  • 原文链接:https://blog.csdn.net/qq_41080341/article/details/109206718
    更新时间:2023-03-26 21:28:59