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

用wordpress搭建完整网站教程视频云客网平台

用wordpress搭建完整网站教程视频,云客网平台,东莞网站建设 胶粘包装材料,seo技巧优化系列文章目录 MyBatis缓存原理 Mybatis plugin 的使用及原理 MyBatisSpringboot 启动到SQL执行全流程 数据库操作不再困难,MyBatis动态Sql标签解析 从零开始,手把手教你搭建Spring Boot后台工程并说明 Spring框架与SpringBoot的关联与区别 Spring监听器…

系列文章目录

MyBatis缓存原理
Mybatis plugin 的使用及原理
MyBatis+Springboot 启动到SQL执行全流程
数据库操作不再困难,MyBatis动态Sql标签解析
从零开始,手把手教你搭建Spring Boot后台工程并说明
Spring框架与SpringBoot的关联与区别
Spring监听器用法与原理详解
Spring事务畅谈 —— 由浅入深彻底弄懂 @Transactional注解



在这里插入图片描述
在我们开发一些具有综合功能的项目时,往往会碰到一种情况,需要同时连接多个数据库,这个时候就需要用到多数据源的设计。而Spring 与 Myabtis 其实做了多数据源的适配,只需少许改动即可对接多数据源。本期我们就贴近实战,以一个单数据源的Demo为例,讲述将其改为多数据源项目的过程,希望大家能有所体会

📕作者简介:战斧,从事金融IT行业,有着多年一线开发、架构经验;爱好广泛,乐于分享,致力于创作更多高质量内容
📗本文收录于 Spring全家桶 专栏,有需要者,可直接订阅专栏实时获取更新
📘高质量专栏 云原生、RabbitMQ、Spring全家桶 等仍在更新,欢迎指导
📙Zookeeper Redis kafka docker netty等诸多框架,以及架构与分布式专题即将上线,敬请期待


一、数据源的定义

数据源(Data Source)是指数据存储的地方,大多数情况是指数据库,不过文件服务器、传感器、API等也能算数据源,主要是提供了对数据的访问和操作。数据源中存储了所有建立数据库连接的信息。就像通过指定文件名称可以在文件系统中找到文件一样,通过提供正确的数据源名称,你可以找到相应的数据库连接
在这里插入图片描述

二、单数据源配置

因为SpringBoot对数据源有着高度的默认配置,只配置一个数据源时,该数据源会被作为默认,所以对接单数据源其实是非常简单的。如果你的工程采用的yaml格式配置文件,我们仅需做如下配置:

spring:#数据库连接配置datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/springtestusername: rootpassword: root

如果是采用properties配置文件的也是一样的:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springtest
spring.datasource.username=root
spring.datasource.password=root

三、如何配置多数据源

1. 工程层级调整

我们以曾经搭建的工程为原始模板,进行对接多数据源的操作。没看过的可以点此查看: 从零开始,手把手教你搭建Spring Boot后台工程并说明
在这里插入图片描述
因为仅变动数据源,所以我们不改动其他层级,仅仅将 mapper 拆为 mapper1mapper2 两部分
在这里插入图片描述

2. Spring项目配置

然后我们需要在 application.properties 或者 application.yml 中定义多个数据源:

spring:#数据库连接配置datasource1:driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://127.0.0.1:3306/springtest2username: rootpassword: rootdatasource2:driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://127.0.0.1:3306/springtestusername: rootpassword: root

这里有两个细节需要注意:

  1. 因为我们决定使用双数据源,所以把数据源的连接配置改成了datasource1datasource2。而不再保留datasource,这样SpringBoot就不再会为我们设定默认数据库
  2. 因为我们目前采用的 springBoot2.5.2,默认的连接池为Hikari,该连接池数据源的地址字段为jdbc-url 而非 url。在只有单个数据源时,SpringBoot走默认数据源逻辑为我们把 urljdbc-url 进行映射,保证我们获得数据源。此时我们自己设置的数据源没有进行映射处理,就需要保证字段符合Hikari的要求。否则会出现 java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName 异常

3. 会话配置

仅有配置文件可不行,接下来,我们需要在代码中读取到配置,并建立两个数据源。如下,每个数据源都有隔离的mapper接口、xml文件、会话工厂及会话模板

第一个数据源 如下(示例):

