亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

springboot配置多數(shù)據(jù)源的一款框架(dynamic-datasource-spring-boot-starter)

 更新時(shí)間:2021年09月10日 14:11:11   作者:一念既出,萬山無阻  
dynamic-datasource-spring-boot-starter 是一個(gè)基于 springboot 的快速集成多數(shù)據(jù)源的啟動(dòng)器,今天通過本文給大家分享這款框架配置springboot多數(shù)據(jù)源的方法,一起看看吧

前言

前篇博客介紹了用基本的方式做多數(shù)據(jù)源,可以應(yīng)對一般的情況,但是遇到一些復(fù)雜的情況就需要擴(kuò)展下功能了,比如:動(dòng)態(tài)增減數(shù)據(jù)源、數(shù)據(jù)源分組,純粹多庫,讀寫分離一主多從,從其他數(shù)據(jù)庫或者配置中心讀取數(shù)據(jù)源等等。其實(shí)就算沒有這些需求,使用此款框架實(shí)現(xiàn)多數(shù)據(jù)源也比之前要便捷,快速的多

框架簡介

dynamic-datasource-spring-boot-starter 是一個(gè)基于 springboot 的快速集成多數(shù)據(jù)源的啟動(dòng)器

文檔:https://github.com/baomidou/dynamic-datasource-spring-boot-starter

文檔:https://gitee.com/baomidou/dynamic-datasource-spring-boot-starter

它跟 mybatis-plus 是一個(gè)生態(tài)圈里的,都是由苞米豆團(tuán)隊(duì)出品,很容易集成 mybatis-plus

基本使用

 框架說明

  • 本框架只做 切換數(shù)據(jù)源 這件核心的事情,并不限制你的具體操作,切換了數(shù)據(jù)源可以做任何 CRUD
  • 配置文件所有以下劃線 _ 分割的數(shù)據(jù)源 首部 即為組的名稱,相同組名稱的數(shù)據(jù)源會放在一個(gè)組下
  • 切換數(shù)據(jù)源可以是組名,也可以是具體數(shù)據(jù)源名稱。組名則切換時(shí)采用負(fù)載均衡算法切換
  • 默認(rèn)的數(shù)據(jù)源名稱為 master ,你可以通過 spring.datasource.dynamic.primary 修改
  • 方法上的注解優(yōu)先于類上注解
  • DS 支持繼承抽象類上的 DS,暫不支持繼承接口上的 DS

與 springboot 的整合

數(shù)據(jù)準(zhǔn)備

  •  springboot 版本:2.0.6.RELEASE
  • mysql 版本:5.7

分別創(chuàng)建數(shù)據(jù)庫 test1,test2,數(shù)據(jù)庫表均為 goods,數(shù)據(jù)不相同

