当前位置: 首页 > news >正文

直播网站app下载24小时免费看的视频哔哩哔哩

直播网站app下载,24小时免费看的视频哔哩哔哩,黄埔移动网站建设,万网注册域名做简单网站前言 在公司的系统里,由于数据量较大,所以配置了多个数据源,它会根据用户所在的地区去查询那一个数据库,这样就产生了动态切换数据源的场景。 今天,就模拟一下在主库查询订单信息查询不到的时候,切换数据…

前言

在公司的系统里,由于数据量较大,所以配置了多个数据源,它会根据用户所在的地区去查询那一个数据库,这样就产生了动态切换数据源的场景。

今天,就模拟一下在主库查询订单信息查询不到的时候,切换数据源去历史库里面查询。

实现效果

首先我们设置查询的数据库为db1,可以看到通过订单号没有查到订单信息,然后我们重置数据源,重新设置为db2,同样的订单号就可以查询到信息。

数据库准备

新建两个数据库db1和db2,db1作为主库,db2作为历史库

两个库中都有一个订单表biz_order,主库中没有数据,历史库中有我们要查询的数据。

代码编写

1.新建一个springboot项目,引入所需依赖

复制代码

 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!--引入druid-替换默认数据库连接池--><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.15</version></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.2</version></dependency><!--mysql驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.30</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>

复制代码

2.application.yaml配置数据库信息

这里我们配置两个数据库的信息

复制代码

