TestBatchController.java 3.2 KB

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