CREATE TABLE `goods` (
  `goodsId` bigint(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
  `goodsName` varchar(500) NOT NULL DEFAULT '' COMMENT 'name',
  `subject` varchar(200) NOT NULL DEFAULT '' COMMENT '標(biāo)題',
  `price` decimal(15,2) NOT NULL DEFAULT '0.00' COMMENT '價(jià)格',
  `stock` int(11) NOT NULL DEFAULT '0' COMMENT 'stock',
  PRIMARY KEY (`goodsId`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COMMENT='商品表';

test1 數(shù)據(jù)庫數(shù)據(jù)如下

在這里插入圖片描述

test2 數(shù)據(jù)庫數(shù)據(jù)如下

在這里插入圖片描述

引入依賴

至于其他依賴,不再贅述

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

springboot 配置文件

配置文件詳情可以參考官方文檔

server.port=8080

#設(shè)置test1為主數(shù)據(jù)源
spring.datasource.dynamic.primary=master
#test1主數(shù)據(jù)源配置
spring.datasource.dynamic.datasource.master.url=jdbc:mysql://127.0.0.1:3306/test1?characterEncoding=utf8&useSSL=false&autoReconnect=true&serverTimezone=UTC
spring.datasource.dynamic.datasource.master.username=root
spring.datasource.dynamic.datasource.master.password=123456
spring.datasource.dynamic.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.dynamic.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
#druid連接池配置
spring.datasource.dynamic.datasource.master.druid.initial-size=5
spring.datasource.dynamic.datasource.master.druid.max-active=20
spring.datasource.dynamic.datasource.master.druid.min-idle=5
spring.datasource.dynamic.datasource.master.druid.max-wait=60000

#test2從數(shù)據(jù)源配置
spring.datasource.dynamic.datasource.slave.url=jdbc:mysql://127.0.0.1:3306/test2?characterEncoding=utf8&useSSL=false&autoReconnect=true&serverTimezone=UTC
spring.datasource.dynamic.datasource.slave.username=root
spring.datasource.dynamic.datasource.slave.password=123456
spring.datasource.dynamic.datasource.slave.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.dynamic.datasource.slave.type=com.alibaba.druid.pool.DruidDataSource
#druid連接池配置
spring.datasource.dynamic.datasource.slave.druid.initial-size=5
spring.datasource.dynamic.datasource.slave.druid.max-active=20
spring.datasource.dynamic.datasource.slave.druid.min-idle=5
spring.datasource.dynamic.datasource.slave.druid.max-wait=60000

#mybatis配置
mybatis.mapper-locations=classpath:org/example/mapper/*.xml
mybatis.configuration.cache-enabled=true
#開啟駝峰命名
mybatis.configuration.map-underscore-to-camel-case=true
#打印SQL
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

啟動(dòng)類

需要排除掉 DruidDataSourceAutoConfigure 類,不然啟動(dòng)會報(bào)錯(cuò)找不到配置的 url

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
@Slf4j
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
        log.info("------springboot running-----");
    }
}

實(shí)體類

@Data
@ApiModel
public class Goods implements Serializable {

    @ApiModelProperty(value = "商品id")
    private Long goodsid;

    @ApiModelProperty(value = "商品名稱")
    @NotBlank(message = "商品名稱不能為空")
    private String goodsname;

    @ApiModelProperty(value = "商品描述")
    @NotBlank(message = "商品描述不能為空")
    private String subject;

    @ApiModelProperty(value = "商品價(jià)格")
    @NotNull(message = "商品價(jià)格不能為空")
    private BigDecimal price;

    @ApiModelProperty(value = "商品庫存", example = "0")
    @NotNull(message = "商品庫存不能為空")
    private Integer stock;
}

service 層

至于 dao 層,使用的是 mybatis-generator 插件自動(dòng)生成

@Service
public class GoodServiceImpl implements GoodService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public Goods selectOneGoods(Long goodsid) {
        return goodsMapper.selectByPrimaryKey(goodsid);
    }

    @Override
    public int addGoods(Goods goods) {
        return goodsMapper.insertSelective(goods);
    }
}

controller 層

@Controller
@RequestMapping(path = "/goods")
@Api(tags = "商品管理相關(guān)接口")
@Slf4j
public class GoodsController {

    @Autowired
    private GoodService goodService;

    @GetMapping(path = "/selectOne")
    @ResponseBody
    @ApiOperation(value = "查詢商品接口")
    @ApiImplicitParam(name = "id", value = "商品id", required = true)
    public ResultMap selectOne(@RequestParam(name = "id", defaultValue = "3") Long goodsid) {
        Goods goods = goodService.selectOneGoods(goodsid);
        log.info("查詢到的商品數(shù)據(jù):" + goods.toString());
        if (StringUtils.isEmpty(goods)) {
            return new ResultMap().fail().message("查詢失敗,沒有您要的數(shù)據(jù)");
        }
        return new ResultMap().success().message("查詢成功").data(goods);
    }

    @PostMapping(path = "/addGoods")
    @ResponseBody
    @ApiOperation(value = "添加商品的接口")
    public ResultMap addGoods(@Valid Goods goods, @NotNull BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            String defaultMessage = Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage();
            return new ResultMap().fail().message(defaultMessage);
        }
        int i = goodService.addGoods(goods);
        if (i > 0) {
            return new ResultMap().success().message("添加成功");
        }
        return new ResultMap().fail().message("添加失敗");
    }
}

測試

 service 層方法上都沒有注解 @DS

使用 postman 測試第一個(gè)接口

在這里插入圖片描述

使用 postman 測試第二個(gè)接口

在這里插入圖片描述
在這里插入圖片描述

以上兩個(gè)接口測試說明:它們都默認(rèn)操作的是主數(shù)據(jù)源 test1,證明我們配置文件中配置的主數(shù)據(jù)源 test1 是配置正確的,已經(jīng)生效

service 層方法上加上注解 @DS 再測試

@Service
public class GoodServiceImpl implements GoodService {

    @Autowired
    private GoodsMapper goodsMapper;

    @DS(value = "slave")// 切換數(shù)據(jù)源,并指定要訪問的數(shù)據(jù)庫名稱
    @Override
    public Goods selectOneGoods(Long goodsid) {
        return goodsMapper.selectByPrimaryKey(goodsid);
    }

    @Override
    public int addGoods(Goods goods) {
        return goodsMapper.insertSelective(goods);
    }
}

使用 postman 測試第一個(gè)接口

在這里插入圖片描述

此時(shí) @DS 注解已生效,發(fā)生了數(shù)據(jù)源的動(dòng)態(tài)切換

使用 postman 測試第二個(gè)接口

在這里插入圖片描述

在這里插入圖片描述

由于該接口沒有 @DS 注解,所以沒有發(fā)生數(shù)據(jù)源的切換,依然操作的是 test1 默認(rèn)數(shù)據(jù)源

@DS 注解說明

注解 結(jié)果
沒有@DS 默認(rèn)數(shù)據(jù)源
@DS("dsName") dsName可以為組名也可以為具體某個(gè)庫的名稱
  • @DS 可以注解在方法上或類上,同時(shí)存在就近原則 方法上注解 優(yōu)先于 類上注解
  • @DS 官方建議使用在 service 層的方法上

該框架更詳細(xì)的分析:http://chabaoo.cn/article/222550.htm
項(xiàng)目源碼:https://gitee.com/chaojiangcj/springboot-dynamic-datasource

到此這篇關(guān)于springboot配置多數(shù)據(jù)源的一款框架的文章就介紹到這了,更多相關(guān)springboot多數(shù)據(jù)源配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot中Shiro緩存使用Redis、Ehcache的方法

    SpringBoot中Shiro緩存使用Redis、Ehcache的方法

    這篇文章主要介紹了SpringBoot中Shiro緩存使用Redis、Ehcache的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • springboot整合websocket最基礎(chǔ)入門使用教程詳解

    springboot整合websocket最基礎(chǔ)入門使用教程詳解

    這篇文章主要介紹了springboot整合websocket最基礎(chǔ)入門使用教程詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • Java String的intern方法使用場景示例

    Java String的intern方法使用場景示例

    這篇文章主要介紹了Java String的intern方法使用場景示例,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-11-11
  • Springboot基于BCrypt非對稱加密字符串的實(shí)現(xiàn)

    Springboot基于BCrypt非對稱加密字符串的實(shí)現(xiàn)

    本文主要介紹了Springboot基于BCrypt非對稱加密字符串的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • 詳解MyBatis直接執(zhí)行SQL查詢及數(shù)據(jù)批量插入

    詳解MyBatis直接執(zhí)行SQL查詢及數(shù)據(jù)批量插入

    這篇文章主要介紹了MyBatis直接執(zhí)行SQL查詢及數(shù)據(jù)批量插入的相關(guān)知識,需要的朋友一起學(xué)習(xí)吧
    2016-01-01
  • 使用Spring?Boot?2.x構(gòu)建Web服務(wù)的詳細(xì)代碼

    使用Spring?Boot?2.x構(gòu)建Web服務(wù)的詳細(xì)代碼

    這篇文章主要介紹了使用Spring?Boot?2.x構(gòu)建Web服務(wù)的詳細(xì)代碼,主要基于JWT的身份認(rèn)證,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • SpringData關(guān)鍵字查詢實(shí)現(xiàn)方法詳解

    SpringData關(guān)鍵字查詢實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了SpringData關(guān)鍵字查詢實(shí)現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • springboot訪問請求404的原因及解決辦法

    springboot訪問請求404的原因及解決辦法

    在使用Spring Boot開發(fā)應(yīng)用程序時(shí),有時(shí)可能會遇到訪問請求出現(xiàn)404錯(cuò)誤的情況,即請求的資源未找到,這篇文章主要給大家介紹了關(guān)于springboot訪問請求404的原因及解決辦法,需要的朋友可以參考下
    2023-09-09
  • Mybatis動(dòng)態(tài)調(diào)用表名和字段名的解決方法

    Mybatis動(dòng)態(tài)調(diào)用表名和字段名的解決方法

    今天在項(xiàng)目開發(fā)中有個(gè)業(yè)務(wù)是需要限制各個(gè)用戶對某些表里的字段查詢以及某些字段是否顯示,這種情況下,就需要構(gòu)建sql來動(dòng)態(tài)傳入表名、字段名了,下面給大家介紹mybatis動(dòng)態(tài)調(diào)用表名和字段名的解決方法,一起看看吧
    2016-10-10
  • 盤點(diǎn)幾種常見的java排序算法

    盤點(diǎn)幾種常見的java排序算法

    所謂排序就是使一串記錄,按照其中的某個(gè)或某些關(guān)鍵字的大小,遞增或遞減的排列起來的操作,下面這篇文章主要給大家介紹了幾種常見的java排序算法的相關(guān)資料,需要的朋友可以參考下
    2021-11-11

最新評論