Mybatis Plus + Spring 分包配置 ClickHouse 和 Mysql 双数据源

目录

一、背景

二、各个配置文件总览(文件位置因人而异)

[2.1 DataSourceConfig](#2.1 DataSourceConfig)

[2.2 MybatisClickHouseConfig (ClickHouse 配置类)](#2.2 MybatisClickHouseConfig (ClickHouse 配置类))

[2.3 MybatisMysqlConfig(Mysql 配置类)](#2.3 MybatisMysqlConfig(Mysql 配置类))

[2.4 application.properties 配置](#2.4 application.properties 配置)

[2.5 mapper 分包情况](#2.5 mapper 分包情况)

[2.6 主类中的 @MapperScan 去掉,因为在上面 ClickHouse 配置类 和 Mysql配置类 中已经定义](#2.6 主类中的 @MapperScan 去掉,因为在上面 ClickHouse 配置类 和 Mysql配置类 中已经定义)

[2.7 引用依赖(关键性依赖)](#2.7 引用依赖(关键性依赖))

三、参考大神,感谢分享!


一、背景

业务需求,连接池之前用的 Hikari,换成了 Druid注意一下 ~

我选择 分包 配置动态双数据源

二、各个配置文件总览(文件位置因人而异)

2.1 DataSourceConfig

java 复制代码
package xxx.xxx.xxx.common.config;

import javax.sql.DataSource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;

/**
 * 双数据源配置类
 *
 * @Author Elenda
 * @Date 2024/03/12 10:10
 * @Version 1.0
 */
@Configuration
public class DataSourceConfig {

    /**
     * Mysql
     *
     * @return DataSource
     */
    @Bean(name = "mysqlDatasource")
    @ConfigurationProperties(prefix = "spring.datasource.druid.mysql")
    public DataSource mysqlDatasource() {
        return new DruidDataSource();
    }

    /**
     * ClickHouse
     *
     * @return DataSource
     */
    @Bean(name = "clickHouseDatasource")
    @ConfigurationProperties(prefix = "spring.datasource.druid.clickhouse")
    public DataSource clickHouseDatasource() {
        return new DruidDataSource();
    }
}

2.2 MybatisClickHouseConfig (ClickHouse 配置类)

java 复制代码
package xxx.xxx.xxx.common.config;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * clickHouse数据源配置
 *
 * @Author Elenda
 * @Date 2024/03/12 10:10
 * @Version 1.0
 */
@Configuration
@MapperScan(basePackages = {"xxx.xxx.xxx.mapper.clickhouse"}, sqlSessionFactoryRef = "sqlSessionFactoryDsClickhouse")
public class MybatisClickHouseConfig {

    @Autowired
    @Qualifier("clickHouseDatasource")
    private DataSource clickHouseDatasource;

    @Bean
    public SqlSessionFactory sqlSessionFactoryDsClickhouse() throws Exception {
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        factoryBean.setDataSource(clickHouseDatasource);
        /*factoryBean.setMapperLocations(
                //设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath:/mappings/clickhouse/*.xml")
        );*/
        return factoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplateDsClickhouse() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryDsClickhouse());
        return template;
    }

    @Bean
    public DataSourceTransactionManager transactionManagerClickhouse() {
        return new DataSourceTransactionManager(clickHouseDatasource);
    }
}

2.3 MybatisMysqlConfig(Mysql 配置类)

java 复制代码
package xxx.xxx.xxx.common.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * mysql数据源配置
 *
 * @Author Elenda
 * @Date 2024/03/12 10:10
 * @Version 1.0
 */
@Configuration
@MapperScan(basePackages = {"xxx.xxx.xxx.mapper.mysql"}, sqlSessionFactoryRef = "sqlSessionFactoryDsMysql")
public class MybatisMysqlConfig {

    @Autowired
    @Qualifier("mysqlDatasource")
    private DataSource mysqlDatasource;

    @Bean
    @Primary
    public SqlSessionFactory sqlSessionFactoryDsMysql() throws Exception {
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        factoryBean.setDataSource(mysqlDatasource);
        factoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:/mappings/mysql/*.xml")
        );
        //向Mybatis过滤器链中添加拦截器
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        factoryBean.setPlugins(interceptor);
        return factoryBean.getObject();
    }

    @Bean
    @Primary
    public SqlSessionTemplate sqlSessionTemplateDsMysql() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryDsMysql());
        return template;
    }

    @Bean
    @Primary
    public DataSourceTransactionManager transactionManagerMysql() {
        return new DataSourceTransactionManager(mysqlDatasource);
    }

}

2.4 application.properties 配置

java 复制代码
# datasource 配置
# clickhouse
spring.datasource.druid.clickhouse.test-while-idle=true
spring.datasource.druid.clickhouse.validation-query=SELECT 1
spring.datasource.druid.clickhouse.username=username
spring.datasource.druid.clickhouse.password=password
spring.datasource.druid.clickhouse.url=url
spring.datasource.druid.clickhouse.driver-class-name=com.clickhouse.jdbc.ClickHouseDriver
spring.datasource.druid.clickhouse.type=com.alibaba.druid.pool.DruidDataSource

# mysql
spring.datasource.druid.mysql.test-while-idle=true
spring.datasource.druid.mysql.validation-query=SELECT 1
spring.datasource.druid.mysql.username=username
spring.datasource.druid.mysql.password=password
spring.datasource.druid.mysql.url=url
spring.datasource.druid.mysql.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.mysql.type=com.alibaba.druid.pool.DruidDataSource

其中,spring.datasource.druid.clickhouse.validation-query=SELECT 1

该选项用来验证数据库连接的有效性

2.5 mapper 分包情况

2.6 主类中的 @MapperScan 去掉,因为在上面 ClickHouse 配置类 和 Mysql配置类 中已经定义

2.7 引用依赖(关键性依赖)

XML 复制代码
<!-- druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.15</version>
</dependency>
<!-- clickhouse-->
<dependency>
    <groupId>com.clickhouse</groupId>
    <artifactId>clickhouse-jdbc</artifactId>
    <version>0.3.2-patch11</version>
</dependency>
<!-- mysql-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- mybatis-plus-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.3</version>
</dependency>

三、参考大神,感谢分享!

正确可用版:SpringBoot+Mybatis-plus+Druid 实现配置多数据源(数据库)_spring_Oxye-华为云开发者联盟前言此文介绍的是在SpringBoot中使用Mybatis配置多数据源(多个数据库),整合Druid网上有很多文章,翻了翻,但是都有问题啊,有的是没使用Druid,使用了默认的Hirika,有的是配置文件不给或者给的是Hirika能用的配置,层级不对,所以我自己来记一下代码pom文件maven依赖<dependency><groupId>com.alibaba</gr Oxye 华为云开发者联盟https://huaweicloud.csdn.net/6387524cdacf622b8df8ab9f.html?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ODI0NzQsImV4cCI6MTcxMDgwOTA1MywiaWF0IjoxNzEwMjA0MjUzLCJ1c2VybmFtZSI6IkVsZW5kYUxlZSJ9.EAq9dDI8MxXQwa-msFI9Urbg_N59E13lEQS3o95rCVw

相关推荐
罗政21 分钟前
PDF书籍《手写调用链监控APM系统-Java版》第9章 插件与链路的结合:Mysql插件实现
java·mysql·pdf
chengma_09090938 分钟前
MySQL 数据库连接数查询、配置
数据库·mysql
奋斗的老史44 分钟前
Spring Retry + Redis Watch实现高并发乐观锁
java·redis·spring
林农1 小时前
C05S14-MySQL高级语句
linux·mysql·云计算
北欧人写代码2 小时前
MySQL 数据库备份与恢复
mysql
00Allen002 小时前
mybatis/mybatisplus
java·spring·mybatis
一只搬砖的猹3 小时前
cJson系列——常用cJson库函数
linux·前端·javascript·python·物联网·mysql·json
冰镇毛衣3 小时前
4.3 数据库HAVING语句
数据库·sql·mysql
编码浪子3 小时前
Springboot3国际化
java·spring·mybatis
HUNAG-DA-PAO3 小时前
Spring AOP是什么
java·jvm·spring