Commit 5e6e02e4f4900d5be2415e5875905475c98c7466
1 parent
ddb5bce0
vo 简化:使用 BeanUtils 替代 mapstruct 转换
Showing
7 changed files
with
69 additions
and
78 deletions
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/object/BeanUtils.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.common.util.object; | |
| 2 | + | |
| 3 | +import cn.hutool.core.bean.BeanUtil; | |
| 4 | +import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 5 | +import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | |
| 6 | + | |
| 7 | +import java.util.List; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * Bean 工具类 | |
| 11 | + * | |
| 12 | + * 1. 默认使用 {@link cn.hutool.core.bean.BeanUtil} 作为实现类,虽然不同 bean 工具的性能有差别,但是对绝大多数同学的项目,不用在意这点性能 | |
| 13 | + * 2. 针对复杂的对象转换,可以搜参考 AuthConvert 实现,通过 mapstruct + default 配合实现 | |
| 14 | + * | |
| 15 | + * @author 芋道源码 | |
| 16 | + */ | |
| 17 | +public class BeanUtils { | |
| 18 | + | |
| 19 | + public static <T> T toBean(Object source, Class<T> targetClass) { | |
| 20 | + return BeanUtil.toBean(source, targetClass); | |
| 21 | + } | |
| 22 | + | |
| 23 | + public static <S, T> List<T> toBean(List<S> source, Class<T> targetType) { | |
| 24 | + if (source == null) { | |
| 25 | + return null; | |
| 26 | + } | |
| 27 | + return CollectionUtils.convertList(source, s -> toBean(s, targetType)); | |
| 28 | + } | |
| 29 | + | |
| 30 | + public static <S, T> PageResult<T> toBean(PageResult<S> source, Class<T> targetType) { | |
| 31 | + if (source == null) { | |
| 32 | + return null; | |
| 33 | + } | |
| 34 | + return new PageResult<>(toBean(source.getList(), targetType), source.getTotal()); | |
| 35 | + } | |
| 36 | + | |
| 37 | +} | |
| 0 | 38 | \ No newline at end of file | ... | ... |
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; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 5 | 5 | import cn.iocoder.yudao.framework.common.pojo.PageParam; |
| 6 | 6 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 7 | +import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | |
| 7 | 8 | import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; |
| 8 | 9 | import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; |
| 9 | -import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.*; | |
| 10 | -import cn.iocoder.yudao.module.system.convert.dept.PostConvert; | |
| 10 | +import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostPageReqVO; | |
| 11 | +import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostSaveReqVO; | |
| 12 | +import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostRespVO; | |
| 11 | 13 | import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO; |
| 12 | 14 | import cn.iocoder.yudao.module.system.service.dept.PostService; |
| 13 | -import io.swagger.v3.oas.annotations.tags.Tag; | |
| 14 | -import io.swagger.v3.oas.annotations.Parameter; | |
| 15 | 15 | import io.swagger.v3.oas.annotations.Operation; |
| 16 | +import io.swagger.v3.oas.annotations.Parameter; | |
| 17 | +import io.swagger.v3.oas.annotations.tags.Tag; | |
| 16 | 18 | import org.springframework.security.access.prepost.PreAuthorize; |
| 17 | 19 | import org.springframework.validation.annotation.Validated; |
| 18 | 20 | import org.springframework.web.bind.annotation.*; |
| ... | ... | @@ -40,7 +42,7 @@ public class PostController { |
| 40 | 42 | @PostMapping("/create") |
| 41 | 43 | @Operation(summary = "创建岗位") |
| 42 | 44 | @PreAuthorize("@ss.hasPermission('system:post:create')") |
| 43 | - public CommonResult<Long> createPost(@Valid @RequestBody PostReqVO createReqVO) { | |
| 45 | + public CommonResult<Long> createPost(@Valid @RequestBody PostSaveReqVO createReqVO) { | |
| 44 | 46 | Long postId = postService.createPost(createReqVO); |
| 45 | 47 | return success(postId); |
| 46 | 48 | } |
| ... | ... | @@ -48,7 +50,7 @@ public class PostController { |
| 48 | 50 | @PutMapping("/update") |
| 49 | 51 | @Operation(summary = "修改岗位") |
| 50 | 52 | @PreAuthorize("@ss.hasPermission('system:post:update')") |
| 51 | - public CommonResult<Boolean> updatePost(@Valid @RequestBody PostReqVO updateReqVO) { | |
| 53 | + public CommonResult<Boolean> updatePost(@Valid @RequestBody PostSaveReqVO updateReqVO) { | |
| 52 | 54 | postService.updatePost(updateReqVO); |
| 53 | 55 | return success(true); |
| 54 | 56 | } |
| ... | ... | @@ -66,7 +68,8 @@ public class PostController { |
| 66 | 68 | @Parameter(name = "id", description = "岗位编号", required = true, example = "1024") |
| 67 | 69 | @PreAuthorize("@ss.hasPermission('system:post:query')") |
| 68 | 70 | public CommonResult<PostRespVO> getPost(@RequestParam("id") Long id) { |
| 69 | - return success(PostConvert.INSTANCE.convert(postService.getPost(id))); | |
| 71 | + PostDO post = postService.getPost(id); | |
| 72 | + return success(BeanUtils.toBean(post, PostRespVO.class)); | |
| 70 | 73 | } |
| 71 | 74 | |
| 72 | 75 | @GetMapping("/list-all-simple") |
| ... | ... | @@ -76,14 +79,15 @@ public class PostController { |
| 76 | 79 | List<PostDO> list = postService.getPostList(null, Collections.singleton(CommonStatusEnum.ENABLE.getStatus())); |
| 77 | 80 | // 排序后,返回给前端 |
| 78 | 81 | list.sort(Comparator.comparing(PostDO::getSort)); |
| 79 | - return success(PostConvert.INSTANCE.convertList(list)); | |
| 82 | + return success(BeanUtils.toBean(list, PostRespVO.class)); | |
| 80 | 83 | } |
| 81 | 84 | |
| 82 | 85 | @GetMapping("/page") |
| 83 | 86 | @Operation(summary = "获得岗位分页列表") |
| 84 | 87 | @PreAuthorize("@ss.hasPermission('system:post:query')") |
| 85 | - public CommonResult<PageResult<PostRespVO>> getPostPage(@Validated PostPageReqVO reqVO) { | |
| 86 | - return success(PostConvert.INSTANCE.convertPage(postService.getPostPage(reqVO))); | |
| 88 | + public CommonResult<PageResult<PostRespVO>> getPostPage(@Validated PostPageReqVO pageReqVO) { | |
| 89 | + PageResult<PostDO> pageResult = postService.getPostPage(pageReqVO); | |
| 90 | + return success(BeanUtils.toBean(pageResult, PostRespVO.class)); | |
| 87 | 91 | } |
| 88 | 92 | |
| 89 | 93 | @GetMapping("/export") |
| ... | ... | @@ -92,10 +96,10 @@ public class PostController { |
| 92 | 96 | @OperateLog(type = EXPORT) |
| 93 | 97 | public void export(HttpServletResponse response, @Validated PostPageReqVO reqVO) throws IOException { |
| 94 | 98 | reqVO.setPageSize(PageParam.PAGE_SIZE_NONE); |
| 95 | - PageResult<PostDO> pageResult = postService.getPostPage(reqVO); | |
| 99 | + List<PostDO> list = postService.getPostPage(reqVO).getList(); | |
| 96 | 100 | // 输出 |
| 97 | - PageResult<PostRespVO> list = PostConvert.INSTANCE.convertPage(pageResult); | |
| 98 | - ExcelUtils.write(response, "岗位数据.xls", "岗位列表", PostRespVO.class, list.getList()); | |
| 101 | + ExcelUtils.write(response, "岗位数据.xls", "岗位列表", PostRespVO.class, | |
| 102 | + BeanUtils.toBean(list, PostRespVO.class)); | |
| 99 | 103 | } |
| 100 | 104 | |
| 101 | 105 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/vo/post/PostReqVO.java renamed to yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/vo/post/PostSaveReqVO.java
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/convert/dept/PostConvert.java deleted
| 1 | -package cn.iocoder.yudao.module.system.convert.dept; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 4 | -import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.*; | |
| 5 | -import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO; | |
| 6 | -import org.mapstruct.Mapper; | |
| 7 | -import org.mapstruct.factory.Mappers; | |
| 8 | - | |
| 9 | -import java.util.List; | |
| 10 | - | |
| 11 | -@Mapper | |
| 12 | -public interface PostConvert { | |
| 13 | - | |
| 14 | - PostConvert INSTANCE = Mappers.getMapper(PostConvert.class); | |
| 15 | - | |
| 16 | - List<PostRespVO> convertList(List<PostDO> list); | |
| 17 | - | |
| 18 | - PageResult<PostRespVO> convertPage(PageResult<PostDO> page); | |
| 19 | - | |
| 20 | - PostRespVO convert(PostDO bean); | |
| 21 | - | |
| 22 | - PostDO convert(PostReqVO bean); | |
| 23 | - | |
| 24 | -} |
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; |
| 3 | 3 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | 5 | import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostPageReqVO; |
| 6 | -import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostReqVO; | |
| 6 | +import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostSaveReqVO; | |
| 7 | 7 | import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO; |
| 8 | 8 | import org.springframework.lang.Nullable; |
| 9 | 9 | |
| ... | ... | @@ -25,14 +25,14 @@ public interface PostService { |
| 25 | 25 | * @param createReqVO 岗位信息 |
| 26 | 26 | * @return 岗位编号 |
| 27 | 27 | */ |
| 28 | - Long createPost(PostReqVO createReqVO); | |
| 28 | + Long createPost(PostSaveReqVO createReqVO); | |
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * 更新岗位 |
| 32 | 32 | * |
| 33 | 33 | * @param updateReqVO 岗位信息 |
| 34 | 34 | */ |
| 35 | - void updatePost(PostReqVO updateReqVO); | |
| 35 | + void updatePost(PostSaveReqVO updateReqVO); | |
| 36 | 36 | |
| 37 | 37 | /** |
| 38 | 38 | * 删除岗位信息 | ... | ... |
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; |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 5 | 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 6 | +import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | |
| 6 | 7 | import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostPageReqVO; |
| 7 | -import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostReqVO; | |
| 8 | -import cn.iocoder.yudao.module.system.convert.dept.PostConvert; | |
| 8 | +import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostSaveReqVO; | |
| 9 | 9 | import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO; |
| 10 | 10 | import cn.iocoder.yudao.module.system.dal.mysql.dept.PostMapper; |
| 11 | 11 | import org.springframework.stereotype.Service; |
| ... | ... | @@ -33,23 +33,23 @@ public class PostServiceImpl implements PostService { |
| 33 | 33 | private PostMapper postMapper; |
| 34 | 34 | |
| 35 | 35 | @Override |
| 36 | - public Long createPost(PostReqVO createReqVO) { | |
| 36 | + public Long createPost(PostSaveReqVO createReqVO) { | |
| 37 | 37 | // 校验正确性 |
| 38 | 38 | validatePostForCreateOrUpdate(null, createReqVO.getName(), createReqVO.getCode()); |
| 39 | 39 | |
| 40 | 40 | // 插入岗位 |
| 41 | - PostDO post = PostConvert.INSTANCE.convert(createReqVO); | |
| 41 | + PostDO post = BeanUtils.toBean(createReqVO, PostDO.class); | |
| 42 | 42 | postMapper.insert(post); |
| 43 | 43 | return post.getId(); |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | @Override |
| 47 | - public void updatePost(PostReqVO updateReqVO) { | |
| 47 | + public void updatePost(PostSaveReqVO updateReqVO) { | |
| 48 | 48 | // 校验正确性 |
| 49 | 49 | validatePostForCreateOrUpdate(updateReqVO.getId(), updateReqVO.getName(), updateReqVO.getCode()); |
| 50 | 50 | |
| 51 | 51 | // 更新岗位 |
| 52 | - PostDO updateObj = PostConvert.INSTANCE.convert(updateReqVO); | |
| 52 | + PostDO updateObj = BeanUtils.toBean(updateReqVO, PostDO.class); | |
| 53 | 53 | postMapper.updateById(updateObj); |
| 54 | 54 | } |
| 55 | 55 | ... | ... |
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; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | 5 | import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; |
| 6 | 6 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| 7 | -import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostReqVO; | |
| 8 | -import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostExportReqVO; | |
| 9 | 7 | import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostPageReqVO; |
| 10 | -import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostUpdateReqVO; | |
| 8 | +import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostSaveReqVO; | |
| 11 | 9 | import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO; |
| 12 | 10 | import cn.iocoder.yudao.module.system.dal.mysql.dept.PostMapper; |
| 13 | 11 | import org.junit.jupiter.api.Test; |
| ... | ... | @@ -44,7 +42,7 @@ public class PostServiceImplTest extends BaseDbUnitTest { |
| 44 | 42 | @Test |
| 45 | 43 | public void testCreatePost_success() { |
| 46 | 44 | // 准备参数 |
| 47 | - PostReqVO reqVO = randomPojo(PostReqVO.class, | |
| 45 | + PostSaveReqVO reqVO = randomPojo(PostSaveReqVO.class, | |
| 48 | 46 | o -> o.setStatus(randomEle(CommonStatusEnum.values()).getStatus())); |
| 49 | 47 | // 调用 |
| 50 | 48 | Long postId = postService.createPost(reqVO); |
| ... | ... | @@ -62,7 +60,7 @@ public class PostServiceImplTest extends BaseDbUnitTest { |
| 62 | 60 | PostDO postDO = randomPostDO(); |
| 63 | 61 | postMapper.insert(postDO);// @Sql: 先插入出一条存在的数据 |
| 64 | 62 | // 准备参数 |
| 65 | - PostUpdateReqVO reqVO = randomPojo(PostUpdateReqVO.class, o -> { | |
| 63 | + PostSaveReqVO reqVO = randomPojo(PostSaveReqVO.class, o -> { | |
| 66 | 64 | // 设置更新的 ID |
| 67 | 65 | o.setId(postDO.getId()); |
| 68 | 66 | o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()); |
| ... | ... | @@ -103,7 +101,7 @@ public class PostServiceImplTest extends BaseDbUnitTest { |
| 103 | 101 | PostDO postDO = randomPostDO(); |
| 104 | 102 | postMapper.insert(postDO);// @Sql: 先插入出一条存在的数据 |
| 105 | 103 | // 准备参数 |
| 106 | - PostReqVO reqVO = randomPojo(PostReqVO.class, | |
| 104 | + PostSaveReqVO reqVO = randomPojo(PostSaveReqVO.class, | |
| 107 | 105 | // 模拟 name 重复 |
| 108 | 106 | o -> o.setName(postDO.getName())); |
| 109 | 107 | assertServiceException(() -> postService.createPost(reqVO), POST_NAME_DUPLICATE); |
| ... | ... | @@ -118,7 +116,7 @@ public class PostServiceImplTest extends BaseDbUnitTest { |
| 118 | 116 | PostDO codePostDO = randomPostDO(); |
| 119 | 117 | postMapper.insert(codePostDO); |
| 120 | 118 | // 准备参数 |
| 121 | - PostUpdateReqVO reqVO = randomPojo(PostUpdateReqVO.class, o -> { | |
| 119 | + PostSaveReqVO reqVO = randomPojo(PostSaveReqVO.class, o -> { | |
| 122 | 120 | // 设置更新的 ID |
| 123 | 121 | o.setId(postDO.getId()); |
| 124 | 122 | // 模拟 code 重复 |
| ... | ... | @@ -155,30 +153,6 @@ public class PostServiceImplTest extends BaseDbUnitTest { |
| 155 | 153 | } |
| 156 | 154 | |
| 157 | 155 | @Test |
| 158 | - public void testGetPostList_export() { | |
| 159 | - // mock 数据 | |
| 160 | - PostDO postDO = randomPojo(PostDO.class, o -> { | |
| 161 | - o.setName("码仔"); | |
| 162 | - o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 163 | - }); | |
| 164 | - postMapper.insert(postDO); | |
| 165 | - // 测试 name 不匹配 | |
| 166 | - postMapper.insert(cloneIgnoreId(postDO, o -> o.setName("程序员"))); | |
| 167 | - // 测试 status 不匹配 | |
| 168 | - postMapper.insert(cloneIgnoreId(postDO, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 169 | - // 准备参数 | |
| 170 | - PostExportReqVO reqVO = new PostExportReqVO(); | |
| 171 | - reqVO.setName("码"); | |
| 172 | - reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 173 | - | |
| 174 | - // 调用 | |
| 175 | - List<PostDO> list = postService.getPostList(reqVO); | |
| 176 | - // 断言 | |
| 177 | - assertEquals(1, list.size()); | |
| 178 | - assertPojoEquals(postDO, list.get(0)); | |
| 179 | - } | |
| 180 | - | |
| 181 | - @Test | |
| 182 | 156 | public void testGetPostList() { |
| 183 | 157 | // mock 数据 |
| 184 | 158 | PostDO postDO01 = randomPojo(PostDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); | ... | ... |