av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

springboot2+mybatis多種方式實(shí)現(xiàn)多數(shù)據(jù)配置方法

瀏覽:2日期:2023-11-26 18:06:05

業(yè)務(wù)系統(tǒng)復(fù)雜程度增加,為了解決數(shù)據(jù)庫(kù)I/O瓶頸,很自然會(huì)進(jìn)行拆庫(kù)拆表分服務(wù)來(lái)應(yīng)對(duì)。這就會(huì)出現(xiàn)一個(gè)系統(tǒng)中可能會(huì)訪問(wèn)多處數(shù)據(jù)庫(kù),需要配置多個(gè)數(shù)據(jù)源。

第一種場(chǎng)景:項(xiàng)目服務(wù)從其它多處數(shù)據(jù)庫(kù)取基礎(chǔ)數(shù)據(jù)進(jìn)行業(yè)務(wù)處理,因此各庫(kù)之間不會(huì)出現(xiàn)重表等情況。

第二種場(chǎng)景:為了減輕寫入壓力進(jìn)行讀寫分庫(kù),讀走從庫(kù),寫為主庫(kù)。此種表名等信息皆為一致。

第三種場(chǎng)景:以上兩種皆有。對(duì)于某些業(yè)務(wù)需要大數(shù)據(jù)量的匯總統(tǒng)計(jì),希望不影響正常業(yè)務(wù)必須走從庫(kù)(表信息一致),某些配置信息不存在讀寫壓力,出現(xiàn)不分庫(kù)(表信息不一致)

項(xiàng)目源代碼:

https://github.com/zzsong/springboot-multiple-datasource.git

有三個(gè)目錄:

one: 直接使用多@Bean配置,@MapperScan來(lái)路徑區(qū)分讀何庫(kù)

two: 使用注解的方式來(lái)標(biāo)識(shí)走何dataSource,AOP攔截注入動(dòng)態(tài)數(shù)據(jù)源

third: 使用spring的Bean命名策略進(jìn)行區(qū)分?jǐn)?shù)據(jù)來(lái)源

項(xiàng)目技術(shù)選型: springBoot2.2.5 + mybatis + druid + mysql

先看主要的pom包

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> <relativePath/> </parent><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.21</version> </dependency>

application.yml

spring: datasource: druid: core: url: jdbc:mysql:///kc_core?characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource schedule: url: jdbc:mysql:///kc_schedule?characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource

mysql新版本必須帶有serverTimezone,不然會(huì)報(bào)連接異常。

第一種:通過(guò)@MapperScans 掃描匹配相關(guān)的數(shù)據(jù)源

@Configuration@MapperScans({ @MapperScan(basePackages = 'com.zss.one.mapper.core', sqlSessionTemplateRef = 'coreSqlSessionTemplate',sqlSessionFactoryRef = 'coreSqlSessionFactory'), @MapperScan(basePackages = 'com.zss.one.mapper.schedule', sqlSessionTemplateRef = 'scheduleSqlSessionTemplate',sqlSessionFactoryRef = 'scheduleSqlSessionFactory')})public class MybatisOneConfig { @Bean @ConfigurationProperties(prefix = 'spring.datasource.druid.core') public DataSource coreDataSource(){ return DruidDataSourceBuilder.create().build(); } @Bean public SqlSessionFactory coreSqlSessionFactory(@Qualifier('coreDataSource') DataSource coreDataSource) throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(coreDataSource); sessionFactory.getObject().getConfiguration().setJdbcTypeForNull(null); sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true); return sessionFactory.getObject(); } @Bean public SqlSessionTemplate coreSqlSessionTemplate(@Qualifier('coreSqlSessionFactory') SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } //======schedule======== @Bean @ConfigurationProperties(prefix = 'spring.datasource.druid.schedule') public DataSource scheduleDataSource(){ return DruidDataSourceBuilder.create().build(); } @Bean public SqlSessionFactory scheduleSqlSessionFactory(@Qualifier('scheduleDataSource') DataSource coreDataSource) throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(coreDataSource); sessionFactory.getObject().getConfiguration().setJdbcTypeForNull(null); sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true); return sessionFactory.getObject(); } @Bean public SqlSessionTemplate scheduleSqlSessionTemplate(@Qualifier('scheduleSqlSessionFactory') SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); }}

第二種是動(dòng)態(tài)數(shù)據(jù)源模式,通過(guò)AOP切入注解引導(dǎo)使用何數(shù)據(jù)源。用自定義注解@interface來(lái)標(biāo)識(shí)方法走對(duì)應(yīng)的數(shù)據(jù)源。

注意事項(xiàng):類中的方法再調(diào)用帶數(shù)據(jù)源的方法,不能被AOP切入

@Target({ElementType.METHOD, ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface TargetDataSource { String value();}

extends spring的動(dòng)態(tài)DataSource路由來(lái)匹配

public class DynamicDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DataSourceContextRouting.getDataSourceName(); }}

