抓蛙师
2021-08-25 545f0bd4b0b2f84fb54c165bc551aa08ca9c073e
ruoyi-demo/src/main/java/com/ruoyi/demo/controller/TestBatchController.java
@@ -5,6 +5,8 @@
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.demo.domain.TestDemo;
import com.ruoyi.demo.service.ITestDemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
@@ -21,6 +23,7 @@
 * @author Lion Li
 * @date 2021-05-30
 */
@Api(value = "测试批量方法", tags = {"测试批量方法"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/demo/batch")
@@ -29,22 +32,31 @@
    private final ITestDemoService iTestDemoService;
    /**
     * 新增批量方法
     * 新增批量方法 可完美替代 saveBatch 秒级插入上万数据 (对mysql负荷较大)
     */
   @ApiOperation(value = "新增批量方法")
    @PostMapping()
//   @DataSource(DataSourceType.SLAVE)
    public AjaxResult<Void> add() {
      List<TestDemo> list = new ArrayList<>();
      for (int i = 0; i < 1000; i++) {
         list.add(new TestDemo().setOrderNum(-1L).setTestKey("批量新增").setValue("测试新增"));
      for (int i = 0; i < 10; i++) {
         TestDemo testDemo = new TestDemo();
         if (i % 2 == 0) {
            list.add(testDemo.setId((long) i + 1).setTestKey("批量更新").setValue("批量更新"));
         } else {
            list.add(testDemo.setOrderNum(-1L).setTestKey("批量新增").setValue("测试新增"));
         }
      }
        return toAjax(iTestDemoService.saveAll(list) ? 1 : 0);
    }
    /**
     * 修改批量方法
     * 删除批量方法
     */
   @ApiOperation(value = "删除批量方法")
    @DeleteMapping()
    public AjaxResult<Void> edit() {
//   @DataSource(DataSourceType.SLAVE)
    public AjaxResult<Void> remove() {
        return toAjax(iTestDemoService.remove(new LambdaQueryWrapper<TestDemo>()
         .eq(TestDemo::getOrderNum, -1L)) ? 1 : 0);
    }