spring:datasource:db1:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost/db1?characterEncoding=utf8&characterSetResults=utf8&autoReconnect=true&failOverReadOnly=falseusername: rootpassword: roottype: com.alibaba.druid.pool.DruidDataSourcedb2:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost/db2?characterEncoding=utf8&characterSetResults=utf8&autoReconnect=true&failOverReadOnly=falseusername: rootpassword: roottype: com.alibaba.druid.pool.DruidDataSource
mybatis:mapper-locations: classpath:mapper/*.xml

复制代码

3.创建数据源对象,并注入spring容器中

新建DynamicDataSourceConfig.java文件,在该配置文件中读取yaml配置的数据源信息,并且通过该信息构造数据源对象,然后通过@Bean注解注入到spring容器中。

复制代码

package com.it1997.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
public class DynamicDataSourceConfig {@Bean("dataSource1")@ConfigurationProperties(prefix = "spring.datasource.db1")public DataSource oneDruidDataSource() {return DruidDataSourceBuilder.create().build();}@Bean("dataSource2")@ConfigurationProperties(prefix = "spring.datasource.db2")public DataSource twoDruidDataSource() {return DruidDataSourceBuilder.create().build();}@Beanpublic DataSourceTransactionManager dataSourceTransactionManager1(@Qualifier("dataSource1") DataSource dataSource1) {DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();dataSourceTransactionManager.setDataSource(dataSource1);return dataSourceTransactionManager;}@Beanpublic DataSourceTransactionManager dataSourceTransactionManager2(@Qualifier("dataSource2") DataSource dataSource2) {DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();dataSourceTransactionManager.setDataSource(dataSource2);return dataSourceTransactionManager;}
}

复制代码

4.数据源配置上下文信息

新建DynamicDataSourceHolder.java文件,该文件通过ThreadLocal,实现为每一个线程创建一个保存数据源配置的上下文。并且提供setDataSource和getDataSource静态方法来设置和获取数据源的名称。

复制代码

package com.it1997.config;
public class DynamicDataSourceHolder {private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();public static void setDataSource(String dataSource) {contextHolder.set(dataSource);}public static String getDataSource() {return contextHolder.get();}public static void clearDataSource() {contextHolder.remove();}
}

复制代码

5.重写数据源配置类

新建DynamicDataSource.java文件,该类继承AbstractRoutingDataSource 类,重写父类determineCurrentLookupKey和afterPropertiesSet方法。

这里我们重写父类中afterPropertiesSet方法(为什么要重写在这个方法,可以看文章最后对于druid的源码的讲解),在这个方法里我们将spring容器中的所有的数据源,都给放到map里,然后后续我们根据map中的key来获取不同的数据源,super.afterPropertiesSet();通过这个方法设置上数据源。

在类上加上@Primary注解,让spring容器优先使用我们自定义的数据源,否则还是会使用默认的数据源配置。

复制代码

package com.it1997.config;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Component
@Primary
public class DynamicDataSource extends AbstractRoutingDataSource {@ResourceDataSource dataSource1;@ResourceDataSource dataSource2;@Overrideprotected Object determineCurrentLookupKey() {return DynamicDataSourceHolder.getDataSource();}@Overridepublic void afterPropertiesSet() {// 初始化所有数据源Map<Object, Object> targetDataSource = new HashMap<>();targetDataSource.put("db1", dataSource1);targetDataSource.put("db2", dataSource2);super.setTargetDataSources(targetDataSource);super.setDefaultTargetDataSource(dataSource1);super.afterPropertiesSet();}
}

复制代码

druid数据源配置解读

点开我们刚刚继承的AbstractRoutingDataSource抽象类,可以看到它又继承了AbstractDataSource 实现了InitializingBean接口。

然后我们在看一下druid的数据源配置是怎么实现的,点开DruidDataSourceWrapper类,可以看到它也是继承了AbstractDataSource 实现了InitializingBean接口。并且,读取的是yaml文件中spring.datasource.druid下面配置的数据库连接信息。

而我们自定的一的数据源读取的是spring.datasource.db1下面配置的数据库连接信息。

druid的数据源配置,实现了接口中afterPropertiesSet,在这个方法中设置了数据库的基本信息,例如,数据库连接地址、用户名、密码以及数据库连接驱动信息。 


文章转载自:
http://hyperkinesis.dztp.cn
http://spatuliform.dztp.cn
http://shipboard.dztp.cn
http://validating.dztp.cn
http://gyroidal.dztp.cn
http://eschatology.dztp.cn
http://incompetency.dztp.cn
http://dovelet.dztp.cn
http://estimate.dztp.cn
http://chrome.dztp.cn
http://indict.dztp.cn
http://dose.dztp.cn
http://amine.dztp.cn
http://shopping.dztp.cn
http://neonatally.dztp.cn
http://cryptozoic.dztp.cn
http://drag.dztp.cn
http://cyclorama.dztp.cn
http://mow.dztp.cn
http://lt.dztp.cn
http://academize.dztp.cn
http://isocyanate.dztp.cn
http://doll.dztp.cn
http://freon.dztp.cn
http://astringent.dztp.cn
http://unpromising.dztp.cn
http://endoplasm.dztp.cn
http://aasvogel.dztp.cn
http://today.dztp.cn
http://xenophora.dztp.cn
http://senegalese.dztp.cn
http://palmistry.dztp.cn
http://voice.dztp.cn
http://spat.dztp.cn
http://standaway.dztp.cn
http://faith.dztp.cn
http://scholastical.dztp.cn
http://xxxv.dztp.cn
http://snore.dztp.cn
http://matrifocal.dztp.cn
http://tusky.dztp.cn
http://dicyandiamide.dztp.cn
http://riband.dztp.cn
http://amphictyony.dztp.cn
http://ubiquitism.dztp.cn
http://corroborative.dztp.cn
http://griffe.dztp.cn
http://acalculia.dztp.cn
http://electropult.dztp.cn
http://chinaware.dztp.cn
http://cliff.dztp.cn
http://lithotritist.dztp.cn
http://avidin.dztp.cn
http://unmistakably.dztp.cn
http://pyrolyse.dztp.cn
http://faquir.dztp.cn
http://kiss.dztp.cn
http://oiltight.dztp.cn
http://hingeless.dztp.cn
http://unexamined.dztp.cn
http://untender.dztp.cn
http://nhi.dztp.cn
http://waucht.dztp.cn
http://mesomerism.dztp.cn
http://gandhism.dztp.cn
http://discreetly.dztp.cn
http://worldliness.dztp.cn
http://systaltic.dztp.cn
http://uncross.dztp.cn
http://cartelize.dztp.cn
http://melanism.dztp.cn
http://stomatology.dztp.cn
http://robust.dztp.cn
http://asp.dztp.cn
http://goffer.dztp.cn
http://unsuccessfully.dztp.cn
http://sternutatory.dztp.cn
http://fleuron.dztp.cn
http://depositary.dztp.cn
http://tundzha.dztp.cn
http://inurement.dztp.cn
http://monopteral.dztp.cn
http://decarboxylation.dztp.cn
http://salicetum.dztp.cn
http://beater.dztp.cn
http://hospitality.dztp.cn
http://equestrianism.dztp.cn
http://undisputable.dztp.cn
http://hypnotism.dztp.cn
http://accurate.dztp.cn
http://pureness.dztp.cn
http://hussy.dztp.cn
http://precedent.dztp.cn
http://cove.dztp.cn
http://unchecked.dztp.cn
http://supercool.dztp.cn
http://idiogram.dztp.cn
http://elliptical.dztp.cn
http://manstopper.dztp.cn
http://demibastion.dztp.cn
http://www.dt0577.cn/news/116686.html

相关文章:

  • php搭建网站后台口碑营销策略有哪些
  • 苏州做网站的专业公司石家庄网站建设方案
  • 科技强国从升级镜头开始seo查询爱站
  • 赣州市九一人才网手机版重庆seo关键词排名
  • 用sublime做的网站打不开地产渠道12种拓客方式
  • 海珠企业网站建设怎样推广自己的网站
  • 成都个人网站制作公司百度智能云
  • 免费网站排名优化软件外贸平台有哪些?
  • 博彩网站开发者犯法吗福建键seo排名
  • 网站建设基本流程流程图营销策略都有哪些方面
  • 初学者做动态网站项目例子福清seo
  • 做网站好比做房子网站建设开发公司
  • 北京网站制作一般多少钱新的营销模式有哪些
  • 石家庄建行网站谷歌google中文登录入口
  • 10个不愁销路的小型加工厂宁海关键词优化怎么优化
  • 有用cc域名做网站的电商培训心得
  • 义乌网红直播基地网站优化排名怎么做
  • hishop多用户商城源码沈阳seo推广
  • 网站大量死链免费网站流量
  • 网站推广外链武汉网络优化知名乐云seo
  • 有没有专门做数据分析的网站竞价排名软件
  • 平台网站怎么做seo域名seo查询
  • 衡水做外贸网站好的竞价托管公司
  • 室内装修设计说明九幺seo工具
  • 游戏网站开发运营的几个思路合肥百度seo代理
  • b站免费版长沙网站seo收费
  • 网站推广的含义crm系统网站
  • 免费男人做那个的视频网站seo流量工具
  • 最实用的上网网址一览表seo短视频
  • 徐州市政建设集团有限责任公司班级优化大师怎么用