TestBatchController.java 3.0 KB

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