官网 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);
}
}
标签:spring,mysql,boot,dynamic,datasource,数据源,com,public
From: https://blog.51cto.com/u_16270511/7691801