@Configuration//@EnableConfigurationProperties(MybatisProperties.class)//不要使用此公共配置,Configuration會(huì)破壞相關(guān)dataSource的配置@MapperScan('com.zss.two.mapper')public class MybatisConfig { @Bean @ConfigurationProperties(prefix = 'spring.datasource.druid.core') public DataSource coreDataSource() { return DruidDataSourceBuilder.create().build(); } @Bean @ConfigurationProperties(prefix = 'spring.datasource.druid.schedule') public DataSource scheduleDataSource() { return DruidDataSourceBuilder.create().build(); } @Autowired @Qualifier('coreDataSource') private DataSource coreDataSource; @Autowired @Qualifier('scheduleDataSource') private DataSource scheduleDataSource; @Bean public DynamicDataSource dataSource() { Map<Object, Object> targetDataSources = new HashMap<>(); targetDataSources.put(DataSourceConstants.CORE_DATA_SOURCE, coreDataSource); targetDataSources.put(DataSourceConstants.SCHEDULE_DATA_SOURCE, scheduleDataSource); DynamicDataSource dataSource = new DynamicDataSource(); //設(shè)置數(shù)據(jù)源映射 dataSource.setTargetDataSources(targetDataSources);//// 設(shè)置默認(rèn)數(shù)據(jù)源,當(dāng)無(wú)法映射到數(shù)據(jù)源時(shí)會(huì)使用默認(rèn)數(shù)據(jù)源 dataSource.setDefaultTargetDataSource(coreDataSource); dataSource.afterPropertiesSet(); return dataSource; } /** * 根據(jù)數(shù)據(jù)源創(chuàng)建SqlSessionFactory */ @Bean public SqlSessionFactory sqlSessionFactory(DynamicDataSource dataSource) throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); sessionFactory.getObject().getConfiguration().setJdbcTypeForNull(null); sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true); return sessionFactory.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); }

第三種,自定義Bean命名策略,按beanName進(jìn)行自動(dòng)匹配使用數(shù)據(jù)源

@Componentpublic class CoreBeanNameGenerator implements BeanNameGenerator { @Override public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { return 'core'+ ClassUtils.getShortName(definition.getBeanClassName()); }}@Componentpublic class ScheduleBeanNameGenerator implements BeanNameGenerator { @Override public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { return 'schedule'+ ClassUtils.getShortName(definition.getBeanClassName()); }}

使用mybatis MapperScannerConfigurer自動(dòng)掃描,將Mapper接口生成注入到spring

@Bean public MapperScannerConfigurer coreMapperScannerConfig(CoreBeanNameGenerator coreBeanNameGenerator){ MapperScannerConfigurer configurer = new MapperScannerConfigurer(); configurer.setNameGenerator(coreBeanNameGenerator); configurer.setBasePackage('com.zss.third.mapper.core,com.zss.third.mapper.order'); configurer.setSqlSessionFactoryBeanName('coreSqlSessionFactory'); configurer.setSqlSessionTemplateBeanName('coreSqlSessionTemplate'); return configurer; } @Bean public MapperScannerConfigurer scheduleMapperScannerConfig(ScheduleBeanNameGenerator scheduleBeanNameGenerator){ MapperScannerConfigurer configurer = new MapperScannerConfigurer(); configurer.setNameGenerator(scheduleBeanNameGenerator); configurer.setBasePackage('com.zss.third.mapper.schedule,com.zss.third.mapper.order'); configurer.setSqlSessionFactoryBeanName('scheduleSqlSessionFactory'); configurer.setSqlSessionTemplateBeanName('scheduleSqlSessionTemplate'); return configurer; }

到此,三種多數(shù)據(jù)源匹配主要點(diǎn)介紹完,詳細(xì)直接下載github項(xiàng)目。 在resources/db含有相關(guān)測(cè)試表及數(shù)據(jù)腳本。

到此這篇關(guān)于springboot2+mybatis多種方式實(shí)現(xiàn)多數(shù)據(jù)配置方法的文章就介紹到這了,更多相關(guān)springboot2+mybatis 多數(shù)據(jù)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 欧美顶级黄色大片免费 | 一区二区三区四区视频 | 在线视频日本 | 亚洲欧美日韩一区二区三区四区 | 中文字幕在线观看一区二区 | 亚洲在线免费视频 | 日韩免费一级片 | 天天操天天碰 | 欧美一级片在线播放 | 欧洲一级片| 日韩在线播放视频 | 国产精品手机在线 | 亚洲成人黄色 | 国产一区免费视频 | 亚洲成人精品一区 | 亚洲欧美综合 | 欧美一区二区免费 | 久久精品视频一区 | 九久久| 99一区二区 | 日韩一级黄色片 | 成人a毛片| 97视频免费在线观看 | 激情视频网 | 久久久久久黄色 | 毛片aaa | 狠狠干夜夜 | 毛片网页 | 日韩午夜精品 | 日韩免费观看视频 | 丁香六月婷婷 | 精品福利一区 | 国产成人三级在线观看 | 国产精品91在线 | 久久机热 | a视频在线观看 | 亚洲第一区在线观看 | 日本三级大片 | 日日拍夜夜拍 | 日本一级大毛片a一 | 91极品视频 |