spring boot 多数据源切换(dynamic-datasource-spring-boot-starter)
  IDYp5aKWcwEJ 2023年11月02日 34 0
官网 https://dynamic-datasource.com/guide/
集成MybatisPlus https://dynamic-datasource.com/guide/integration/MybatisPlus.html#基础介绍
自动读写分离 https://dynamic-datasource.com/guide/advance/Read-Write-Separation.html
本地事物(不支持spring事务),使用@DSTransactional https://dynamic-datasource.com/guide/tx/Local.html
约定
本框架只做 切换数据源 这件核心的事情。
配置文件所有以下划线 _ 分割的数据源 首部 即为组的名称,相同组名称的数据源会放在一个组下。
切换数据源可以是组名,也可以是具体数据源名称。组名则切换时采用负载均衡算法切换。
默认的数据源名称为 master ,你可以通过 spring.datasource.dynamic.primary 修改。
方法上的注解优先于类上注解。
使用方法
<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
  <version>3.3.1</version>
</dependency>

server:
  port: 8083
spring:
  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<SysUserMapper,SysUser> 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<ShopStore> updateWrapper=new UpdateWrapper<ShopStore>();
        updateWrapper.setSql("storenum = storenum - "+buyNum);
        updateWrapper.eq("productid", productid);
        shopStoreMapper.update(null,updateWrapper);

    }
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  xaeiTka4h8LY   2024年05月31日   37   0   0 MySQL索引
  xaeiTka4h8LY   2024年05月31日   50   0   0 MySQLSQL
  xaeiTka4h8LY   2024年05月31日   31   0   0 字段MySQL
  xaeiTka4h8LY   2024年05月31日   46   0   0 MySQL数据库
  xaeiTka4h8LY   2024年05月17日   50   0   0 MySQLgithub
  xaeiTka4h8LY   2024年05月17日   38   0   0 MySQL数据库
IDYp5aKWcwEJ