您好,欢迎来到好走旅游网。
搜索
您的当前位置:首页springboot多数据源切换(dynamic-datasource-spring-bo。。。

springboot多数据源切换(dynamic-datasource-spring-bo。。。

来源:好走旅游网
springboot多数据源切换(dynamic-datasource-spring-bo。。。

约定

本框架只做 切换数据源 这件核⼼的事情。

配置⽂件所有以下划线 _ 分割的数据源 ⾸部 即为组的名称,相同组名称的数据源会放在⼀个组下。切换数据源可以是组名,也可以是具体数据源名称。组名则切换时采⽤负载均衡算法切换。默认的数据源名称为 master ,你可以通过 spring.datasource.dynamic.primary 修改。⽅法上的注解优先于类上注解。

使⽤⽅法

com.baomidou

dynamic-datasource-spring-boot-starter 3.3.1

server:

port: 8083spring:

application:

name: spring-boot-dynamic-datasource jackson:

default-property-inclusion: non_null date-format: YYYY-MM-dd HH:mm:ss time-zone: GMT+8 datasource: dynamic:

primary: master #设置默认的数据源或者数据源组,默认值即为master datasource: master:

url: jdbc:mysql://localhost:3306/dkn-shop-master?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 username: root password: root

driver-class-name: com.mysql.cj.jdbc.Driver slave_1:

url: jdbc:mysql://localhost:3306/dkn-shop-slave-1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 username: root password: root

driver-class-name: com.mysql.cj.jdbc.Driver slave_2:

url: jdbc:mysql://localhost:3306/dkn-shop-slave-2?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 username: root password: root

driver-class-name: com.mysql.cj.jdbc.Driver user:

url: jdbc:mysql://localhost:3306/dkn-dynamic-user?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 username: root password: root

driver-class-name: com.mysql.cj.jdbc.Driver

logging: level:

com.dkn: debug

org.springframework.web: trace com.baomidou: trace

mybatis-plus: configuration:

log-impl: org.apache.ibatis.logging.stdout.StdOutImpl@RestController

@RequestMapping(\"/Shop\")public class ShopController { @Autowired

private ShopService shopService; @Autowired

private SysUserService sysUserService;

//获取订单信息 从库操作, @GetMapping(\"getOrder\")

public AjaxResult getOrder(Integer id){

ShopOrder shopOrder = shopService.getOrder(id); return AjaxResult.success(shopOrder); }

//购买商品 主库操作 @GetMapping(\"buy\")

public AjaxResult buy(Integer id,Integer num){ shopService.buy(id,num); return AjaxResult.success();

}

//获取⽤户信息

@GetMapping(\"getUserInfo\")

public AjaxResult getUserInfo(Integer userid){ SysUser user = sysUserService.getById(userid); return AjaxResult.success(user); } }

@Service@DS(\"user\")

public class SysUserServiceImpl extends ServiceImpl implements SysUserService { }

@Service

public class ShopServiceImpl implements ShopService { @Autowired

ShopStoreMapper shopStoreMapper; @Autowired

ShopOrderMapper shopOrderMapper; @DS(\"slave\")

public ShopOrder getOrder(Integer id) { return shopOrderMapper.selectById(id); }

@DSTransactional

public void buy(Integer productid, Integer buyNum) { ShopOrder shopOrder=new ShopOrder(); shopOrder.setProductid(productid); shopOrder.setBuynum(buyNum); shopOrderMapper.insert(shopOrder); int a=1/0;

UpdateWrapper updateWrapper=new UpdateWrapper(); updateWrapper.setSql(\"storenum = storenum - \"+buyNum); updateWrapper.eq(\"productid\

shopStoreMapper.update(null,updateWrapper); }}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- haog.cn 版权所有 赣ICP备2024042798号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务