Commit fc3bb430c39a477d5cd007009b397d1fcd77fb0a
1 parent
645e6f3c
🔧 简化 dataSourceConfig 模块的 VO
Showing
9 changed files
with
48 additions
and
107 deletions
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/db/DataSourceConfigController.java
| 1 | 1 | package cn.iocoder.yudao.module.infra.controller.admin.db; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | -import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigCreateReqVO; | |
| 4 | +import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | |
| 5 | 5 | import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigRespVO; |
| 6 | -import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigUpdateReqVO; | |
| 7 | -import cn.iocoder.yudao.module.infra.convert.db.DataSourceConfigConvert; | |
| 6 | +import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigSaveReqVO; | |
| 8 | 7 | import cn.iocoder.yudao.module.infra.dal.dataobject.db.DataSourceConfigDO; |
| 9 | 8 | import cn.iocoder.yudao.module.infra.service.db.DataSourceConfigService; |
| 10 | -import io.swagger.v3.oas.annotations.tags.Tag; | |
| 11 | -import io.swagger.v3.oas.annotations.Parameter; | |
| 12 | 9 | import io.swagger.v3.oas.annotations.Operation; |
| 10 | +import io.swagger.v3.oas.annotations.Parameter; | |
| 11 | +import io.swagger.v3.oas.annotations.tags.Tag; | |
| 13 | 12 | import org.springframework.security.access.prepost.PreAuthorize; |
| 14 | 13 | import org.springframework.validation.annotation.Validated; |
| 15 | 14 | import org.springframework.web.bind.annotation.*; |
| ... | ... | @@ -32,14 +31,14 @@ public class DataSourceConfigController { |
| 32 | 31 | @PostMapping("/create") |
| 33 | 32 | @Operation(summary = "创建数据源配置") |
| 34 | 33 | @PreAuthorize("@ss.hasPermission('infra:data-source-config:create')") |
| 35 | - public CommonResult<Long> createDataSourceConfig(@Valid @RequestBody DataSourceConfigCreateReqVO createReqVO) { | |
| 34 | + public CommonResult<Long> createDataSourceConfig(@Valid @RequestBody DataSourceConfigSaveReqVO createReqVO) { | |
| 36 | 35 | return success(dataSourceConfigService.createDataSourceConfig(createReqVO)); |
| 37 | 36 | } |
| 38 | 37 | |
| 39 | 38 | @PutMapping("/update") |
| 40 | 39 | @Operation(summary = "更新数据源配置") |
| 41 | 40 | @PreAuthorize("@ss.hasPermission('infra:data-source-config:update')") |
| 42 | - public CommonResult<Boolean> updateDataSourceConfig(@Valid @RequestBody DataSourceConfigUpdateReqVO updateReqVO) { | |
| 41 | + public CommonResult<Boolean> updateDataSourceConfig(@Valid @RequestBody DataSourceConfigSaveReqVO updateReqVO) { | |
| 43 | 42 | dataSourceConfigService.updateDataSourceConfig(updateReqVO); |
| 44 | 43 | return success(true); |
| 45 | 44 | } |
| ... | ... | @@ -58,8 +57,8 @@ public class DataSourceConfigController { |
| 58 | 57 | @Parameter(name = "id", description = "编号", required = true, example = "1024") |
| 59 | 58 | @PreAuthorize("@ss.hasPermission('infra:data-source-config:query')") |
| 60 | 59 | public CommonResult<DataSourceConfigRespVO> getDataSourceConfig(@RequestParam("id") Long id) { |
| 61 | - DataSourceConfigDO dataSourceConfig = dataSourceConfigService.getDataSourceConfig(id); | |
| 62 | - return success(DataSourceConfigConvert.INSTANCE.convert(dataSourceConfig)); | |
| 60 | + DataSourceConfigDO config = dataSourceConfigService.getDataSourceConfig(id); | |
| 61 | + return success(BeanUtils.toBean(config, DataSourceConfigRespVO.class)); | |
| 63 | 62 | } |
| 64 | 63 | |
| 65 | 64 | @GetMapping("/list") |
| ... | ... | @@ -67,7 +66,7 @@ public class DataSourceConfigController { |
| 67 | 66 | @PreAuthorize("@ss.hasPermission('infra:data-source-config:query')") |
| 68 | 67 | public CommonResult<List<DataSourceConfigRespVO>> getDataSourceConfigList() { |
| 69 | 68 | List<DataSourceConfigDO> list = dataSourceConfigService.getDataSourceConfigList(); |
| 70 | - return success(DataSourceConfigConvert.INSTANCE.convertList(list)); | |
| 69 | + return success(BeanUtils.toBean(list, DataSourceConfigRespVO.class)); | |
| 71 | 70 | } |
| 72 | 71 | |
| 73 | 72 | } | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/db/vo/DataSourceConfigCreateReqVO.java deleted
| 1 | -package cn.iocoder.yudao.module.infra.controller.admin.db.vo; | |
| 2 | - | |
| 3 | -import io.swagger.v3.oas.annotations.media.Schema; | |
| 4 | -import lombok.*; | |
| 5 | -import javax.validation.constraints.*; | |
| 6 | - | |
| 7 | -@Schema(description = "管理后台 - 数据源配置创建 Request VO") | |
| 8 | -@Data | |
| 9 | -@EqualsAndHashCode(callSuper = true) | |
| 10 | -@ToString(callSuper = true) | |
| 11 | -public class DataSourceConfigCreateReqVO extends DataSourceConfigBaseVO { | |
| 12 | - | |
| 13 | - @Schema(description = "密码", requiredMode = Schema.RequiredMode.REQUIRED, example = "123456") | |
| 14 | - @NotNull(message = "密码不能为空") | |
| 15 | - private String password; | |
| 16 | - | |
| 17 | -} |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/db/vo/DataSourceConfigRespVO.java
| 1 | 1 | package cn.iocoder.yudao.module.infra.controller.admin.db.vo; |
| 2 | 2 | |
| 3 | 3 | import io.swagger.v3.oas.annotations.media.Schema; |
| 4 | -import lombok.*; | |
| 4 | +import lombok.Data; | |
| 5 | 5 | |
| 6 | 6 | import java.time.LocalDateTime; |
| 7 | 7 | |
| 8 | 8 | @Schema(description = "管理后台 - 数据源配置 Response VO") |
| 9 | 9 | @Data |
| 10 | -@EqualsAndHashCode(callSuper = true) | |
| 11 | -@ToString(callSuper = true) | |
| 12 | -public class DataSourceConfigRespVO extends DataSourceConfigBaseVO { | |
| 10 | +public class DataSourceConfigRespVO { | |
| 13 | 11 | |
| 14 | 12 | @Schema(description = "主键编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") |
| 15 | 13 | private Integer id; |
| 16 | 14 | |
| 15 | + @Schema(description = "数据源名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "test") | |
| 16 | + private String name; | |
| 17 | + | |
| 18 | + @Schema(description = "数据源连接", requiredMode = Schema.RequiredMode.REQUIRED, example = "jdbc:mysql://127.0.0.1:3306/ruoyi-vue-pro") | |
| 19 | + private String url; | |
| 20 | + | |
| 21 | + @Schema(description = "用户名", requiredMode = Schema.RequiredMode.REQUIRED, example = "root") | |
| 22 | + private String username; | |
| 23 | + | |
| 17 | 24 | @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED) |
| 18 | 25 | private LocalDateTime createTime; |
| 19 | 26 | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/db/vo/DataSourceConfigBaseVO.java renamed to yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/db/vo/DataSourceConfigSaveReqVO.java
| 1 | 1 | package cn.iocoder.yudao.module.infra.controller.admin.db.vo; |
| 2 | + | |
| 2 | 3 | import io.swagger.v3.oas.annotations.media.Schema; |
| 3 | 4 | import lombok.*; |
| 4 | 5 | import javax.validation.constraints.*; |
| 5 | 6 | |
| 6 | -/** | |
| 7 | -* 数据源配置 Base VO,提供给添加、修改、详细的子 VO 使用 | |
| 8 | -* 如果子 VO 存在差异的字段,请不要添加到这里,影响 Swagger 文档生成 | |
| 9 | -*/ | |
| 7 | +@Schema(description = "管理后台 - 数据源配置创建/修改 Request VO") | |
| 10 | 8 | @Data |
| 11 | -public class DataSourceConfigBaseVO { | |
| 9 | +public class DataSourceConfigSaveReqVO { | |
| 10 | + | |
| 11 | + @Schema(description = "主键编号", example = "1024") | |
| 12 | + private Long id; | |
| 12 | 13 | |
| 13 | 14 | @Schema(description = "数据源名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "test") |
| 14 | 15 | @NotNull(message = "数据源名称不能为空") |
| ... | ... | @@ -22,4 +23,8 @@ public class DataSourceConfigBaseVO { |
| 22 | 23 | @NotNull(message = "用户名不能为空") |
| 23 | 24 | private String username; |
| 24 | 25 | |
| 26 | + @Schema(description = "密码", requiredMode = Schema.RequiredMode.REQUIRED, example = "123456") | |
| 27 | + @NotNull(message = "密码不能为空") | |
| 28 | + private String password; | |
| 29 | + | |
| 25 | 30 | } | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/db/vo/DataSourceConfigUpdateReqVO.java deleted
| 1 | -package cn.iocoder.yudao.module.infra.controller.admin.db.vo; | |
| 2 | - | |
| 3 | -import io.swagger.v3.oas.annotations.media.Schema; | |
| 4 | -import lombok.*; | |
| 5 | -import javax.validation.constraints.*; | |
| 6 | - | |
| 7 | -@Schema(description = "管理后台 - 数据源配置更新 Request VO") | |
| 8 | -@Data | |
| 9 | -@EqualsAndHashCode(callSuper = true) | |
| 10 | -@ToString(callSuper = true) | |
| 11 | -public class DataSourceConfigUpdateReqVO extends DataSourceConfigBaseVO { | |
| 12 | - | |
| 13 | - @Schema(description = "主键编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") | |
| 14 | - @NotNull(message = "主键编号不能为空") | |
| 15 | - private Long id; | |
| 16 | - | |
| 17 | - @Schema(description = "密码", requiredMode = Schema.RequiredMode.REQUIRED, example = "123456") | |
| 18 | - @NotNull(message = "密码不能为空") | |
| 19 | - private String password; | |
| 20 | - | |
| 21 | -} |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/convert/db/DataSourceConfigConvert.java deleted
| 1 | -package cn.iocoder.yudao.module.infra.convert.db; | |
| 2 | - | |
| 3 | -import java.util.*; | |
| 4 | - | |
| 5 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 6 | - | |
| 7 | -import org.mapstruct.Mapper; | |
| 8 | -import org.mapstruct.factory.Mappers; | |
| 9 | -import cn.iocoder.yudao.module.infra.controller.admin.db.vo.*; | |
| 10 | -import cn.iocoder.yudao.module.infra.dal.dataobject.db.DataSourceConfigDO; | |
| 11 | - | |
| 12 | -/** | |
| 13 | - * 数据源配置 Convert | |
| 14 | - * | |
| 15 | - * @author 芋道源码 | |
| 16 | - */ | |
| 17 | -@Mapper | |
| 18 | -public interface DataSourceConfigConvert { | |
| 19 | - | |
| 20 | - DataSourceConfigConvert INSTANCE = Mappers.getMapper(DataSourceConfigConvert.class); | |
| 21 | - | |
| 22 | - DataSourceConfigDO convert(DataSourceConfigCreateReqVO bean); | |
| 23 | - | |
| 24 | - DataSourceConfigDO convert(DataSourceConfigUpdateReqVO bean); | |
| 25 | - | |
| 26 | - DataSourceConfigRespVO convert(DataSourceConfigDO bean); | |
| 27 | - | |
| 28 | - List<DataSourceConfigRespVO> convertList(List<DataSourceConfigDO> list); | |
| 29 | - | |
| 30 | -} |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/db/DataSourceConfigService.java
| 1 | 1 | package cn.iocoder.yudao.module.infra.service.db; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigCreateReqVO; | |
| 4 | -import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigUpdateReqVO; | |
| 3 | +import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigSaveReqVO; | |
| 5 | 4 | import cn.iocoder.yudao.module.infra.dal.dataobject.db.DataSourceConfigDO; |
| 6 | 5 | |
| 7 | 6 | import javax.validation.Valid; |
| ... | ... | @@ -20,14 +19,14 @@ public interface DataSourceConfigService { |
| 20 | 19 | * @param createReqVO 创建信息 |
| 21 | 20 | * @return 编号 |
| 22 | 21 | */ |
| 23 | - Long createDataSourceConfig(@Valid DataSourceConfigCreateReqVO createReqVO); | |
| 22 | + Long createDataSourceConfig(@Valid DataSourceConfigSaveReqVO createReqVO); | |
| 24 | 23 | |
| 25 | 24 | /** |
| 26 | 25 | * 更新数据源配置 |
| 27 | 26 | * |
| 28 | 27 | * @param updateReqVO 更新信息 |
| 29 | 28 | */ |
| 30 | - void updateDataSourceConfig(@Valid DataSourceConfigUpdateReqVO updateReqVO); | |
| 29 | + void updateDataSourceConfig(@Valid DataSourceConfigSaveReqVO updateReqVO); | |
| 31 | 30 | |
| 32 | 31 | /** |
| 33 | 32 | * 删除数据源配置 | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/db/DataSourceConfigServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.infra.service.db; |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | |
| 3 | 4 | import cn.iocoder.yudao.framework.mybatis.core.util.JdbcUtils; |
| 4 | -import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigCreateReqVO; | |
| 5 | -import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigUpdateReqVO; | |
| 6 | -import cn.iocoder.yudao.module.infra.convert.db.DataSourceConfigConvert; | |
| 5 | +import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigSaveReqVO; | |
| 7 | 6 | import cn.iocoder.yudao.module.infra.dal.dataobject.db.DataSourceConfigDO; |
| 8 | 7 | import cn.iocoder.yudao.module.infra.dal.mysql.db.DataSourceConfigMapper; |
| 9 | 8 | import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty; |
| ... | ... | @@ -35,21 +34,21 @@ public class DataSourceConfigServiceImpl implements DataSourceConfigService { |
| 35 | 34 | private DynamicDataSourceProperties dynamicDataSourceProperties; |
| 36 | 35 | |
| 37 | 36 | @Override |
| 38 | - public Long createDataSourceConfig(DataSourceConfigCreateReqVO createReqVO) { | |
| 39 | - DataSourceConfigDO dataSourceConfig = DataSourceConfigConvert.INSTANCE.convert(createReqVO); | |
| 40 | - validateConnectionOK(dataSourceConfig); | |
| 37 | + public Long createDataSourceConfig(DataSourceConfigSaveReqVO createReqVO) { | |
| 38 | + DataSourceConfigDO config = BeanUtils.toBean(createReqVO, DataSourceConfigDO.class); | |
| 39 | + validateConnectionOK(config); | |
| 41 | 40 | |
| 42 | 41 | // 插入 |
| 43 | - dataSourceConfigMapper.insert(dataSourceConfig); | |
| 42 | + dataSourceConfigMapper.insert(config); | |
| 44 | 43 | // 返回 |
| 45 | - return dataSourceConfig.getId(); | |
| 44 | + return config.getId(); | |
| 46 | 45 | } |
| 47 | 46 | |
| 48 | 47 | @Override |
| 49 | - public void updateDataSourceConfig(DataSourceConfigUpdateReqVO updateReqVO) { | |
| 48 | + public void updateDataSourceConfig(DataSourceConfigSaveReqVO updateReqVO) { | |
| 50 | 49 | // 校验存在 |
| 51 | 50 | validateDataSourceConfigExists(updateReqVO.getId()); |
| 52 | - DataSourceConfigDO updateObj = DataSourceConfigConvert.INSTANCE.convert(updateReqVO); | |
| 51 | + DataSourceConfigDO updateObj = BeanUtils.toBean(updateReqVO, DataSourceConfigDO.class); | |
| 53 | 52 | validateConnectionOK(updateObj); |
| 54 | 53 | |
| 55 | 54 | // 更新 | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/db/DataSourceConfigServiceImplTest.java
| ... | ... | @@ -6,8 +6,7 @@ import cn.hutool.crypto.symmetric.AES; |
| 6 | 6 | import cn.iocoder.yudao.framework.mybatis.core.type.EncryptTypeHandler; |
| 7 | 7 | import cn.iocoder.yudao.framework.mybatis.core.util.JdbcUtils; |
| 8 | 8 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| 9 | -import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigCreateReqVO; | |
| 10 | -import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigUpdateReqVO; | |
| 9 | +import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigSaveReqVO; | |
| 11 | 10 | import cn.iocoder.yudao.module.infra.dal.dataobject.db.DataSourceConfigDO; |
| 12 | 11 | import cn.iocoder.yudao.module.infra.dal.mysql.db.DataSourceConfigMapper; |
| 13 | 12 | import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty; |
| ... | ... | @@ -70,7 +69,8 @@ public class DataSourceConfigServiceImplTest extends BaseDbUnitTest { |
| 70 | 69 | public void testCreateDataSourceConfig_success() { |
| 71 | 70 | try (MockedStatic<JdbcUtils> databaseUtilsMock = mockStatic(JdbcUtils.class)) { |
| 72 | 71 | // 准备参数 |
| 73 | - DataSourceConfigCreateReqVO reqVO = randomPojo(DataSourceConfigCreateReqVO.class); | |
| 72 | + DataSourceConfigSaveReqVO reqVO = randomPojo(DataSourceConfigSaveReqVO.class) | |
| 73 | + .setId(null); // 避免 id 被设置 | |
| 74 | 74 | // mock 方法 |
| 75 | 75 | databaseUtilsMock.when(() -> JdbcUtils.isConnectionOK(eq(reqVO.getUrl()), |
| 76 | 76 | eq(reqVO.getUsername()), eq(reqVO.getPassword()))).thenReturn(true); |
| ... | ... | @@ -81,7 +81,7 @@ public class DataSourceConfigServiceImplTest extends BaseDbUnitTest { |
| 81 | 81 | assertNotNull(dataSourceConfigId); |
| 82 | 82 | // 校验记录的属性是否正确 |
| 83 | 83 | DataSourceConfigDO dataSourceConfig = dataSourceConfigMapper.selectById(dataSourceConfigId); |
| 84 | - assertPojoEquals(reqVO, dataSourceConfig); | |
| 84 | + assertPojoEquals(reqVO, dataSourceConfig, "id"); | |
| 85 | 85 | } |
| 86 | 86 | } |
| 87 | 87 | |
| ... | ... | @@ -92,7 +92,7 @@ public class DataSourceConfigServiceImplTest extends BaseDbUnitTest { |
| 92 | 92 | DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class); |
| 93 | 93 | dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据 |
| 94 | 94 | // 准备参数 |
| 95 | - DataSourceConfigUpdateReqVO reqVO = randomPojo(DataSourceConfigUpdateReqVO.class, o -> { | |
| 95 | + DataSourceConfigSaveReqVO reqVO = randomPojo(DataSourceConfigSaveReqVO.class, o -> { | |
| 96 | 96 | o.setId(dbDataSourceConfig.getId()); // 设置更新的 ID |
| 97 | 97 | }); |
| 98 | 98 | // mock 方法 |
| ... | ... | @@ -110,7 +110,7 @@ public class DataSourceConfigServiceImplTest extends BaseDbUnitTest { |
| 110 | 110 | @Test |
| 111 | 111 | public void testUpdateDataSourceConfig_notExists() { |
| 112 | 112 | // 准备参数 |
| 113 | - DataSourceConfigUpdateReqVO reqVO = randomPojo(DataSourceConfigUpdateReqVO.class); | |
| 113 | + DataSourceConfigSaveReqVO reqVO = randomPojo(DataSourceConfigSaveReqVO.class); | |
| 114 | 114 | |
| 115 | 115 | // 调用, 并断言异常 |
| 116 | 116 | assertServiceException(() -> dataSourceConfigService.updateDataSourceConfig(reqVO), DATA_SOURCE_CONFIG_NOT_EXISTS); | ... | ... |