Skip to content

Commit

Permalink
feat: 添加 sharding jdbc 测试接口,以 plg_log 为例
Browse files Browse the repository at this point in the history
  • Loading branch information
cadecode committed Dec 1, 2023
1 parent eba893b commit 3f2eeef
Showing 1 changed file with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.github.cadecode.uniboot.example.svc.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.github.cadecode.uniboot.common.plugin.datasource.config.ShardingJdbcConfig;
import com.github.cadecode.uniboot.framework.base.annotation.ApiFormat;
import com.github.cadecode.uniboot.framework.base.plugin.bean.po.PlgLog;
import com.github.cadecode.uniboot.framework.base.plugin.service.PlgLogService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
Expand All @@ -13,6 +17,7 @@
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.List;

/**
* dynamic-datasource 测试
Expand All @@ -23,7 +28,7 @@
@ApiFormat
@Slf4j
@RequiredArgsConstructor
@Api(tags = "dynamic-datasource 测试")
@Api(tags = "动态数据源测试")
@DependsOn("dataSource")
@RestController
@RequestMapping("demo/ds")
Expand All @@ -34,6 +39,8 @@ public class DynamicDsController {
*/
private final AbstractDataSource dataSource;

private final PlgLogService plgLogService;

@DS("master")
@ApiOperation("测试 master 数据源")
@PostMapping("test_master")
Expand All @@ -48,4 +55,23 @@ public String testSlave1() throws SQLException {
return dataSource.getConnection().getMetaData().getURL();
}

@DS(ShardingJdbcConfig.SHARDING_DATA_SOURCE_NAME)
@ApiOperation("测试 sharding 数据源读取")
@PostMapping("test_sharding_read")
public List<PlgLog> testShardingRead() throws SQLException {
return PageHelper.startPage(0, 10)
.<PlgLog>doSelectPageInfo(plgLogService::list).getList();
}

@DS(ShardingJdbcConfig.SHARDING_DATA_SOURCE_NAME)
@ApiOperation("测试 sharding 数据源写入")
@PostMapping("test_sharding_write")
public boolean testShardingWrite() throws SQLException {
return plgLogService.save(PlgLog.builder()
.logType("测试LOG")
.exceptional(false)
.classMethod("DynamicDsController.testShardingWriteAndRead")
.build());
}

}

0 comments on commit 3f2eeef

Please sign in to comment.