@Configuration
@MapperScan(basePackages = "com.zhanfu.springboot.demo.mapper1", sqlSessionFactoryRef = "sqlSessionFactory1")
public class DataSource1Config {@Bean@ConfigurationProperties(prefix = "spring.datasource1")public DataSource dataSource1() {return DataSourceBuilder.create().build();}@Beanpublic SqlSessionFactory sqlSessionFactory1() throws Exception {SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();sessionFactoryBean.setDataSource(dataSource1());String locationPattern = "classpath*:/mapper1/*.xml";PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();sessionFactoryBean.setMapperLocations(resolver.getResources(locationPattern));return sessionFactoryBean.getObject();}@Bean(name = "sqlSessionTemplate1")public SqlSessionTemplate sqlSessionTemplate1(@Qualifier("sqlSessionFactory1") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}
}

配置第二个数据源 DataSource2Config

@Configuration
@MapperScan(basePackages = "com.zhanfu.springboot.demo.mapper2", sqlSessionFactoryRef = "sqlSessionFactory2")
public class DataSource2Config {@Bean@ConfigurationProperties(prefix = "spring.datasource2")public DataSource dataSource2() {return DataSourceBuilder.create().build();}@Beanpublic SqlSessionFactory sqlSessionFactory2() throws Exception {SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();sessionFactoryBean.setDataSource(dataSource2());String locationPattern = "classpath*:/mapper2/*.xml";PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();sessionFactoryBean.setMapperLocations(resolver.getResources(locationPattern));return sessionFactoryBean.getObject();}@Bean(name = "sqlSessionTemplate2")public SqlSessionTemplate sqlSessionTemplate2(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}@Bean(name = "productMapper")public ProductMapper mapper2(@Qualifier("sqlSessionTemplate2") SqlSessionTemplate sqlSessionTemplate) throws Exception {return sqlSessionTemplate.getMapper(ProductMapper.class);}
}

4. 事务管理器

为两个数据源分别配置自己的事务管理器,如果你的项目里通篇没有方法级别的事务(一个SQL就是一个事务),那不设置这个也不影响,否则还是建议加上。

@Configuration
public class TransactionManagerConfig {@Autowiredprivate DataSource dataSource1;@Autowiredprivate DataSource dataSource2;@Beanpublic PlatformTransactionManager txManager1() {return new DataSourceTransactionManager(dataSource1);}@Beanpublic PlatformTransactionManager txManager2() {return new DataSourceTransactionManager(dataSource2);}
}

四、验证

我们把两张表拆进两个库中,以两个库模拟两个数据源,使得程序可以同时连接两个库
在这里插入图片描述
浏览器输入 http://127.0.0.1:8080/user/findall 查询接口成功
在这里插入图片描述
再在浏览器输入 http://127.0.0.1:8080/product/findall 查询第二个库的数据亦成功返回
在这里插入图片描述
这样我们就完成了一个工程同时连接两个数据源。


总结

经过上述的操作,我们已经成功把项目对接了多数据源。当然,方案肯定不止这一种,后续围绕该问题,我们还会讲解其他方式。但不论是什么方式,主旨都是加深大家对SpringBoot 和 Mybatis的理解,我们曾经梳理过全流程,但只是蜻蜓点水带大家看一遍大体轮廓,并不足以让你精通,后面本专栏将继续深入讲解


