SpringBoot多数据源配置,基本适用所有流行的数据库

基于dynamic-datasource实现多数据源

dynamic-datasource介绍

dynamic-datasource-spring-boot-starter 是一个基于springboot的快速集成多数据源的启动器。

其支持 Jdk 1.7+, SpringBoot 1.4.x 1.5.x 2.x.x

dynamic-datasource特性

  • 可采用数据库和配置文件维护数据源
  • 自动加载、移除数据源(数据来源为数据库方式)
  • 可以使用接口参数自动切换数据源
  • 同一数据源下切换schema(对于postgresql用的多)
  • 多层数据源嵌套切换使用方案
  • 自定义数据源切换逻辑

启动类

@MapperScan("com.hylink.mapper.*")

依赖包

<!-- 多数据源配置 -->
<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
  <version>1.4.2</version>
</dependency>
<!-- clickhouse -->
<dependency>
  <groupId>ru.yandex.clickhouse</groupId>
  <artifactId>clickhouse-jdbc</artifactId>
  <version>0.2.4</version>
</dependency>
<!-- postgresSql -->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.3.2</version>
</dependency>

yaml配置

Spring:
  datasource:
    click:
      driver-class-name: ru.yandex.clickhouse.ClickHouseDriver
      url: jdbc:clickhouse://192.168.3.233:8123/hlk_zfjd
      username: default
      password: Hylink2014@clickhouse
    pgsql:
      driver-class-name: org.postgresql.Driver
      url: jdbc:postgresql://192.168.3.233:5432/zfjd?currentSchema=zfjd
      username: postgres
      password: Hylink2014@postgres
	  
mybatis-plus:
  # xml地址
  mapper-locations: classpath:mapper/*/*.xml
  # 实体扫描,多个package用逗号或者分号分隔
  type-aliases-package: com.hylink.entity.*  #自己的实体类地址
  configuration:
    # 驼峰命名
    map-underscore-to-camel-case: true
  global-config:
    db-config:
      logic-delete-value: 1 #逻辑删除 默认为1
      logic-not-delete-value: 0 #逻辑未删除 默认为0	  

多数据源配置

/**
 * 多数据源配置
 * @author wenjiawei
 */
@Configuration
public class DataSourceConfig {

    /**
     * 主数据源配置 click数据源
     * @return
     */
    @Primary
    @Bean(name = "clickDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.click")
    public DataSourceProperties clickDataSourceProperties() {
        return new DataSourceProperties();
    }

    /**
     * 主数据源 click数据源
     * @param dataSourceProperties
     * @return
     */
    @Primary
    @Bean(name = "clickDataSource")
    public DataSource clickDataSource(@Qualifier("clickDataSourceProperties") DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    /**
     * 第二个pgsql数据源配置
     * @return
     */
    @Bean(name = "pgsqlDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.pgsql")
    public DataSourceProperties pgsqlDataSourceProperties() {
        return new DataSourceProperties();
    }

    /**
     * 第二个pgsql数据源
     * @param dataSourceProperties
     * @return
     */
    @Bean("pgsqlDataSource")
    public DataSource pgsqlDataSource(@Qualifier("pgsqlDataSourceProperties") DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

}

pgsql数据源配置

package com.hylink.config;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
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;

/**
 * pgsql数据源配置
 * 多数据源配置依赖数据源配置
 * @author wenjiawei
 * @see  DataSourceConfig
 */
@Configuration
@MapperScan(basePackages ="com.hylink.mapper.pgsql", sqlSessionTemplateRef  = "pgsqlSqlSessionTemplate")
public class MybatisPlusConfigPgsql {

    /**
     * pgsql数据源
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Bean("pgsqlSqlSessionFactory")
    public SqlSessionFactory pgsqlSqlSessionFactory(@Qualifier("pgsqlDataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
                getResources("classpath:/mapper/pgsql/*.xml"));
        MybatisConfiguration configuration = new MybatisConfiguration();
        // mybatis-plus 3.4.0版本以上需要用MybatisPlusInterceptor,但实际使用未生效.因此使用旧版本
        configuration.addInterceptor(new PaginationInterceptor());
        configuration.setLogImpl(StdOutImpl.class);
        sqlSessionFactory.setConfiguration(configuration);
        SqlSessionFactory factory = sqlSessionFactory.getObject();
        factory.getConfiguration().setJdbcTypeForNull(JdbcType.NULL);
        return factory;
    }

    /**
     * 事务支持
     * @param dataSource
     * @return
     */
    @Bean(name = "pgsqlTransactionManager")
    public DataSourceTransactionManager pgsqlTransactionManager(@Qualifier("pgsqlDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "pgsqlSqlSessionTemplate")
    public SqlSessionTemplate pgsqlSqlSessionTemplate(@Qualifier("pgsqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

clickHouse配置

package com.hylink.config;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
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;

/**
 * clickHouse数据源配置
 * 多数据源配置依赖数据源配置
 * @author wenjiawei
 * @see  DataSourceConfig
 */
@Configuration
@MapperScan(basePackages ="com.hylink.mapper.click", sqlSessionTemplateRef  = "clickSqlSessionTemplate")
public class MybatisPlusConfigClick {

    /**
     * 主数据源 click数据源
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Primary
    @Bean("clickSqlSessionFactory")
    public SqlSessionFactory clickSqlSessionFactory(@Qualifier("clickDataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
                        getResources("classpath:/mapper/click/*.xml"));
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.addInterceptor(new PaginationInterceptor());
        configuration.setLogImpl(StdOutImpl.class);
        sqlSessionFactory.setConfiguration(configuration);
        SqlSessionFactory factory = sqlSessionFactory.getObject();
        factory.getConfiguration().setJdbcTypeForNull(JdbcType.NULL);
        return factory;
    }

    @Primary
    @Bean(name = "clickTransactionManager")
    public DataSourceTransactionManager clickTransactionManager(@Qualifier("clickDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean(name = "clickSqlSessionTemplate")
    public SqlSessionTemplate clickSqlSessionTemplate(@Qualifier("clickSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

实体类目录格式
注:mapper、xml格式同实体类一致
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值