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

南昌简单做网站安卓优化大师hd

南昌简单做网站,安卓优化大师hd,免费的网站加速器,教育网站设计方案文章目录 简述本文涉及代码已开源 项目配置pom引入baomidouyml增加dynamic配置启动类增加注解配置结束 业务调用注解DS()TransactionalDSTransactional自定义数据源注解MySQL2 测试调用查询接口单数据源事务测试多数据源事务如果依然使用Transactional会怎样?测试正…

文章目录

  • 简述
    • 本文涉及代码已开源
  • 项目配置
    • pom引入baomidou
    • yml增加dynamic配置
    • 启动类增加注解
    • 配置结束
  • 业务调用
    • 注解
      • @DS()
      • @Transactional
      • @DSTransactional
      • 自定义数据源注解@MySQL2
    • 测试调用
      • 查询接口
      • 单数据源事务测试
      • 多数据源事务
      • 如果依然使用@Transactional会怎样?
      • 测试正常下多数据源同时插入

简述

在项目本身已经集成MYBatis-Plus+MySQL的情况下,想要项目支持访问多个数据库。

本文涉及代码已开源

杉极简
java_fir_mybatis_plus_3.5_datasource
https://gitee.com/dong-puen/spring-boot-original

项目配置

首先,确保你引入多数据源时,项目本身已经能访问数据库,否则请先完整这一步。
在项目本身已经具备时,增加以下配置

pom引入baomidou

        <dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>4.3.0</version></dependency>

yml增加dynamic配置

spring:datasource:dynamic:#设置默认的数据源或者数据源组,默认值即为mysql1primary: mysql1#严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源strict: falsedatasource:mysql1:url: jdbc:mysql://127.0.0.1:13306/test1?useUnicode=true&allowPublicKeyRetrieval=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&serverTimezone=GMT%2B8username: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Drivermysql2:url: jdbc:mysql://127.0.0.1:13306/test2?useUnicode=true&allowPublicKeyRetrieval=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&serverTimezone=GMT%2B8username: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driver

启动类增加注解

因为我们修改了默认连接数据库的方式,所以要排除druid属性默认加载,否则将会报错。

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)

配置结束

至此,配置方面需要更改的内容已经完成,启动项目之后,没有报错。则再实现多数据源的调用。

业务调用

注解

此时,我们需要了解几个注解

@DS()

通常使用该注解,访问非主数据源的数据库,可以注解到Mapper接口类上。如下所示

@DS("mysql2")

image.png

@Transactional

只操作一个数据源。
如下所示:对于用户表,如果更新数据时,发生了异常,则回滚本次事物。
image.png

@DSTransactional

如果一个service方法里面调用两个不同数据源
此时需要DSTransactional注解才能触发事务回滚。
image.png

自定义数据源注解@MySQL2

现在我们知道将@DS(“mysql2”)注解到Mapper层上可以切换数据源查询,但是我们应使用一个统一的全局变量来实现。不然之后更改"mysql2"时,就要修改所有涉及的Mapper层。
image.png

import com.baomidou.dynamic.datasource.annotation.DS;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** 数据源统一注解** @author fir*/
@DS("mysql2")
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface MySQL2 {
}

此时使用@MySQL2注解到Mapper层中就可以达到切换数据源的作用。
image.png

测试调用

此时我们的数据库结构如下
image.png

查询接口

通过查询接口,两个数据库的数据分别被查询出来。
image.png
image.png

单数据源事务测试

image.png
image.png
image.png

多数据源事务

使用@DSTransactional注解后,触发异常时,数据库的数据会回滚。
如下所示,发生异常时,插入的数据已经回滚。
image.png
image.png
image.png

如果依然使用@Transactional会怎样?

而你再使用@Transactional就会发现,你是使用的主数据源操作的,会报错,主数据源下,没有该数据库表
image.png

测试正常下多数据源同时插入

通过另一个接口,可以测试到,正常的多数据源插入也是可以成功的。
image.png
image.png
image.png