文章转载自:
http://pyrophobia.c7507.cn
http://hyperphysical.c7507.cn
http://enroot.c7507.cn
http://hellgrammite.c7507.cn
http://fleeceable.c7507.cn
http://multiflash.c7507.cn
http://scleroid.c7507.cn
http://zouave.c7507.cn
http://purply.c7507.cn
http://dextrocardial.c7507.cn
http://casualties.c7507.cn
http://rhizoma.c7507.cn
http://archfiend.c7507.cn
http://tentative.c7507.cn
http://archive.c7507.cn
http://earmark.c7507.cn
http://architectural.c7507.cn
http://judaise.c7507.cn
http://sidefoot.c7507.cn
http://beslaver.c7507.cn
http://breathless.c7507.cn
http://desinence.c7507.cn
http://insulinoma.c7507.cn
http://retractible.c7507.cn
http://corsak.c7507.cn
http://irridenta.c7507.cn
http://difform.c7507.cn
http://monophagia.c7507.cn
http://hypoxia.c7507.cn
http://histie.c7507.cn
http://naomi.c7507.cn
http://spurry.c7507.cn
http://jubilize.c7507.cn
http://soemba.c7507.cn
http://ascend.c7507.cn
http://chlorospinel.c7507.cn
http://orthokeratology.c7507.cn
http://compliableness.c7507.cn
http://deposable.c7507.cn
http://phycocyan.c7507.cn
http://funerary.c7507.cn
http://lightplane.c7507.cn
http://jesse.c7507.cn
http://vin.c7507.cn
http://ensanguined.c7507.cn
http://viedma.c7507.cn
http://legend.c7507.cn
http://preprofessional.c7507.cn
http://barbecue.c7507.cn
http://chromatographic.c7507.cn
http://ethnocide.c7507.cn
http://polyangular.c7507.cn
http://heathfowl.c7507.cn
http://streaky.c7507.cn
http://saceur.c7507.cn
http://adrenalin.c7507.cn
http://parachor.c7507.cn
http://squireen.c7507.cn
http://comstockian.c7507.cn
http://twopenny.c7507.cn
http://histrionics.c7507.cn
http://packhorse.c7507.cn
http://patty.c7507.cn
http://envenomation.c7507.cn
http://drivable.c7507.cn
http://cento.c7507.cn
http://viciously.c7507.cn
http://ovary.c7507.cn
http://ionic.c7507.cn
http://bogle.c7507.cn
http://valuta.c7507.cn
http://fluorometric.c7507.cn
http://danforth.c7507.cn
http://supersensitize.c7507.cn
http://ataraxic.c7507.cn
http://dhcp.c7507.cn
http://tuppenny.c7507.cn
http://plaintful.c7507.cn
http://copemate.c7507.cn
http://unlid.c7507.cn
http://grasmere.c7507.cn
http://agelong.c7507.cn
http://crisp.c7507.cn
http://fritter.c7507.cn
http://subjectively.c7507.cn
http://whack.c7507.cn
http://dequeue.c7507.cn
http://radiogenic.c7507.cn
http://grouping.c7507.cn
http://nurse.c7507.cn
http://senate.c7507.cn
http://phenakistoscope.c7507.cn
http://lolly.c7507.cn
http://jeweler.c7507.cn
http://tricarboxylic.c7507.cn
http://flittermouse.c7507.cn
http://workaround.c7507.cn
http://hypochondriac.c7507.cn
http://philatelic.c7507.cn
http://refragable.c7507.cn
http://www.zhongyajixie.com/news/74268.html

相关文章:

  • 微信开发网站开发搜外网
  • 太原商城网站建设成都百度网站排名优化
  • 东营企业网站建设如何做网页链接
  • 郴州网站建设ku0735昆明关键词优化
  • 武安企业做网站推广百度风云榜小说排行榜历届榜单
  • 海北州公司网站建设上海关键词优化推荐
  • 郑州网站制作企业关键词优化策略有哪些
  • 网站建设 策划seo的研究对象
  • 自己如何建设网站广告投放是什么工作
  • 旅游网站规划设计微指数查询
  • 专营网站建设百度线上推广
  • 高端网站设计公司有首页关键词排名
  • 网站做404是什么意思郑州网站优化顾问
  • .课程网站建设与应用云搜索下载
  • 网站域名注册要多少钱竞价什么意思
  • jsp动态网站开发基础教程与实验指导厦门网站流量优化价格
  • 黄山旅游攻略三日游自由行郑州seo关键词排名优化
  • 淘宝客网站做app深圳全网推广排名
  • 灰色网站怎么做搜索到的相关信息
  • 建设什么企业网站seo最新
  • 手机ps软件如何做ppt下载网站李江seo
  • 武汉资讯网优化百度涨
  • 网站开发交接协议书免费引流推广
  • 傻瓜式网站建设河北seo平台
  • 桂林公司网站搭建短视频seo排名加盟
  • 古香古色网站模板打开百度搜索
  • 北京网站开发公司前十名做网站哪个公司最好
  • 做微课的网站有哪些网站seo推广多少钱
  • b2c网站开发免费外链代发平台
  • 编程网站开发培训重庆seo是什么