关键词:
第一个数据源:
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "spring.datasource") public class PropertiesDatasource { private String driverClassName; private String url; private String username; private String password; private String connectionProperties; private Integer initialSize = 1; private Integer minIdle = 3; private Integer maxActive = 20; private Integer timeBetweenEvictionRunsMillis = 60000; private Integer minEvictableIdleTimeMillis = 300000; private Boolean testWhileIdle = true; private String validationQuery = null; private Boolean testOnBorrow = false; private Boolean testOnReturn = false; private Boolean poolPreparedStatements = true; private Integer maxPoolPreparedStatementPerConnectionSize = 20; private String filters = "stat"; public final String getDriverClassName() { return driverClassName; } public final void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } public final String getUrl() { return url; } public final void setUrl(String url) { this.url = url; } public final String getUsername() { return username; } public final void setUsername(String username) { this.username = username; } public final String getPassword() { return password; } public final void setPassword(String password) { this.password = password; } public final String getConnectionProperties() { return connectionProperties; } public final void setConnectionProperties(String connectionProperties) { this.connectionProperties = connectionProperties; } public final Integer getInitialSize() { return initialSize; } public final void setInitialSize(Integer initialSize) { this.initialSize = initialSize; } public final Integer getMinIdle() { return minIdle; } public final void setMinIdle(Integer minIdle) { this.minIdle = minIdle; } public final Integer getMaxActive() { return maxActive; } public final void setMaxActive(Integer maxActive) { this.maxActive = maxActive; } public final Integer getTimeBetweenEvictionRunsMillis() { return timeBetweenEvictionRunsMillis; } public final void setTimeBetweenEvictionRunsMillis(Integer timeBetweenEvictionRunsMillis) { this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; } public final Integer getMinEvictableIdleTimeMillis() { return minEvictableIdleTimeMillis; } public final void setMinEvictableIdleTimeMillis(Integer minEvictableIdleTimeMillis) { this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; } public final Boolean getTestWhileIdle() { return testWhileIdle; } public final void setTestWhileIdle(Boolean testWhileIdle) { this.testWhileIdle = testWhileIdle; } public final String getValidationQuery() { return validationQuery; } public final void setValidationQuery(String validationQuery) { this.validationQuery = validationQuery; } public final Boolean getTestOnBorrow() { return testOnBorrow; } public final void setTestOnBorrow(Boolean testOnBorrow) { this.testOnBorrow = testOnBorrow; } public final Boolean getTestOnReturn() { return testOnReturn; } public final void setTestOnReturn(Boolean testOnReturn) { this.testOnReturn = testOnReturn; } public final Boolean getPoolPreparedStatements() { return poolPreparedStatements; } public final void setPoolPreparedStatements(Boolean poolPreparedStatements) { this.poolPreparedStatements = poolPreparedStatements; } public final Integer getMaxPoolPreparedStatementPerConnectionSize() { return maxPoolPreparedStatementPerConnectionSize; } public final void setMaxPoolPreparedStatementPerConnectionSize(Integer maxPoolPreparedStatementPerConnectionSize) { this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize; } public final String getFilters() { return filters; } public final void setFilters(String filters) { this.filters = filters; } }
import java.sql.SQLException; import javax.sql.DataSource; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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 org.springframework.transaction.annotation.EnableTransactionManagement; import com.alibaba.druid.pool.DruidDataSource; import com.csget.properties.PropertiesDatasource; @Configuration @EnableTransactionManagement @MapperScan(basePackages = "com.test.dao", sqlSessionTemplateRef = "sqlSessionTemplate") public class ConfigDataSource { private Logger logger = LoggerFactory.getLogger(ConfigDataSource.class); @Autowired private PropertiesDatasource propertiesDatasource; @Bean(name = "dataSource") @Primary public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(propertiesDatasource.getUrl()); dataSource.setDriverClassName(propertiesDatasource.getDriverClassName()); dataSource.setUsername(propertiesDatasource.getUsername()); dataSource.setPassword(propertiesDatasource.getPassword()); if (StringUtils.isNotBlank(propertiesDatasource.getConnectionProperties())) { dataSource.setConnectionProperties(propertiesDatasource.getConnectionProperties()); } dataSource.setInitialSize(propertiesDatasource.getInitialSize()); dataSource.setMinIdle(propertiesDatasource.getMinIdle()); dataSource.setMaxWait(propertiesDatasource.getMaxActive()); dataSource.setMaxActive(propertiesDatasource.getMaxActive()); dataSource.setTimeBetweenEvictionRunsMillis(propertiesDatasource.getTimeBetweenEvictionRunsMillis()); dataSource.setMinEvictableIdleTimeMillis(propertiesDatasource.getMinEvictableIdleTimeMillis()); dataSource.setTestWhileIdle(propertiesDatasource.getTestWhileIdle()); if(StringUtils.isNotBlank(propertiesDatasource.getValidationQuery())) { dataSource.setValidationQuery(propertiesDatasource.getValidationQuery()); } dataSource.setTestOnBorrow(propertiesDatasource.getTestOnBorrow()); dataSource.setTestOnReturn(propertiesDatasource.getTestOnReturn()); dataSource.setPoolPreparedStatements(propertiesDatasource.getPoolPreparedStatements()); dataSource.setMaxPoolPreparedStatementPerConnectionSize( propertiesDatasource.getMaxPoolPreparedStatementPerConnectionSize()); if (StringUtils.isNotBlank(propertiesDatasource.getFilters())) { try { dataSource.setFilters(propertiesDatasource.getFilters()); } catch (SQLException e) { logger.error("初始化数据库连接池发生异常:{}", e.toString()); } } return dataSource; } @Bean(name = "sqlSessionFactory") @Primary public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setConfigLocation( new PathMatchingResourcePatternResolver().getResource("classpath:config/mybatis-config.xml")); bean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath*:com/csget/dao/xml/**/*.xml")); return bean.getObject(); } @Bean(name = "transactionManager") @Primary public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "sqlSessionTemplate") @Primary public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
第二个数据源:
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "spring.datasource.history") public class PropertiesDatasourceHistory { private String driverClassName; private String url; private String username; private String password; private String connectionProperties; private Integer initialSize = 1; private Integer minIdle = 3; private Integer maxActive = 20; private Integer timeBetweenEvictionRunsMillis = 60000; private Integer minEvictableIdleTimeMillis = 300000; private Boolean testWhileIdle = true; private String validationQuery = null; private Boolean testOnBorrow = false; private Boolean testOnReturn = false; private Boolean poolPreparedStatements = true; private Integer maxPoolPreparedStatementPerConnectionSize = 20; private String filters = "stat"; public final String getDriverClassName() { return driverClassName; } public final void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } public final String getUrl() { return url; } public final void setUrl(String url) { this.url = url; } public final String getUsername() { return username; } public final void setUsername(String username) { this.username = username; } public final String getPassword() { return password; } public final void setPassword(String password) { this.password = password; } public final String getConnectionProperties() { return connectionProperties; } public final void setConnectionProperties(String connectionProperties) { this.connectionProperties = connectionProperties; } public final Integer getInitialSize() { return initialSize; } public final void setInitialSize(Integer initialSize) { this.initialSize = initialSize; } public final Integer getMinIdle() { return minIdle; } public final void setMinIdle(Integer minIdle) { this.minIdle = minIdle; } public final Integer getMaxActive() { return maxActive; } public final void setMaxActive(Integer maxActive) { this.maxActive = maxActive; } public final Integer getTimeBetweenEvictionRunsMillis() { return timeBetweenEvictionRunsMillis; } public final void setTimeBetweenEvictionRunsMillis(Integer timeBetweenEvictionRunsMillis) { this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; } public final Integer getMinEvictableIdleTimeMillis() { return minEvictableIdleTimeMillis; } public final void setMinEvictableIdleTimeMillis(Integer minEvictableIdleTimeMillis) { this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; } public final Boolean getTestWhileIdle() { return testWhileIdle; } public final void setTestWhileIdle(Boolean testWhileIdle) { this.testWhileIdle = testWhileIdle; } public final String getValidationQuery() { return validationQuery; } public final void setValidationQuery(String validationQuery) { this.validationQuery = validationQuery; } public final Boolean getTestOnBorrow() { return testOnBorrow; } public final void setTestOnBorrow(Boolean testOnBorrow) { this.testOnBorrow = testOnBorrow; } public final Boolean getTestOnReturn() { return testOnReturn; } public final void setTestOnReturn(Boolean testOnReturn) { this.testOnReturn = testOnReturn; } public final Boolean getPoolPreparedStatements() { return poolPreparedStatements; } public final void setPoolPreparedStatements(Boolean poolPreparedStatements) { this.poolPreparedStatements = poolPreparedStatements; } public final Integer getMaxPoolPreparedStatementPerConnectionSize() { return maxPoolPreparedStatementPerConnectionSize; } public final void setMaxPoolPreparedStatementPerConnectionSize(Integer maxPoolPreparedStatementPerConnectionSize) { this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize; } public final String getFilters() { return filters; } public final void setFilters(String filters) { this.filters = filters; } }
/** * * 类描述: 历史库 * */ @Configuration @EnableTransactionManagement @MapperScan(basePackages = "com.test.history.dao", sqlSessionTemplateRef = "sqlSessionTemplateHistory") public class ConfigDataSourceHistory { private Logger logger = LoggerFactory.getLogger(ConfigDataSourceHistory.class); @Autowired private PropertiesDatasourceHistory propertiesDatasourceHistory; @Bean(name = "dataSourceHistory") public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(propertiesDatasourceHistory.getUrl()); dataSource.setDriverClassName(propertiesDatasourceHistory.getDriverClassName()); dataSource.setUsername(propertiesDatasourceHistory.getUsername()); dataSource.setPassword(propertiesDatasourceHistory.getPassword()); if (StringUtils.isNotBlank(propertiesDatasourceHistory.getConnectionProperties())) { dataSource.setConnectionProperties(propertiesDatasourceHistory.getConnectionProperties()); } dataSource.setInitialSize(propertiesDatasourceHistory.getInitialSize()); dataSource.setMinIdle(propertiesDatasourceHistory.getMinIdle()); dataSource.setMaxWait(propertiesDatasourceHistory.getMaxActive()); dataSource.setMaxActive(propertiesDatasourceHistory.getMaxActive()); dataSource.setTimeBetweenEvictionRunsMillis(propertiesDatasourceHistory.getTimeBetweenEvictionRunsMillis()); dataSource.setMinEvictableIdleTimeMillis(propertiesDatasourceHistory.getMinEvictableIdleTimeMillis()); dataSource.setTestWhileIdle(propertiesDatasourceHistory.getTestWhileIdle()); if (StringUtils.isNotBlank(propertiesDatasourceHistory.getValidationQuery())) { dataSource.setValidationQuery(propertiesDatasourceHistory.getValidationQuery()); } dataSource.setTestOnBorrow(propertiesDatasourceHistory.getTestOnBorrow()); dataSource.setTestOnReturn(propertiesDatasourceHistory.getTestOnReturn()); dataSource.setPoolPreparedStatements(propertiesDatasourceHistory.getPoolPreparedStatements()); dataSource.setMaxPoolPreparedStatementPerConnectionSize( propertiesDatasourceHistory.getMaxPoolPreparedStatementPerConnectionSize()); if (StringUtils.isNotBlank(propertiesDatasourceHistory.getFilters())) { try { dataSource.setFilters(propertiesDatasourceHistory.getFilters()); } catch (SQLException e) { logger.error("初始化数据库连接池发生异常:{}", e.toString()); } } return dataSource; } @Bean(name = "sqlSessionFactoryHistory") public SqlSessionFactory sqlSessionFactoryHistory(@Qualifier("dataSourceHistory") DataSource dataSourceHistory) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSourceHistory); bean.setConfigLocation( new PathMatchingResourcePatternResolver().getResource("classpath:config/mybatis-config.xml")); bean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath*:com/csget/history/dao/xml/**/*.xml")); return bean.getObject(); } @Bean(name = "transactionManagerHistory") public DataSourceTransactionManager transactionManagerHistory(@Qualifier("dataSourceHistory") DataSource dataSourceHistory) { return new DataSourceTransactionManager(dataSourceHistory); } @Bean(name = "sqlSessionTemplateHistory") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactoryHistory") SqlSessionFactory sqlSessionFactoryHistory) throws Exception { return new SqlSessionTemplate(sqlSessionFactoryHistory); } }
注意:
@Primary 这个注解只能存在于一个数据源中。
springboot+mybatis+pagehelper配置多数据源
前言:本文为springboot结合mybatis配置多数据源,在项目当中很多情况是使用主从数据源来读写分离,还有就是操作多库,本文介绍如何一个项目同时使用2个数据源。也希望大家带着思考去学习!博主是最近才学的配置写成博... 查看详情
springboot+mybatis配置多数据源,分包方式
看了不少网上关于多数据源的配置,大致可分为两类,分包方式和通过切面方式;样例已上传至github:https://github.com/dadachao/multids第一个子项目ds01即时使用分包方式完成多数据源配置。总结项目中出现的问题和解决办法:数据库... 查看详情
springboot+mybatis多数据源切换
由于公司业务划分了多个数据库,开发一个项目会同事调用多个库,经过学习我们采用了注解+aop的方式实现的1.首先定义一个注解类@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public@interfaceTargetDataSource{ Stringvalue();//此处接收... 查看详情
springboot+mybatis多数据源配置(代码片段)
配置文件pom包依赖配置:<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>$mybatis-spring-boot-starter.version</version></dependency><dependency> <gr... 查看详情
springboot+mybatis+druid+pagehelper实现多数据源并分页
前言本篇文章主要讲述的是SpringBoot整合Mybatis、Druid和PageHelper并实现多数据源和分页。其中SpringBoot整合Mybatis这块,在之前的的一篇文章中已经讲述了,这里就不过多说明了。重点是讲述在多数据源下的如何配置使用Druid和PageHelpe... 查看详情
springboot+mybatis-plus多数据源配置整合dynamic-datasource
参考技术A这里要排除DruidDataSourceAutoConfigure,因为DruidDataSourceAutoConfigure会注入一个DataSourceWrapper,其会在原生的spring.datasource下找url,username,password等。而我们动态数据源的配置路径是变化的。@DS优先级:方法>类@DS可以注解在方... 查看详情
springboot+mybatis+多数据源入门搭建+跨域集成以及过滤器配置的方式和过滤器加载顺序控制(代码片段)
Springboot+mybatis+多数据源入门搭建+跨域集成以及过滤器配置的方式和过滤器加载顺序控制由于项目需要,最近研究了一下springboot的项目搭建,下面和大家说说项目搭建的心得体会开发环境: JDK1.8 ... 查看详情
springboot+mybatis通过databaseidprovider支持多数据库
概述本人最近接到一个任务,将一个系统改成同时兼容Oracle和PostgreSQL(原来是仅支持Oracle)。虽然大部分的sql语句通用,但是还有许多语法存在差异,所以我们可以通过mybatis自身提供的databaseIdProvider解决这个问题,这里记录一下... 查看详情
springboot+mybatis入门培训3多数据源与缓存和数据连接池设置(代码片段)
....baidu.com/s/1Q6pbC2SR70HKCeVXgOGUPQ提取码:1234本站资源下载Springboot+ 查看详情
springboot(mybatis)多数据源:mysql+mysql
在做数据迁移的时候,通常数据库表的结构和数据的格式都会发生变化,采用sql迁移的难度复杂很多,所以可以使用代码进行多数据库操作。本文简单介绍mysql迁移到mysql。其他项目相关配置,请自行查找1、applicat... 查看详情
springboot(mybatis)多数据源:mysql+mysql(代码片段)
在做数据迁移的时候,通常数据库表的结构和数据的格式都会发生变化,采用sql迁移的难度复杂很多,所以可以使用代码进行多数据库操作。本文简单介绍mysql迁移到mysql。其他项目相关配置,请自行查找1、applicat... 查看详情
springboot(mybatis)多数据源:mysql+mysql(代码片段)
在做数据迁移的时候,通常数据库表的结构和数据的格式都会发生变化,采用sql迁移的难度复杂很多,所以可以使用代码进行多数据库操作。本文简单介绍mysql迁移到mysql。其他项目相关配置,请自行查找1、applicat... 查看详情
springboot+mybatis实现数据库读写分离
本文不包含数据库主从配置。实现思路:在项目中配置多数据源,通过代码控制访问哪一个数据源。spring-jdbc为我们提供了AbstractRoutingDataSource,DataSource的抽象实现,基于查找键,返回不通不同的数据源。编写我们自己的动态数... 查看详情
springboot+mybatis链接多数据库
多数据库:https://blog.csdn.net/weixin_44076260/article/details/111912460多数据库:https://cloud.tencent.com/developer/article/1676947https://springboot.io/t/topic/2782https://m.imooc.com/article/ 查看详情
第九章springboot+mybatis+多数据源(aop实现)
在第八章springboot+mybatis+多数据源代码的基础上,做两点修改1、ShopDaopackagecom.xxx.firstboot.dao;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Repository;importcom.xxx. 查看详情
springboot、mybatis-plus、druid多数据源环境搭建
...类与连接串)。(本文阅读大概花费两分钟)技术条件:springboot2.5.3(即springframework5.3.9)MySQL5.7mybatis-plus3.5.0dynamic-datasource3.5.0druid1.2.9idea开发工具、maven3.3.91、导入相关依赖2、建立两个数据库mydb(表user)、db2(表t_class),并分... 查看详情
第九章springboot+mybatis+多数据源(aop实现)(转载)
本编博客转发自:http://www.cnblogs.com/java-zhao/p/5415896.html 1、ShopDaopackagecom.xxx.firstboot.dao;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Rep 查看详情
第八章springboot+mybatis+多数据源
在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源。代码结构:简要原理:1)DatabaseType列出所有的数据源的key---key2)DatabaseContextHolder是一个线程安全的DatabaseType容器,并提供了向其中设置和... 查看详情