SpringBoot+MyBatis+MySQL读写分离实现( 二 )
多数据源配置
package com.cjs.example.config;import com.cjs.example.bean.MyRoutingDataSource;import com.cjs.example.enums.DBTypeEnum;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 javax.sql.DataSource;import java.util.HashMap;import java.util.Map;/** * 关于数据源配置 , 参考SpringBoot官方文档第79章《Data Access》 * 79. Data Access * 79.1 Configure a Custom DataSource * 79.2 Configure Two DataSources */@Configurationpublic class DataSourceConfig {@Bean@ConfigurationProperties("spring.datasource.master")public DataSource masterDataSource() {return DataSourceBuilder.create().build();}@Bean@ConfigurationProperties("spring.datasource.slave1")public DataSource slave1DataSource() {return DataSourceBuilder.create().build();}@Bean@ConfigurationProperties("spring.datasource.slave2")public DataSource slave2DataSource() {return DataSourceBuilder.create().build();}@Beanpublic DataSource myRoutingDataSource(@Qualifier("masterDataSource") DataSource masterDataSource,@Qualifier("slave1DataSource") DataSource slave1DataSource,@Qualifier("slave2DataSource") DataSource slave2DataSource) {Map
这里 , 我们配置了4个数据源 , 1个master , 2两个slave , 1个路由数据源 。 前3个数据源都是为了生成第4个数据源 , 而且后续我们只用这最后一个路由数据源 。
MyBatis配置
package com.cjs.example.config;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.transaction.PlatformTransactionManager;import org.springframework.transaction.annotation.EnableTransactionManagement;import javax.annotation.Resource;import javax.sql.DataSource;@EnableTransactionManagement@Configurationpublic class MyBatisConfig {@Resource(name = "myRoutingDataSource")private DataSource myRoutingDataSource;@Beanpublic SqlSessionFactory sqlSessionFactory() throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(myRoutingDataSource);sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));return sqlSessionFactoryBean.getObject();}@Beanpublic PlatformTransactionManager platformTransactionManager() {return new DataSourceTransactionManager(myRoutingDataSource);}}
推荐阅读
- MagSafe皮革钱包上手:能塞3张卡片 放入口袋时会分离
- 水药分离设计+独立内胆,左点小仙智能足蒸器Z8表现无可挑剔
- 超高效 Vue 前后端分离cms管理系统LinCMS-Vue
- python文件读写模式,覆盖写和清空写你清楚了吗
- 群联第二代PCIe 4.0 SSD主控E18性能曝光:读写均超7GB/s
- 分离摇杆+16000DPI,旗舰电竞鼠标ROG魔刃标准版体验
- 国产良心SSD读写不虚标,379元入手512G,8秒开机
- vivo又开始秀了!这手机摄像头能分离
- Sharding-Jdbc之水平分库和读写分离(二)
- 2020年NAS首选,超高速读写备份,联想个人云存储A1体验