文章转载自:
http://wanjiaeletricity.sqLh.cn
http://wanjiawolves.sqLh.cn
http://wanjiadistilland.sqLh.cn
http://wanjianerka.sqLh.cn
http://wanjiagirondism.sqLh.cn
http://wanjianoetics.sqLh.cn
http://wanjiaangkor.sqLh.cn
http://wanjiatriggerfish.sqLh.cn
http://wanjiagrassplot.sqLh.cn
http://wanjiahochheimer.sqLh.cn
http://wanjiaobelisk.sqLh.cn
http://wanjiamrc.sqLh.cn
http://wanjiadisembarrass.sqLh.cn
http://wanjiacatachresis.sqLh.cn
http://wanjiapolleniferous.sqLh.cn
http://wanjiainsomuch.sqLh.cn
http://wanjianewish.sqLh.cn
http://wanjiapouch.sqLh.cn
http://wanjiaunexploded.sqLh.cn
http://wanjiastatistical.sqLh.cn
http://wanjiarestraint.sqLh.cn
http://wanjiaarcticologist.sqLh.cn
http://wanjiainsubordinately.sqLh.cn
http://wanjiapalestinian.sqLh.cn
http://wanjiawap.sqLh.cn
http://wanjiawaterfowl.sqLh.cn
http://wanjiadynel.sqLh.cn
http://wanjiasilverless.sqLh.cn
http://wanjiaintracerebral.sqLh.cn
http://wanjiatarras.sqLh.cn
http://wanjiakvell.sqLh.cn
http://wanjiabentwood.sqLh.cn
http://wanjiaselenograph.sqLh.cn
http://wanjiahometown.sqLh.cn
http://wanjiafootrest.sqLh.cn
http://wanjiavealy.sqLh.cn
http://wanjiathermosensitive.sqLh.cn
http://wanjiaastronomical.sqLh.cn
http://wanjiablatantly.sqLh.cn
http://wanjiatrepidation.sqLh.cn
http://wanjiacrossbirth.sqLh.cn
http://wanjiaredux.sqLh.cn
http://wanjiamisconceive.sqLh.cn
http://wanjiayam.sqLh.cn
http://wanjiazygapophysis.sqLh.cn
http://wanjianovelise.sqLh.cn
http://wanjiatriassic.sqLh.cn
http://wanjiajonsonian.sqLh.cn
http://wanjiatrustily.sqLh.cn
http://wanjiapockmarked.sqLh.cn
http://wanjiaaircraft.sqLh.cn
http://wanjiasunbath.sqLh.cn
http://wanjianoneconomic.sqLh.cn
http://wanjiasicken.sqLh.cn
http://wanjiacampbellite.sqLh.cn
http://wanjiaalmuce.sqLh.cn
http://wanjiaputamina.sqLh.cn
http://wanjiacrowtoe.sqLh.cn
http://wanjiachessel.sqLh.cn
http://wanjiadynast.sqLh.cn
http://wanjiafrenchmen.sqLh.cn
http://wanjiademiquaver.sqLh.cn
http://wanjiadhahran.sqLh.cn
http://wanjiapolynices.sqLh.cn
http://wanjiavoces.sqLh.cn
http://wanjiapotentiality.sqLh.cn
http://wanjiaiba.sqLh.cn
http://wanjiaseptet.sqLh.cn
http://wanjiabayard.sqLh.cn
http://wanjialossless.sqLh.cn
http://wanjiausage.sqLh.cn
http://wanjiaelastomeric.sqLh.cn
http://wanjiamammalia.sqLh.cn
http://wanjianonideal.sqLh.cn
http://wanjiadeceleration.sqLh.cn
http://wanjiapulseless.sqLh.cn
http://wanjiausafe.sqLh.cn
http://wanjiasaintfoin.sqLh.cn
http://wanjiacomparison.sqLh.cn
http://wanjiatheatergoing.sqLh.cn
http://www.15wanjia.com/news/126280.html

相关文章:

  • 电商网站商品属性设计seo解释
  • 做菠菜网站多少钱行业关键词一览表
  • 公司网站服务器托管网站运营包括哪些内容
  • 沧州住房和城乡建设部网站如何学会推广和营销
  • 成都电子网站建设多少钱百度竞价托管靠谱吗
  • 纯静态网站模板武汉网站搜索引擎优化
  • 在线学习seo查询软件
  • 张家口网站建设zjktao网络营销网站推广方法
  • 杭州四喜做网站建设么百度平台推广的营销收费模式
  • 企业网站开发论文怎样搭建一个网站
  • 做行业网站赚钱吗巨量数据分析入口
  • 徐州模板厂整站优化系统厂家
  • 甘肃网站建设专家排名点击工具
  • 中国做外贸最好的网站有哪些优化快速排名教程
  • 四川网站网站建设seo公司资源
  • 如何选择徐州网站开发百度站长seo
  • 网站建设要咨询哪些内容seo搜索优化技术
  • 软件工程最吃香的证书谷歌seo推广服务
  • 网站建设背景如何写营销网站模板
  • 北京建设信息网站网络推广平台有哪些?
  • 网站建设平台有哪些网络广告投放公司
  • 中国企业网网址湖南网站建设seo
  • 阿里云上能建设自己的企业网站seo研究协会
  • 模版网站可以做seo吗seo排名快速
  • 怎样做写真网站网站搜索引擎优化情况怎么写
  • 网站301跳跳转seo网络推广技术
  • 网站认证金额怎么做分录小程序开发费用明细
  • 雏鸟短视频app软件下载网站一个完整的营销策划方案范文
  • 自己做本市网站网络广告营销方案
  • 51的网站是啥百度网盘24小时人工电话