From 5e6e02e4f4900d5be2415e5875905475c98c7466 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Wed, 15 Nov 2023 09:19:51 +0800 Subject: [PATCH] =?UTF-8?q?vo=20=E7=AE=80=E5=8C=96=EF=BC=9A=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20BeanUtils=20=E6=9B=BF=E4=BB=A3=20mapstruct=20?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/util/object/BeanUtils.java | 37 +++++++++++++++++++ .../controller/admin/dept/PostController.java | 30 ++++++++------- .../{PostReqVO.java => PostSaveReqVO.java} | 2 +- .../system/convert/dept/PostConvert.java | 24 ------------ .../system/service/dept/PostService.java | 6 +-- .../system/service/dept/PostServiceImpl.java | 12 +++--- .../service/dept/PostServiceImplTest.java | 36 +++--------------- 7 files changed, 69 insertions(+), 78 deletions(-) create mode 100644 yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/object/BeanUtils.java rename yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/vo/post/{PostReqVO.java => PostSaveReqVO.java} (98%) delete mode 100644 yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/convert/dept/PostConvert.java diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/object/BeanUtils.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/object/BeanUtils.java new file mode 100644 index 0000000000..e14572a7ff --- /dev/null +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/object/BeanUtils.java @@ -0,0 +1,37 @@ +package cn.iocoder.yudao.framework.common.util.object; + +import cn.hutool.core.bean.BeanUtil; +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; + +import java.util.List; + +/** + * Bean 工具类 + * + * 1. 默认使用 {@link cn.hutool.core.bean.BeanUtil} 作为实现类,虽然不同 bean 工具的性能有差别,但是对绝大多数同学的项目,不用在意这点性能 + * 2. 针对复杂的对象转换,可以搜参考 AuthConvert 实现,通过 mapstruct + default 配合实现 + * + * @author 芋道源码 + */ +public class BeanUtils { + + public static T toBean(Object source, Class targetClass) { + return BeanUtil.toBean(source, targetClass); + } + + public static List toBean(List source, Class targetType) { + if (source == null) { + return null; + } + return CollectionUtils.convertList(source, s -> toBean(s, targetType)); + } + + public static PageResult toBean(PageResult source, Class targetType) { + if (source == null) { + return null; + } + return new PageResult<>(toBean(source.getList(), targetType), source.getTotal()); + } + +} \ No newline at end of file diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/PostController.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/PostController.java index e678a62c75..0f22fda4f7 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/PostController.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/PostController.java @@ -4,15 +4,17 @@ import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; -import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.*; -import cn.iocoder.yudao.module.system.convert.dept.PostConvert; +import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostPageReqVO; +import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostSaveReqVO; +import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostRespVO; import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO; import cn.iocoder.yudao.module.system.service.dept.PostService; -import io.swagger.v3.oas.annotations.tags.Tag; -import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -40,7 +42,7 @@ public class PostController { @PostMapping("/create") @Operation(summary = "创建岗位") @PreAuthorize("@ss.hasPermission('system:post:create')") - public CommonResult createPost(@Valid @RequestBody PostReqVO createReqVO) { + public CommonResult createPost(@Valid @RequestBody PostSaveReqVO createReqVO) { Long postId = postService.createPost(createReqVO); return success(postId); } @@ -48,7 +50,7 @@ public class PostController { @PutMapping("/update") @Operation(summary = "修改岗位") @PreAuthorize("@ss.hasPermission('system:post:update')") - public CommonResult updatePost(@Valid @RequestBody PostReqVO updateReqVO) { + public CommonResult updatePost(@Valid @RequestBody PostSaveReqVO updateReqVO) { postService.updatePost(updateReqVO); return success(true); } @@ -66,7 +68,8 @@ public class PostController { @Parameter(name = "id", description = "岗位编号", required = true, example = "1024") @PreAuthorize("@ss.hasPermission('system:post:query')") public CommonResult getPost(@RequestParam("id") Long id) { - return success(PostConvert.INSTANCE.convert(postService.getPost(id))); + PostDO post = postService.getPost(id); + return success(BeanUtils.toBean(post, PostRespVO.class)); } @GetMapping("/list-all-simple") @@ -76,14 +79,15 @@ public class PostController { List list = postService.getPostList(null, Collections.singleton(CommonStatusEnum.ENABLE.getStatus())); // 排序后,返回给前端 list.sort(Comparator.comparing(PostDO::getSort)); - return success(PostConvert.INSTANCE.convertList(list)); + return success(BeanUtils.toBean(list, PostRespVO.class)); } @GetMapping("/page") @Operation(summary = "获得岗位分页列表") @PreAuthorize("@ss.hasPermission('system:post:query')") - public CommonResult> getPostPage(@Validated PostPageReqVO reqVO) { - return success(PostConvert.INSTANCE.convertPage(postService.getPostPage(reqVO))); + public CommonResult> getPostPage(@Validated PostPageReqVO pageReqVO) { + PageResult pageResult = postService.getPostPage(pageReqVO); + return success(BeanUtils.toBean(pageResult, PostRespVO.class)); } @GetMapping("/export") @@ -92,10 +96,10 @@ public class PostController { @OperateLog(type = EXPORT) public void export(HttpServletResponse response, @Validated PostPageReqVO reqVO) throws IOException { reqVO.setPageSize(PageParam.PAGE_SIZE_NONE); - PageResult pageResult = postService.getPostPage(reqVO); + List list = postService.getPostPage(reqVO).getList(); // 输出 - PageResult list = PostConvert.INSTANCE.convertPage(pageResult); - ExcelUtils.write(response, "岗位数据.xls", "岗位列表", PostRespVO.class, list.getList()); + ExcelUtils.write(response, "岗位数据.xls", "岗位列表", PostRespVO.class, + BeanUtils.toBean(list, PostRespVO.class)); } } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/vo/post/PostReqVO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/vo/post/PostSaveReqVO.java similarity index 98% rename from yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/vo/post/PostReqVO.java rename to yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/vo/post/PostSaveReqVO.java index ac8204a24d..b8d3c24cd9 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/vo/post/PostReqVO.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/vo/post/PostSaveReqVO.java @@ -11,7 +11,7 @@ import javax.validation.constraints.Size; @Schema(description = "管理后台 - 岗位创建/修改 Request VO") @Data -public class PostReqVO { +public class PostSaveReqVO { @Schema(description = "岗位编号", example = "1024") private Long id; diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/convert/dept/PostConvert.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/convert/dept/PostConvert.java deleted file mode 100644 index 8b6c28f1bb..0000000000 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/convert/dept/PostConvert.java +++ /dev/null @@ -1,24 +0,0 @@ -package cn.iocoder.yudao.module.system.convert.dept; - -import cn.iocoder.yudao.framework.common.pojo.PageResult; -import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.*; -import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO; -import org.mapstruct.Mapper; -import org.mapstruct.factory.Mappers; - -import java.util.List; - -@Mapper -public interface PostConvert { - - PostConvert INSTANCE = Mappers.getMapper(PostConvert.class); - - List convertList(List list); - - PageResult convertPage(PageResult page); - - PostRespVO convert(PostDO bean); - - PostDO convert(PostReqVO bean); - -} diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/PostService.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/PostService.java index 88c8b0fbd8..73068e2344 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/PostService.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/PostService.java @@ -3,7 +3,7 @@ package cn.iocoder.yudao.module.system.service.dept; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostPageReqVO; -import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostReqVO; +import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostSaveReqVO; import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO; import org.springframework.lang.Nullable; @@ -25,14 +25,14 @@ public interface PostService { * @param createReqVO 岗位信息 * @return 岗位编号 */ - Long createPost(PostReqVO createReqVO); + Long createPost(PostSaveReqVO createReqVO); /** * 更新岗位 * * @param updateReqVO 岗位信息 */ - void updatePost(PostReqVO updateReqVO); + void updatePost(PostSaveReqVO updateReqVO); /** * 删除岗位信息 diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/PostServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/PostServiceImpl.java index 44031ac3ca..560a8bc8f0 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/PostServiceImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/PostServiceImpl.java @@ -3,9 +3,9 @@ package cn.iocoder.yudao.module.system.service.dept; import cn.hutool.core.collection.CollUtil; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostPageReqVO; -import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostReqVO; -import cn.iocoder.yudao.module.system.convert.dept.PostConvert; +import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostSaveReqVO; import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO; import cn.iocoder.yudao.module.system.dal.mysql.dept.PostMapper; import org.springframework.stereotype.Service; @@ -33,23 +33,23 @@ public class PostServiceImpl implements PostService { private PostMapper postMapper; @Override - public Long createPost(PostReqVO createReqVO) { + public Long createPost(PostSaveReqVO createReqVO) { // 校验正确性 validatePostForCreateOrUpdate(null, createReqVO.getName(), createReqVO.getCode()); // 插入岗位 - PostDO post = PostConvert.INSTANCE.convert(createReqVO); + PostDO post = BeanUtils.toBean(createReqVO, PostDO.class); postMapper.insert(post); return post.getId(); } @Override - public void updatePost(PostReqVO updateReqVO) { + public void updatePost(PostSaveReqVO updateReqVO) { // 校验正确性 validatePostForCreateOrUpdate(updateReqVO.getId(), updateReqVO.getName(), updateReqVO.getCode()); // 更新岗位 - PostDO updateObj = PostConvert.INSTANCE.convert(updateReqVO); + PostDO updateObj = BeanUtils.toBean(updateReqVO, PostDO.class); postMapper.updateById(updateObj); } diff --git a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/dept/PostServiceImplTest.java b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/dept/PostServiceImplTest.java index 6e497585b7..6d117fddc4 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/dept/PostServiceImplTest.java +++ b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/dept/PostServiceImplTest.java @@ -4,10 +4,8 @@ import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; -import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostReqVO; -import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostExportReqVO; import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostPageReqVO; -import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostUpdateReqVO; +import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostSaveReqVO; import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO; import cn.iocoder.yudao.module.system.dal.mysql.dept.PostMapper; import org.junit.jupiter.api.Test; @@ -44,7 +42,7 @@ public class PostServiceImplTest extends BaseDbUnitTest { @Test public void testCreatePost_success() { // 准备参数 - PostReqVO reqVO = randomPojo(PostReqVO.class, + PostSaveReqVO reqVO = randomPojo(PostSaveReqVO.class, o -> o.setStatus(randomEle(CommonStatusEnum.values()).getStatus())); // 调用 Long postId = postService.createPost(reqVO); @@ -62,7 +60,7 @@ public class PostServiceImplTest extends BaseDbUnitTest { PostDO postDO = randomPostDO(); postMapper.insert(postDO);// @Sql: 先插入出一条存在的数据 // 准备参数 - PostUpdateReqVO reqVO = randomPojo(PostUpdateReqVO.class, o -> { + PostSaveReqVO reqVO = randomPojo(PostSaveReqVO.class, o -> { // 设置更新的 ID o.setId(postDO.getId()); o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()); @@ -103,7 +101,7 @@ public class PostServiceImplTest extends BaseDbUnitTest { PostDO postDO = randomPostDO(); postMapper.insert(postDO);// @Sql: 先插入出一条存在的数据 // 准备参数 - PostReqVO reqVO = randomPojo(PostReqVO.class, + PostSaveReqVO reqVO = randomPojo(PostSaveReqVO.class, // 模拟 name 重复 o -> o.setName(postDO.getName())); assertServiceException(() -> postService.createPost(reqVO), POST_NAME_DUPLICATE); @@ -118,7 +116,7 @@ public class PostServiceImplTest extends BaseDbUnitTest { PostDO codePostDO = randomPostDO(); postMapper.insert(codePostDO); // 准备参数 - PostUpdateReqVO reqVO = randomPojo(PostUpdateReqVO.class, o -> { + PostSaveReqVO reqVO = randomPojo(PostSaveReqVO.class, o -> { // 设置更新的 ID o.setId(postDO.getId()); // 模拟 code 重复 @@ -154,30 +152,6 @@ public class PostServiceImplTest extends BaseDbUnitTest { assertPojoEquals(postDO, pageResult.getList().get(0)); } - @Test - public void testGetPostList_export() { - // mock 数据 - PostDO postDO = randomPojo(PostDO.class, o -> { - o.setName("码仔"); - o.setStatus(CommonStatusEnum.ENABLE.getStatus()); - }); - postMapper.insert(postDO); - // 测试 name 不匹配 - postMapper.insert(cloneIgnoreId(postDO, o -> o.setName("程序员"))); - // 测试 status 不匹配 - postMapper.insert(cloneIgnoreId(postDO, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); - // 准备参数 - PostExportReqVO reqVO = new PostExportReqVO(); - reqVO.setName("码"); - reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); - - // 调用 - List list = postService.getPostList(reqVO); - // 断言 - assertEquals(1, list.size()); - assertPojoEquals(postDO, list.get(0)); - } - @Test public void testGetPostList() { // mock 数据