目录
一、背景
二、各个配置文件总览(文件位置因人而异)
2.1 DataSourceConfig
2.2 MybatisClickHouseConfig (ClickHouse 配置类)
2.3 MybatisMysqlConfig(Mysql 配置类)
2.4 application.properties 配置
2.5 mapper 分包情况
2.6 主类中的 @MapperScan 去掉,因为在上面 ClickHouse 配置类 和 Mysql配置类 中已经定义
2.7 引用依赖(关键性依赖)
三、参考大神,感谢分享!
一、背景
业务需求,连接池之前用的 Hikari,换成了 Druid 注意一下 ~
我选择 分包 配置动态双数据源
二、各个配置文件总览(文件位置因人而异)
2.1 DataSourceConfig
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 配置类)
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 配置类)
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 配置
# 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 引用依赖(关键性依赖)
<!-- 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