Commit cd341da674be599301a8fffa3cadc664505c58ad
1 parent
bc77af09
perf:【INFRA 基础设施】代码生成主子表非 erp 模式时,当子表一对多时更新改为通过 diffList 实现对应的增删改
Showing
4 changed files
with
75 additions
and
15 deletions
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java
| ... | ... | @@ -236,6 +236,7 @@ public class CodegenEngine { |
| 236 | 236 | + '.' + "framework"); // 用于后续获取测试类的 package 地址 |
| 237 | 237 | globalBindingMap.put("jakartaPackage", jakartaEnable ? "jakarta" : "javax"); |
| 238 | 238 | globalBindingMap.put("voType", codegenProperties.getVoType()); |
| 239 | + globalBindingMap.put("deleteBatchEnable", codegenProperties.getDeleteBatchEnable()); | |
| 239 | 240 | // 全局 Java Bean |
| 240 | 241 | globalBindingMap.put("CommonResultClassName", CommonResult.class.getName()); |
| 241 | 242 | globalBindingMap.put("PageResultClassName", PageResult.class.getName()); |
| ... | ... | @@ -257,6 +258,7 @@ public class CodegenEngine { |
| 257 | 258 | globalBindingMap.put("ApiAccessLogClassName", ApiAccessLog.class.getName()); |
| 258 | 259 | globalBindingMap.put("OperateTypeEnumClassName", OperateTypeEnum.class.getName()); |
| 259 | 260 | globalBindingMap.put("BeanUtils", BeanUtils.class.getName()); |
| 261 | + globalBindingMap.put("CollectionUtilsClassName", CollectionUtils.class.getName()); | |
| 260 | 262 | } |
| 261 | 263 | |
| 262 | 264 | /** |
| ... | ... | @@ -382,7 +384,6 @@ public class CodegenEngine { |
| 382 | 384 | bindingMap.put("columns", columns); |
| 383 | 385 | bindingMap.put("primaryColumn", CollectionUtils.findFirst(columns, CodegenColumnDO::getPrimaryKey)); // 主键字段 |
| 384 | 386 | bindingMap.put("sceneEnum", CodegenSceneEnum.valueOf(table.getScene())); |
| 385 | - bindingMap.put("deleteBatchEnable", codegenProperties.getDeleteBatchEnable()); | |
| 386 | 387 | // className 相关 |
| 387 | 388 | // 去掉指定前缀,将 TestDictType 转换成 DictType. 因为在 create 等方法后,不需要带上 Test 前缀 |
| 388 | 389 | String className = table.getClassName(); | ... | ... |
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo03/inner/Demo03StudentInnerServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.infra.service.demo.demo03.inner; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | +import cn.hutool.core.util.ObjectUtil; | |
| 4 | 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | 6 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 6 | 7 | import cn.iocoder.yudao.module.infra.controller.admin.demo.demo03.inner.vo.Demo03StudentInnerPageReqVO; |
| ... | ... | @@ -19,6 +20,8 @@ import org.springframework.validation.annotation.Validated; |
| 19 | 20 | import java.util.List; |
| 20 | 21 | |
| 21 | 22 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 23 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; | |
| 24 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList; | |
| 22 | 25 | import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.DEMO03_STUDENT_NOT_EXISTS; |
| 23 | 26 | |
| 24 | 27 | /** |
| ... | ... | @@ -127,9 +130,26 @@ public class Demo03StudentInnerServiceImpl implements Demo03StudentInnerService |
| 127 | 130 | } |
| 128 | 131 | |
| 129 | 132 | private void updateDemo03CourseList(Long studentId, List<Demo03CourseDO> list) { |
| 130 | - deleteDemo03CourseByStudentId(studentId); | |
| 131 | - list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下:1)id 冲突;2)updateTime 不更新 | |
| 132 | - createDemo03CourseList(studentId, list); | |
| 133 | + list.forEach(o -> o.setStudentId(studentId)); | |
| 134 | + List<Demo03CourseDO> oldList = demo03CourseInnerMapper.selectListByStudentId(studentId); | |
| 135 | + List<List<Demo03CourseDO>> diffList = diffList(oldList, list, (oldVal, newVal) -> { | |
| 136 | + boolean same = ObjectUtil.equal(oldVal.getId(), newVal.getId()); | |
| 137 | + if (same) { | |
| 138 | + newVal.setId(oldVal.getId()).setUpdater(null).setUpdateTime(null); // 解决更新情况下:updateTime 不更新 | |
| 139 | + } | |
| 140 | + return same; | |
| 141 | + }); | |
| 142 | + | |
| 143 | + // 第二步,批量添加、修改、删除 | |
| 144 | + if (CollUtil.isNotEmpty(diffList.get(0))) { | |
| 145 | + demo03CourseInnerMapper.insertBatch(diffList.get(0)); | |
| 146 | + } | |
| 147 | + if (CollUtil.isNotEmpty(diffList.get(1))) { | |
| 148 | + demo03CourseInnerMapper.updateBatch(diffList.get(1)); | |
| 149 | + } | |
| 150 | + if (CollUtil.isNotEmpty(diffList.get(2))) { | |
| 151 | + demo03CourseInnerMapper.deleteByIds(convertList(diffList.get(2), Demo03CourseDO::getId)); | |
| 152 | + } | |
| 133 | 153 | } |
| 134 | 154 | |
| 135 | 155 | private void deleteDemo03CourseByStudentId(Long studentId) { | ... | ... |
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/demo/demo03/normal/Demo03StudentNormalServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.infra.service.demo.demo03.normal; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | +import cn.hutool.core.util.ObjectUtil; | |
| 4 | 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | 6 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 6 | 7 | import cn.iocoder.yudao.module.infra.controller.admin.demo.demo03.normal.vo.Demo03StudentNormalPageReqVO; |
| ... | ... | @@ -19,6 +20,8 @@ import org.springframework.validation.annotation.Validated; |
| 19 | 20 | import java.util.List; |
| 20 | 21 | |
| 21 | 22 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 23 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; | |
| 24 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList; | |
| 22 | 25 | import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.DEMO03_STUDENT_NOT_EXISTS; |
| 23 | 26 | |
| 24 | 27 | /** |
| ... | ... | @@ -127,9 +130,26 @@ public class Demo03StudentNormalServiceImpl implements Demo03StudentNormalServic |
| 127 | 130 | } |
| 128 | 131 | |
| 129 | 132 | private void updateDemo03CourseList(Long studentId, List<Demo03CourseDO> list) { |
| 130 | - deleteDemo03CourseByStudentId(studentId); | |
| 131 | - list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下:1)id 冲突;2)updateTime 不更新 | |
| 132 | - createDemo03CourseList(studentId, list); | |
| 133 | + list.forEach(o -> o.setStudentId(studentId)); | |
| 134 | + List<Demo03CourseDO> oldList = demo03CourseNormalMapper.selectListByStudentId(studentId); | |
| 135 | + List<List<Demo03CourseDO>> diffList = diffList(oldList, list, (oldVal, newVal) -> { | |
| 136 | + boolean same = ObjectUtil.equal(oldVal.getId(), newVal.getId()); | |
| 137 | + if (same) { | |
| 138 | + newVal.setId(oldVal.getId()).setUpdater(null).setUpdateTime(null); // 解决更新情况下:updateTime 不更新 | |
| 139 | + } | |
| 140 | + return same; | |
| 141 | + }); | |
| 142 | + | |
| 143 | + // 第二步,批量添加、修改、删除 | |
| 144 | + if (CollUtil.isNotEmpty(diffList.get(0))) { | |
| 145 | + demo03CourseNormalMapper.insertBatch(diffList.get(0)); | |
| 146 | + } | |
| 147 | + if (CollUtil.isNotEmpty(diffList.get(1))) { | |
| 148 | + demo03CourseNormalMapper.updateBatch(diffList.get(1)); | |
| 149 | + } | |
| 150 | + if (CollUtil.isNotEmpty(diffList.get(2))) { | |
| 151 | + demo03CourseNormalMapper.deleteByIds(convertList(diffList.get(2), Demo03CourseDO::getId)); | |
| 152 | + } | |
| 133 | 153 | } |
| 134 | 154 | |
| 135 | 155 | private void deleteDemo03CourseByStudentId(Long studentId) { | ... | ... |
yudao-module-infra/src/main/resources/codegen/java/service/serviceImpl.vm
| ... | ... | @@ -25,6 +25,8 @@ import ${basePackage}.module.${subTable.moduleName}.dal.mysql.${subTable.busines |
| 25 | 25 | #end |
| 26 | 26 | |
| 27 | 27 | import static ${ServiceExceptionUtilClassName}.exception; |
| 28 | +import static ${CollectionUtilsClassName}.convertList; | |
| 29 | +import static ${CollectionUtilsClassName}.diffList; | |
| 28 | 30 | import static ${basePackage}.module.${table.moduleName}.enums.ErrorCodeConstants.*; |
| 29 | 31 | |
| 30 | 32 | /** |
| ... | ... | @@ -56,9 +58,9 @@ public class ${table.className}ServiceImpl implements ${table.className}Service |
| 56 | 58 | #set ($TreeParentJavaField = $treeParentColumn.javaField.substring(0,1).toUpperCase() + ${treeParentColumn.javaField.substring(1)})##首字母大写 |
| 57 | 59 | #set ($TreeNameJavaField = $treeNameColumn.javaField.substring(0,1).toUpperCase() + ${treeNameColumn.javaField.substring(1)})##首字母大写 |
| 58 | 60 | // 校验${treeParentColumn.columnComment}的有效性 |
| 59 | - validateParent${simpleClassName}(null, ${createReqVOVar}.get${TreeParentJavaField}()); | |
| 61 | + validateParent${simpleClassName}(null, ${saveReqVOVar}.get${TreeParentJavaField}()); | |
| 60 | 62 | // 校验${treeNameColumn.columnComment}的唯一性 |
| 61 | - validate${simpleClassName}${TreeNameJavaField}Unique(null, ${createReqVOVar}.get${TreeParentJavaField}(), ${createReqVOVar}.get${TreeNameJavaField}()); | |
| 63 | + validate${simpleClassName}${TreeNameJavaField}Unique(null, ${saveReqVOVar}.get${TreeParentJavaField}(), ${saveReqVOVar}.get${TreeNameJavaField}()); | |
| 62 | 64 | |
| 63 | 65 | #end |
| 64 | 66 | // 插入 |
| ... | ... | @@ -76,9 +78,9 @@ public class ${table.className}ServiceImpl implements ${table.className}Service |
| 76 | 78 | #set ($subJoinColumn = $subJoinColumns.get($index))##当前 join 字段 |
| 77 | 79 | #set ($SubJoinColumnName = $subJoinColumn.javaField.substring(0,1).toUpperCase() + ${subJoinColumn.javaField.substring(1)})##首字母大写 |
| 78 | 80 | #if ( $subTable.subJoinMany) |
| 79 | - create${subSimpleClassName}List(${classNameVar}.getId(), ${createReqVOVar}.get${subSimpleClassNames.get($index)}s()); | |
| 81 | + create${subSimpleClassName}List(${classNameVar}.getId(), ${saveReqVOVar}.get${subSimpleClassNames.get($index)}s()); | |
| 80 | 82 | #else |
| 81 | - create${subSimpleClassName}(${classNameVar}.getId(), ${createReqVOVar}.get${subSimpleClassNames.get($index)}()); | |
| 83 | + create${subSimpleClassName}(${classNameVar}.getId(), ${saveReqVOVar}.get${subSimpleClassNames.get($index)}()); | |
| 82 | 84 | #end |
| 83 | 85 | #end |
| 84 | 86 | #end |
| ... | ... | @@ -372,14 +374,31 @@ public class ${table.className}ServiceImpl implements ${table.className}Service |
| 372 | 374 | #else |
| 373 | 375 | #if ( $subTable.subJoinMany) |
| 374 | 376 | private void create${subSimpleClassName}List(${primaryColumn.javaType} ${subJoinColumn.javaField}, List<${subTable.className}DO> list) { |
| 375 | - list.forEach(o -> o.set$SubJoinColumnName(${subJoinColumn.javaField})); | |
| 377 | + list.forEach(o -> o.set${SubJoinColumnName}(${subJoinColumn.javaField})); | |
| 376 | 378 | ${subClassNameVars.get($index)}Mapper.insertBatch(list); |
| 377 | 379 | } |
| 378 | 380 | |
| 379 | 381 | private void update${subSimpleClassName}List(${primaryColumn.javaType} ${subJoinColumn.javaField}, List<${subTable.className}DO> list) { |
| 380 | - delete${subSimpleClassName}By${SubJoinColumnName}(${subJoinColumn.javaField}); | |
| 381 | - list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下:1)id 冲突;2)updateTime 不更新 | |
| 382 | - create${subSimpleClassName}List(${subJoinColumn.javaField}, list); | |
| 382 | + list.forEach(o -> o.set${SubJoinColumnName}(${subJoinColumn.javaField})); | |
| 383 | + List<${subTable.className}DO> oldList = ${subClassNameVar}Mapper.selectListBy${SubJoinColumnName}(${subJoinColumn.javaField}); | |
| 384 | + List<List<${subTable.className}DO>> diffList = diffList(oldList, list, (oldVal, newVal) -> { | |
| 385 | + boolean same = ObjectUtil.equal(oldVal.getId(), newVal.getId()); | |
| 386 | + if (same) { | |
| 387 | + newVal.setId(oldVal.getId()).setUpdater(null).setUpdateTime(null); // 解决更新情况下:updateTime 不更新 | |
| 388 | + } | |
| 389 | + return same; | |
| 390 | + }); | |
| 391 | + | |
| 392 | + // 第二步,批量添加、修改、删除 | |
| 393 | + if (CollUtil.isNotEmpty(diffList.get(0))) { | |
| 394 | + ${subClassNameVar}Mapper.insertBatch(diffList.get(0)); | |
| 395 | + } | |
| 396 | + if (CollUtil.isNotEmpty(diffList.get(1))) { | |
| 397 | + ${subClassNameVar}Mapper.updateBatch(diffList.get(1)); | |
| 398 | + } | |
| 399 | + if (CollUtil.isNotEmpty(diffList.get(2))) { | |
| 400 | + ${subClassNameVar}Mapper.deleteByIds(convertList(diffList.get(2), ${subTable.className}DO::getId)); | |
| 401 | + } | |
| 383 | 402 | } |
| 384 | 403 | |
| 385 | 404 | #else | ... | ... |