官網 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);
}
}