TestBatchController.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.mapper.TestDemoMapper;
  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. /**
  29. * 为了便于测试 直接引入mapper
  30. */
  31. private final TestDemoMapper testDemoMapper;
  32. /**
  33. * 新增批量方法 可完美替代 saveBatch 秒级插入上万数据 (对mysql负荷较大)
  34. *
  35. * 3.5.0 版本 增加 rewriteBatchedStatements=true 批处理参数 使 MP 原生批处理可以达到同样的速度
  36. */
  37. @ApiOperation(value = "新增批量方法")
  38. @PostMapping("/add")
  39. // @DS("slave")
  40. public AjaxResult<Void> add() {
  41. List<TestDemo> list = new ArrayList<>();
  42. for (int i = 0; i < 1000; i++) {
  43. list.add(new TestDemo().setOrderNum(-1L).setTestKey("批量新增").setValue("测试新增"));
  44. }
  45. return toAjax(testDemoMapper.insertBatch(list) ? 1 : 0);
  46. }
  47. /**
  48. * 新增或更新 可完美替代 saveOrUpdateBatch 高性能
  49. *
  50. * 3.5.0 版本 增加 rewriteBatchedStatements=true 批处理参数 使 MP 原生批处理可以达到同样的速度
  51. */
  52. @ApiOperation(value = "新增或更新批量方法")
  53. @PostMapping("/addOrUpdate")
  54. // @DS("slave")
  55. public AjaxResult<Void> addOrUpdate() {
  56. List<TestDemo> list = new ArrayList<>();
  57. for (int i = 0; i < 1000; i++) {
  58. list.add(new TestDemo().setOrderNum(-1L).setTestKey("批量新增").setValue("测试新增"));
  59. }
  60. testDemoMapper.insertBatch(list);
  61. for (int i = 0; i < list.size(); i++) {
  62. TestDemo testDemo = list.get(i);
  63. testDemo.setTestKey("批量新增或修改").setValue("批量新增或修改");
  64. if (i % 2 == 0) {
  65. testDemo.setId(null);
  66. }
  67. }
  68. return toAjax(testDemoMapper.insertOrUpdateBatch(list) ? 1 : 0);
  69. }
  70. /**
  71. * 删除批量方法
  72. */
  73. @ApiOperation(value = "删除批量方法")
  74. @DeleteMapping()
  75. // @DS("slave")
  76. public AjaxResult<Void> remove() {
  77. return toAjax(testDemoMapper.delete(new LambdaQueryWrapper<TestDemo>()
  78. .eq(TestDemo::getOrderNum, -1L)));
  79. }
  80. }