Commit 06649624e590c78cd003ea20ed47cac8ebcd4559
1 parent
a75bb825
代码生成:支持设置主子表的信息
Showing
5 changed files
with
29 additions
and
5 deletions
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/collection/CollectionUtils.java
| ... | ... | @@ -238,7 +238,7 @@ public class CollectionUtils { |
| 238 | 238 | if (CollUtil.isEmpty(from)) { |
| 239 | 239 | return null; |
| 240 | 240 | } |
| 241 | - assert from.size() > 0; // 断言,避免告警 | |
| 241 | + assert !from.isEmpty(); // 断言,避免告警 | |
| 242 | 242 | T t = from.stream().max(Comparator.comparing(valueFunc)).get(); |
| 243 | 243 | return valueFunc.apply(t); |
| 244 | 244 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/mapper/BaseMapperX.java
| ... | ... | @@ -93,10 +93,15 @@ public interface BaseMapperX<T> extends MPJBaseMapper<T> { |
| 93 | 93 | return selectList(new LambdaQueryWrapper<T>().in(field, values)); |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | + @Deprecated | |
| 96 | 97 | default List<T> selectList(SFunction<T, ?> leField, SFunction<T, ?> geField, Object value) { |
| 97 | 98 | return selectList(new LambdaQueryWrapper<T>().le(leField, value).ge(geField, value)); |
| 98 | 99 | } |
| 99 | 100 | |
| 101 | + default List<T> selectList(SFunction<T, ?> field1, Object value1, SFunction<T, ?> field2, Object value2) { | |
| 102 | + return selectList(new LambdaQueryWrapper<T>().eq(field1, value1).eq(field2, value2)); | |
| 103 | + } | |
| 104 | + | |
| 100 | 105 | /** |
| 101 | 106 | * 批量插入,适合大量数据插入 |
| 102 | 107 | * | ... | ... |
yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/enums/ErrorCodeConstants.java
| ... | ... | @@ -44,6 +44,8 @@ public interface ErrorCodeConstants { |
| 44 | 44 | ErrorCode CODEGEN_TABLE_INFO_COLUMN_COMMENT_IS_NULL = new ErrorCode(1_003_001_009, "数据库的表字段({})注释未填写"); |
| 45 | 45 | ErrorCode CODEGEN_MASTER_TABLE_NOT_EXISTS = new ErrorCode(1_003_001_010, "主表(id={})定义不存在,请检查"); |
| 46 | 46 | ErrorCode CODEGEN_SUB_COLUMN_NOT_EXISTS = new ErrorCode(1_003_001_011, "子表的字段(id={})不存在,请检查"); |
| 47 | + ErrorCode CODEGEN_MASTER_GENERATION_FAIL_NO_SUB_TABLE = new ErrorCode(1_003_001_012, "主表生成代码失败,原因:它没有子表"); | |
| 48 | + ErrorCode CODEGEN_MASTER_GENERATION_FAIL_NO_SUB_COLUMN = new ErrorCode(1_003_001_013, "主表生成代码失败,原因:它的子表({})没有字段"); | |
| 47 | 49 | |
| 48 | 50 | // ========== 文件配置 1-001-006-000 ========== |
| 49 | 51 | ErrorCode FILE_CONFIG_NOT_EXISTS = new ErrorCode(1_001_006_000, "文件配置不存在"); | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/codegen/CodegenTableMapper.java
| ... | ... | @@ -29,4 +29,9 @@ public interface CodegenTableMapper extends BaseMapperX<CodegenTableDO> { |
| 29 | 29 | return selectList(CodegenTableDO::getDataSourceConfigId, dataSourceConfigId); |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | + default List<CodegenTableDO> selectListByTemplateTypeAndMasterTableId(Integer templateType, Long masterTableId) { | |
| 33 | + return selectList(CodegenTableDO::getTemplateType, templateType, | |
| 34 | + CodegenTableDO::getMasterTableId, masterTableId); | |
| 35 | + } | |
| 36 | + | |
| 32 | 37 | } | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/CodegenServiceImpl.java
| ... | ... | @@ -252,13 +252,25 @@ public class CodegenServiceImpl implements CodegenService { |
| 252 | 252 | List<CodegenTableDO> subTables = null; |
| 253 | 253 | List<List<CodegenColumnDO>> subColumnsList = null; |
| 254 | 254 | if (CodegenTemplateTypeEnum.isMaster(table.getTemplateType())) { |
| 255 | - subTables = codegenTableMapper.selectListByMasterTableId(tableId); | |
| 256 | - subTable = codegenTableMapper.selectById(table.getMasterTableId()); | |
| 257 | - subColumns = codegenColumnMapper.selectListByTableId(table.getMasterTableId()); | |
| 255 | + // 校验子表存在 | |
| 256 | + subTables = codegenTableMapper.selectListByTemplateTypeAndMasterTableId( | |
| 257 | + CodegenTemplateTypeEnum.SUB.getType(), tableId); | |
| 258 | + if (CollUtil.isEmpty(subTables)) { | |
| 259 | + throw exception(CODEGEN_MASTER_GENERATION_FAIL_NO_SUB_TABLE); | |
| 260 | + } | |
| 261 | + // 校验子表的关联字段存在 | |
| 262 | + subColumnsList = new ArrayList<>(); | |
| 263 | + for (CodegenTableDO subTable : subTables) { | |
| 264 | + List<CodegenColumnDO> subColumns = codegenColumnMapper.selectListByTableId(subTable.getId()); | |
| 265 | + if (CollUtil.findOne(subColumns, column -> column.getId().equals(subTable.getSubJoinColumnId())) == null) { | |
| 266 | + throw exception(CODEGEN_SUB_COLUMN_NOT_EXISTS, subTable.getId()); | |
| 267 | + } | |
| 268 | + subColumnsList.add(subColumns); | |
| 269 | + } | |
| 258 | 270 | } |
| 259 | 271 | |
| 260 | 272 | // 执行生成 |
| 261 | - return codegenEngine.execute(table, columns, subTable, subColumns); | |
| 273 | + return codegenEngine.execute(table, columns, subTables, subColumnsList); | |
| 262 | 274 | } |
| 263 | 275 | |
| 264 | 276 | @Override | ... | ... |