时间:2021-05-20
1.application.yml中添加两个datasource
server: port: 8080spring: application: name: king datasource: master: type: com.zaxxer.hikari.HikariDataSource jdbc-url: jdbc:mysql://localhost:3306/king?useUnicode=yes&characterEncoding=UTF-8&serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 idle-timeout: 10000 minimum-idle: 1 maximum-pool-size: 10 pool-name: master auto-commit: false connection-test-query: SELECT 1 slave: type: com.zaxxer.hikari.HikariDataSource jdbc-url: jdbc:mysql://localhost:3306/king?useUnicode=yes&characterEncoding=UTF-8&serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 idle-timeout: 10000 minimum-idle: 1 maximum-pool-size: 10 pool-name: slave auto-commit: false connection-test-query: SELECT 12.手动初始化数据源masterDataSource
3.手动初始化数据源ReadDataSource
package cn.bj.king.config;import com.github.pagehelper.PageInterceptor;import com.zaxxer.hikari.HikariConfig;import com.zaxxer.hikari.HikariDataSource;import org.apache.ibatis.plugin.Interceptor;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.jdbc.DataSourceBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.env.Environment;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.transaction.annotation.EnableTransactionManagement;import javax.sql.DataSource;import java.util.Properties;@Configuration@EnableTransactionManagement@MapperScan(basePackages = {"cn.bj.king.mapper.slave"}, sqlSessionFactoryRef = "slaveSqlSessionFactory")public class ReadDataSourceConfig { @Autowired Environment environment; @Bean(name = "slaveDataSource") @Qualifier("slaveDataSource") public DataSource slaveDataSource(@Qualifier("slaveHikariConfig")HikariConfig hikariConfig) { System.out.println("实例化从库"); HikariDataSource hikariDataSource=new HikariDataSource(hikariConfig); return hikariDataSource; } /** * 配置连接池信息 * @return */ @ConfigurationProperties(prefix = "spring.datasource.slave") @Bean("slaveHikariConfig") public HikariConfig slaveHikariConfig(){ HikariConfig hikariConfig=new HikariConfig(); return hikariConfig; } /** * SqlSessionFactory配置 * * @return * @throws Exception */ @Bean(name = "slaveSqlSessionFactory") public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); // 配置mapper文件位置 sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/slave/*.xml")); sqlSessionFactoryBean.setTypeAliasesPackage("cn.bj.king.entity"); return sqlSessionFactoryBean.getObject(); } /** * 配置事物管理器 * * @return */ @Bean(name = "slaveTransactionManager") public DataSourceTransactionManager slaveTransactionManager(@Qualifier("slaveDataSource") DataSource dataSource) { DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource); return dataSourceTransactionManager; }}4.测试启动是否正常初始化master和slave 两个数据源
5.这里需要注意几个地方:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
这篇文章主要介绍了Spring-boot集成pg、mongo多数据源过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
SpringBoot如何快速配置数据源;有如下两种方式:通过spring-boot-starter-jdbc快速配置数据源自定义数据源DataSource首先我
你在使用MyBatis的过程中,是否有想过多个数据源应该如何配置,如何去实现?出于这个好奇心,我在DruidWiki的数据库多数据源中知晓Spring提供了对多
本文实例讲述了spring多数据源配置实现方法。分享给大家供大家参考,具体如下:在网上找到的配置多数据源的方法。1.扩展org.springframework.
由于项目需要,最近研究了一下基于springBoot与SpringDataJPA的多数据源配置问题。以下是传统的单数据源配置代码。这里使用的是Spring的An