springboot druid mybatis多数据源配置方式
springboot druid mybatis多数据源配置
spring boot 在配置时做了很多简化配置的设置,但是简化的配置往往已牺牲一定的定制化,比如在数据源的配置时,spring boot 只提供4种数据库连接池的配置,其中并不支持常用的druid
阅读spring boot DataSourceBuilder 的源码可以发现 spring boot 提供的4种数据源类型并不是我们想要的
private static final String[] DATA_SOURCE_TYPE_NAMES = new String[] {
"org.apache.tomcat.jdbc.pool.DataSource",
"com.zaxxer.hikari.HikariDataSource",
"org.apache.commons.dbcp.BasicDataSource", // deprecated
"org.apache.commons.dbcp2.BasicDataSource" };但是 DataSourceBuilder 提供了type方法来自定义DataSource类型
public DataSourceBuilder type(Class<? extends DataSource> type) {
this.type = type;
return this;
}知道了方法,下面配置就简单许多了
首先是application.properties文件的配置
spring.datasource.sso.url=jdbc:mysql://localhost:3306/sso?useSSL=false spring.datasource.sso.username=root spring.datasource.sso.password=root spring.datasource.sso.driver-class-name=com.mysql.jdbc.Driver spring.datasource.sso.max-idle=5 spring.datasource.sso.max-wait=10000 spring.datasource.sso.min-idle=1 spring.datasource.sso.initial-size=1 spring.datasource.sso.validation-query=SELECT 1 spring.datasource.sso.test-on-borrow=false spring.datasource.sso.test-while-idle=true spring.datasource.sso.time-between-eviction-runs-millis=18800 spring.datasource.message.url=jdbc:mysql://localhost:3306/message?useSSL=false spring.datasource.message.username=root spring.datasource.message.password=root spring.datasource.message.driver-class-name=com.mysql.jdbc.Driver spring.datasource.message.max-idle=5 spring.datasource.message.max-wait=10000 spring.datasource.message.min-idle=1 spring.datasource.message.initial-size=1 spring.datasource.message.validation-query=SELECT 1 spring.datasource.message.test-on-borrow=false spring.datasource.message.test-while-idle=true spring.datasource.message.time-between-eviction-runs-millis=18800
然后是具体的主数据源配置类
@Configuration
@MapperScan(basePackages = {"org.vergil.demo.core.dao.mapper.sso"}, sqlSessionFactoryRef = "ssoSqlSessionFactory")
public class SsoConfig {
@Bean(name = "ssoDataSource")
@ConfigurationProperties(prefix = "spring.datasource.sso")
@Primary
public DataSource ssoDataSource() {
//指定使用DruidDataSource
return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
}
@Bean(name = "ssoSqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("ssoDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/sso/*.xml"));
return bean.getObject();
}
@Primary
@Bean(name = "ssoTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("ssoDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean(name = "ssoSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("ssoSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}_ @ConfigurationProperties(prefix = “spring.datasource.sso”) 引入配置项_
使用如下方式创建DruidDataSource
简化配置
return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
第二数据源
@Configuration
@MapperScan(basePackages = {"org.vergil.demo.core.dao.mapper.message"}, sqlSessionFactoryRef = "messageSqlSessionFactory")
public class MessageConfig {
@Bean(name = "messageDataSource")
@ConfigurationProperties(prefix = "spring.datasource.message")
public DataSource messageDataSource() {
return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
}
@Bean(name = "messageSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("messageDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/message/*.xml"));
return bean.getObject();
}
@Bean(name = "messageTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("messageDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "messageSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("messageSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}数据源配置完毕
每个数据源都会生成自己的sqlSession,相互独立
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
MyBatis设计SQL返回布尔值(Boolean)的常见方法
这篇文章主要为大家详细介绍了MyBatis设计SQL返回布尔值(Boolean)的几种常见方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下2025-06-06
Java 多线程并发 ReentrantReadWriteLock详情
这篇文章主要介绍了Java多线程并发ReentrantReadWriteLock详情,ReentrantReadWriteLock可重入读写锁。实际使用场景中,我们需要处理的操作本质上是读与写,更多相关资料,感兴趣的小伙伴可以参考一下下面文章内容2022-06-06
MyBatis在注解上使用动态SQL方式(@select使用if)
这篇文章主要介绍了MyBatis在注解上使用动态SQL方式(@select使用if),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-07-07
IDEA启动后报错内存溢出java.lang.OutOfMemoryError的解决方法
IDEA在启动项目后报错内存溢出,有时直接修改JVM内存并不能全部解决问题,所以本文给大家介绍了IDEA启动后报错内存溢出java.lang.OutOfMemoryError的解决方法,需要的朋友可以参考下2025-10-10


最新评论