TestBatchController.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package com.ruoyi.demo.controller;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.ruoyi.common.core.controller.BaseController;
  4. import com.ruoyi.common.core.domain.AjaxResult;
  5. import com.ruoyi.demo.domain.TestDemo;
  6. import com.ruoyi.demo.service.ITestDemoService;
  7. import io.swagger.annotations.Api;
  8. import io.swagger.annotations.ApiOperation;
  9. import lombok.RequiredArgsConstructor;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.web.bind.annotation.DeleteMapping;
  12. import org.springframework.web.bind.annotation.PostMapping;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. /**
  18. * 测试批量方法
  19. *
  20. * @author Lion Li
  21. * @date 2021-05-30
  22. */
  23. @Api(value = "测试批量方法", tags = {"测试批量方法"})
  24. @RequiredArgsConstructor(onConstructor_ = @Autowired)
  25. @RestController
  26. @RequestMapping("/demo/batch")
  27. public class TestBatchController extends BaseController {
  28. private final ITestDemoService iTestDemoService;
  29. /**
  30. * 新增批量方法 可完美替代 saveBatch 秒级插入上万数据 (对mysql负荷较大)
  31. *
  32. * 3.5.0 版本 增加 rewriteBatchedStatements=true 批处理参数 使 MP 原生批处理可以达到同样的速度
  33. */
  34. @ApiOperation(value = "新增批量方法")
  35. @PostMapping("/add")
  36. // @DS("slave")
  37. public AjaxResult<Void> add() {
  38. List<TestDemo> list = new ArrayList<>();
  39. for (int i = 0; i < 1000; i++) {
  40. list.add(new TestDemo().setOrderNum(-1L).setTestKey("批量新增").setValue("测试新增"));
  41. }
  42. return toAjax(iTestDemoService.saveAll(list) ? 1 : 0);
  43. }
  44. /**
  45. * 新增或更新 可完美替代 saveOrUpdateBatch 高性能
  46. *
  47. * 3.5.0 版本 增加 rewriteBatchedStatements=true 批处理参数 使 MP 原生批处理可以达到同样的速度
  48. */
  49. @ApiOperation(value = "新增或更新批量方法")
  50. @PostMapping("/addOrUpdate")
  51. // @DS("slave")
  52. public AjaxResult<Void> addOrUpdate() {
  53. List<TestDemo> list = new ArrayList<>();
  54. for (int i = 0; i < 1000; i++) {
  55. list.add(new TestDemo().setOrderNum(-1L).setTestKey("批量新增").setValue("测试新增"));
  56. }
  57. iTestDemoService.saveAll(list);
  58. for (int i = 0; i < list.size(); i++) {
  59. TestDemo testDemo = list.get(i);
  60. testDemo.setTestKey("批量新增或修改").setValue("批量新增或修改");
  61. if (i % 2 == 0) {
  62. testDemo.setId(null);
  63. }
  64. }
  65. return toAjax(iTestDemoService.saveOrUpdateAll(list) ? 1 : 0);
  66. }
  67. /**
  68. * 删除批量方法
  69. */
  70. @ApiOperation(value = "删除批量方法")
  71. @DeleteMapping()
  72. // @DS("slave")
  73. public AjaxResult<Void> remove() {
  74. return toAjax(iTestDemoService.remove(new LambdaQueryWrapper<TestDemo>()
  75. .eq(TestDemo::getOrderNum, -1L)) ? 1 : 0);
  76. }
  77. }