您好,登錄后才能下訂單哦!
這篇文章主要講解了如何實現SpringBoot Mybatis動態數據源切換方案,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
背景
最近讓我做一個大數據的系統,分析了一下,麻煩的地方就是多數據源切換抽取數據。考慮到可以跨服務器跨數據庫抽數,再整理數據,就配置了這個動態數據源的解決方案。在此分享給大家。
實現方案
數據庫配置文件
我們項目使用的是yml形式的配置文件,采用的是hikari的數據庫連接池。第一步我們自然是配置多個數據庫源頭。
我們找到spring的datasource,在下方配置三個數據源。
spring: application: name: dynamicDatasource datasource: test1: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test1?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false username: root password: 123456 test2: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test2?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false username: root password: 123456 test3: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test3?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false username: root password: 123456 hikari: leak-detection-threshold: 2000
定義數據源實體類
我們可以建立個datasourceBean文件夾專門管理數據源的實體類。
我們這里要建立三個實體類。分別對應test1,test2,test3
@Configuration public class Test1DataSourceBean { @Value("${spring.datasource.test1.driver-class-name}") private String test1Driver; @Value("${spring.datasource.test1.url}") private String test1Url; @Value("${spring.datasource.test1.username}") private String test1Username; @Value("${spring.datasource.test1.password}") private String test1Password; @Bean(name="test1DataSource") public DataSource test1DataSource() throws Exception{ HikariDataSource dataSource = new HikariDataSource(); dataSource.setDriverClassName(test1Driver); dataSource.setJdbcUrl(test1Url); dataSource.setUsername(test1Username); dataSource.setPassword(test1Password); return dataSource; } }
@Configuration public class Test2DataSourceBean { @Value("${spring.datasource.test2.driver-class-name}") private String test2Driver; @Value("${spring.datasource.test2.url}") private String test2Url; @Value("${spring.datasource.test2.username}") private String test2Username; @Value("${spring.datasource.test2.password}") private String test2Password; @Bean(name="test2DataSource") public DataSource test2DataSource() throws Exception{ HikariDataSource dataSource = new HikariDataSource(); dataSource.setDriverClassName(test2Driver); dataSource.setJdbcUrl(test2Url); dataSource.setUsername(test2Username); dataSource.setPassword(test2Password); return dataSource; } }
@Configuration public class Test3DataSourceBean { @Value("${spring.datasource.test3.driver-class-name}") private String test3Driver; @Value("${spring.datasource.test3.url}") private String test3Url; @Value("${spring.datasource.test3.username}") private String test3Username; @Value("${spring.datasource.test3.password}") private String test3Password; @Bean(name="test3DataSource") public DataSource test3DataSource() throws Exception{ HikariDataSource dataSource = new HikariDataSource(); dataSource.setDriverClassName(test3Driver); dataSource.setJdbcUrl(test3Url); dataSource.setUsername(test3Username); dataSource.setPassword(test3Password); return dataSource; } }
定義一個枚舉類管理數據源
public enum DatabaseType { test1("test1", "test1"), test2("test2", "test2"), test3("test3","test3"); private String name; private String value; DatabaseType(String name, String value){ this.name = name; this.value = value; } public String getName(){ return name; } public String getValue(){ return value; } }
定義一個線程安全的數據源容器
public class DatabaseContextHolder { private static final ThreadLocal<DatabaseType> contextHolder = new ThreadLocal<>(); public static void setDatabaseType(DatabaseType type){ contextHolder.set(type); } public static DatabaseType getDatabaseType(){ return contextHolder.get(); } }
定義動態數據源
public class DynamicDataSource extends AbstractRoutingDataSource{ protected Object determineCurrentLookupKey() { return DatabaseContextHolder.getDatabaseType(); } }
mybatis配置類
網上的很多文章配置出來都會產生數據源循環依賴的問題,這里解決了這個問題。
@Configuration @MapperScan(basePackages="cn.test.jichi", sqlSessionFactoryRef="sessionFactory") public class MybatisConfig { /** * @Description:設置動態數據源 */ @Bean(name="dynamicDataSource") @Primary public DynamicDataSource DataSource( @Qualifier("test1DataSource") DataSource test1DataSource, @Qualifier("test2DataSource") DataSource test2DataSource, @Qualifier("test3DataSource") DataSource test3DataSource){ Map<Object, Object> targetDataSource = new HashMap<>(); targetDataSource.put(DatabaseType.test1, test1DataSource); targetDataSource.put(DatabaseType.test2, test2DataSource); targetDataSource.put(DatabaseType.test3, test3DataSource); DynamicDataSource dataSource = new DynamicDataSource(); dataSource.setTargetDataSources(targetDataSource); dataSource.setDefaultTargetDataSource(test1DataSource); return dataSource; } /** * @Description:根據動態數據源創建sessionFactory */ @Bean(name="sessionFactory") public SqlSessionFactory sessionFactory( @Qualifier("test1DataSource") DataSource test1DataSource, @Qualifier("test2DataSource") DataSource test2DataSource, @Qualifier("test3DataSource") DataSource test3DataSource) throws Exception{ SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); //構造方法,解決動態數據源循環依賴問題。 sessionFactoryBean.setDataSource(this.DataSource(test1DataSource,test2DataSource, test3DataSource)); return sessionFactoryBean.getObject(); } }
示例
public void testDymnaicDatasource(){ //不切換數據源默認是自己的。 System.out.println("-----默認數據源"); DemoEntity totalCount = demoMapper.getTotalCount(); String nameCount1 = totalCount.getNameCount(); String ageCount2 = totalCount.getAgeCount(); System.out.println("nameCount:"+nameCount1); System.out.println("ageCount:"+ageCount2); //數據源切換為branch System.out.println("-----數據源為test2"); DynamicDataSourceUtils.chooseBranchDataSource(); Integer nameCount = demoMapper.getNameCount(); Integer ageCount = demoMapper.getAgeCount(); System.out.println("nameCount:"+nameCount); System.out.println("ageCount:"+ageCount); //數據源為basic System.out.println("-----數據源為test3"); DynamicDataSourceUtils.chooseBasicDataSource(); Integer ageCount1 = demoMapper.getAgeCount(); System.out.println("ageCount:"+ageCount1); }
看完上述內容,是不是對如何實現SpringBoot Mybatis動態數據源切換方案有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。