Skip to content

Commit

Permalink
Merge pull request #184 from little3201/develop
Browse files Browse the repository at this point in the history
替换BeanUtils为BeanCopier
  • Loading branch information
little3201 authored Mar 18, 2024
2 parents 932d785 + 03ef2cb commit 2c3b439
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import io.leafage.basic.assets.repository.PostRepository;
import io.leafage.basic.assets.service.CategoryService;
import io.leafage.basic.assets.vo.CategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -75,21 +75,25 @@ public boolean exist(String name) {
}

@Override
public CategoryVO create(CategoryDTO categoryDTO) {
public CategoryVO create(CategoryDTO dto) {
Category category = new Category();
BeanUtils.copyProperties(categoryDTO, category);
BeanCopier copier = BeanCopier.create(CategoryDTO.class, Category.class, false);
copier.copy(dto, category, null);

category = categoryRepository.saveAndFlush(category);
return this.convertOuter(category);
}

@Override
public CategoryVO modify(Long id, CategoryDTO categoryDTO) {
public CategoryVO modify(Long id, CategoryDTO dto) {
Assert.notNull(id, "category id must not be null.");
Category category = categoryRepository.findById(id).orElse(null);
if (category == null) {
return null;
}
BeanUtils.copyProperties(categoryDTO, category);
BeanCopier copier = BeanCopier.create(CategoryDTO.class, Category.class, false);
copier.copy(dto, category, null);

category = categoryRepository.save(category);
return this.convertOuter(category);
}
Expand All @@ -104,14 +108,15 @@ public void remove(Long id) {
/**
* 对象转换为输出结果对象
*
* @param info 信息
* @param category 信息
* @return 输出转换后的vo对象
*/
private CategoryVO convertOuter(Category info) {
private CategoryVO convertOuter(Category category) {
CategoryVO vo = new CategoryVO();
BeanUtils.copyProperties(info, vo);
BeanCopier copier = BeanCopier.create(Category.class, CategoryVO.class, false);
copier.copy(category, vo, null);

long count = postRepository.countByCategoryId(info.getId());
long count = postRepository.countByCategoryId(category.getId());
vo.setCount(count);
return vo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import io.leafage.basic.assets.repository.PostStatisticsRepository;
import io.leafage.basic.assets.service.CommentService;
import io.leafage.basic.assets.vo.CommentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -68,9 +68,11 @@ public List<CommentVO> replies(Long replier) {

@Transactional(rollbackFor = Exception.class)
@Override
public CommentVO create(CommentDTO commentDTO) {
public CommentVO create(CommentDTO dto) {
Comment comment = new Comment();
BeanUtils.copyProperties(commentDTO, comment);
BeanCopier copier = BeanCopier.create(CommentDTO.class, Comment.class, false);
copier.copy(dto, comment, null);

comment = commentRepository.saveAndFlush(comment);
// 添加关联帖子的评论数
this.postStatisticsRepository.increaseComment(comment.getPostId());
Expand All @@ -85,7 +87,8 @@ public CommentVO create(CommentDTO commentDTO) {
*/
private CommentVO convertOuter(Comment comment) {
CommentVO vo = new CommentVO();
BeanUtils.copyProperties(comment, vo);
BeanCopier copier = BeanCopier.create(Comment.class, CommentVO.class, false);
copier.copy(comment, vo, null);

Long count = commentRepository.countByReplier(comment.getId());
vo.setCount(count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import io.leafage.basic.assets.repository.PostStatisticsRepository;
import io.leafage.basic.assets.service.PostStatisticsService;
import io.leafage.basic.assets.vo.PostStatisticsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
Expand All @@ -47,9 +47,11 @@ public Page<PostStatisticsVO> retrieve(int page, int size) {
}

@Override
public PostStatisticsVO create(PostStatisticsDTO postStatisticsDTO) {
public PostStatisticsVO create(PostStatisticsDTO dto) {
PostStatistics postStatistics = new PostStatistics();
BeanUtils.copyProperties(postStatisticsDTO, postStatistics);
BeanCopier copier = BeanCopier.create(PostStatisticsDTO.class, PostStatistics.class, false);
copier.copy(dto, postStatistics, null);

postStatistics = postStatisticsRepository.saveAndFlush(postStatistics);
return this.convertOuter(postStatistics);
}
Expand All @@ -62,7 +64,8 @@ public PostStatisticsVO create(PostStatisticsDTO postStatisticsDTO) {
*/
private PostStatisticsVO convertOuter(PostStatistics postStatistics) {
PostStatisticsVO vo = new PostStatisticsVO();
BeanUtils.copyProperties(postStatistics, vo);
BeanCopier copier = BeanCopier.create(PostStatistics.class, PostStatisticsVO.class, false);
copier.copy(postStatistics, vo, null);
return vo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import io.leafage.basic.assets.service.PostsService;
import io.leafage.basic.assets.vo.PostContentVO;
import io.leafage.basic.assets.vo.PostVO;
import org.springframework.beans.BeanUtils;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -90,7 +90,9 @@ public PostContentVO details(Long id) {

PostVO vo = this.convertOuter(post);
PostContentVO postsContentVO = new PostContentVO();
BeanUtils.copyProperties(vo, postsContentVO);
BeanCopier copier = BeanCopier.create(PostVO.class, PostContentVO.class, false);
copier.copy(vo, postsContentVO, null);

postsContentVO.setPostsId(post.getId());
// 获取内容详情
PostContent postContent = postContentRepository.getByPostId(id);
Expand Down Expand Up @@ -121,10 +123,12 @@ public boolean exist(String title) {

@Transactional(rollbackFor = Exception.class)
@Override
public PostVO create(PostDTO postDTO) {
public PostVO create(PostDTO dto) {
Post post = new Post();
BeanUtils.copyProperties(postDTO, post);
post.setTags(postDTO.getTags().toString());
BeanCopier copier = BeanCopier.create(PostDTO.class, Post.class, false);
copier.copy(dto, post, null);

post.setTags(dto.getTags().toString());
// 保存并立即刷盘
post = postRepository.saveAndFlush(post);
//保存帖子内容
Expand All @@ -133,30 +137,32 @@ public PostVO create(PostDTO postDTO) {
postContent = new PostContent();
}
postContent.setPostId(post.getId());
postContent.setContent(postDTO.getContent());
postContent.setContent(dto.getContent());
postContentRepository.saveAndFlush(postContent);
//转换结果
return this.convertOuter(post);
}

@Override
public PostVO modify(Long id, PostDTO postDTO) {
public PostVO modify(Long id, PostDTO dto) {
Assert.notNull(id, "post id must not be null.");
//查询基本信息
Post post = postRepository.findById(id).orElse(null);
if (post == null) {
return null;
}
BeanUtils.copyProperties(postDTO, post);
post.setTags(postDTO.getTags().toString());
BeanCopier copier = BeanCopier.create(PostDTO.class, Post.class, false);
copier.copy(dto, post, null);

post.setTags(dto.getTags().toString());

post = postRepository.save(post);
//保存文章内容
PostContent postContent = postContentRepository.getByPostId(id);
if (postContent == null) {
postContent = new PostContent();
}
postContent.setContent(postDTO.getContent());
postContent.setContent(dto.getContent());
postContentRepository.save(postContent);
//转换结果
return this.convertOuter(post);
Expand All @@ -177,7 +183,9 @@ public void remove(Long id) {
*/
private PostVO convertOuter(Post post) {
PostVO vo = new PostVO();
BeanUtils.copyProperties(post, vo);
BeanCopier copier = BeanCopier.create(Post.class, PostVO.class, false);
copier.copy(post, vo, null);

// 转换 tags
if (StringUtils.hasText(post.getTags())) {
String tags = post.getTags().substring(1, post.getTags().length() - 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import io.leafage.basic.hypervisor.repository.AccessLogRepository;
import io.leafage.basic.hypervisor.service.AccessLogService;
import io.leafage.basic.hypervisor.vo.AccessLogVO;
import org.springframework.beans.BeanUtils;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
Expand All @@ -47,16 +47,19 @@ public Page<AccessLogVO> retrieve(int page, int size) {
}

@Override
public AccessLogVO create(AccessLogDTO accessLogDTO) {
public AccessLogVO create(AccessLogDTO dto) {
AccessLog accessLog = new AccessLog();
BeanUtils.copyProperties(accessLogDTO, accessLog);
BeanCopier copier = BeanCopier.create(AccessLogDTO.class, AccessLog.class, false);
copier.copy(dto, accessLog, null);

accessLogRepository.saveAndFlush(accessLog);
return this.convert(accessLog);
}

private AccessLogVO convert(AccessLog accessLog) {
AccessLogVO vo = new AccessLogVO();
BeanUtils.copyProperties(accessLog, vo);
BeanCopier copier = BeanCopier.create(AccessLog.class, AccessLogVO.class, false);
copier.copy(accessLog, vo, null);
return vo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import io.leafage.basic.hypervisor.repository.DictionaryRepository;
import io.leafage.basic.hypervisor.service.DictionaryService;
import io.leafage.basic.hypervisor.vo.DictionaryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -74,9 +74,11 @@ public boolean exist(String name) {
}

@Override
public DictionaryVO create(DictionaryDTO dictionaryDTO) {
public DictionaryVO create(DictionaryDTO dto) {
Dictionary dictionary = new Dictionary();
BeanUtils.copyProperties(dictionaryDTO, dictionary);
BeanCopier copier = BeanCopier.create(DictionaryDTO.class, Dictionary.class, false);
copier.copy(dto, dictionary, null);

dictionaryRepository.saveAndFlush(dictionary);

return this.convert(dictionary);
Expand All @@ -90,7 +92,8 @@ public DictionaryVO create(DictionaryDTO dictionaryDTO) {
*/
private DictionaryVO convert(Dictionary dictionary) {
DictionaryVO vo = new DictionaryVO();
BeanUtils.copyProperties(dictionary, vo);
BeanCopier copier = BeanCopier.create(Dictionary.class, DictionaryVO.class, false);
copier.copy(dictionary, vo, null);
return vo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import io.leafage.basic.hypervisor.repository.GroupRepository;
import io.leafage.basic.hypervisor.service.GroupService;
import io.leafage.basic.hypervisor.vo.GroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -74,20 +74,25 @@ public List<TreeNode> tree() {
}

@Override
public GroupVO create(GroupDTO groupDTO) {
public GroupVO create(GroupDTO dto) {
Group group = new Group();
BeanUtils.copyProperties(groupDTO, group);
BeanCopier copier = BeanCopier.create(GroupDTO.class, Group.class, false);
copier.copy(dto, group, null);

group = groupRepository.saveAndFlush(group);
return this.convertOuter(group);
}

@Override
public GroupVO modify(Long id, GroupDTO groupDTO) {
public GroupVO modify(Long id, GroupDTO dto) {
Assert.notNull(id, "group id must not be null.");
Group group = groupRepository.findById(id).orElse(null);
if (group == null) {
throw new NoSuchElementException("当前操作数据不存在...");
}
BeanCopier copier = BeanCopier.create(GroupDTO.class, Group.class, false);
copier.copy(dto, group, null);

group = groupRepository.save(group);
return this.convertOuter(group);
}
Expand All @@ -105,11 +110,13 @@ public void remove(Long id) {
* @return 结果对象
*/
private GroupVO convertOuter(Group group) {
GroupVO groupVO = new GroupVO();
BeanUtils.copyProperties(group, groupVO);
GroupVO vo = new GroupVO();
BeanCopier copier = BeanCopier.create(Group.class, GroupVO.class, false);
copier.copy(group, vo, null);

long count = groupMembersRepository.countByGroupIdAndEnabledTrue(group.getId());
groupVO.setCount(count);
return groupVO;
vo.setCount(count);
return vo;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import io.leafage.basic.hypervisor.repository.MessageRepository;
import io.leafage.basic.hypervisor.service.MessageService;
import io.leafage.basic.hypervisor.vo.MessageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -58,9 +58,11 @@ public MessageVO fetch(Long id) {
}

@Override
public MessageVO create(MessageDTO messageDTO) {
public MessageVO create(MessageDTO dto) {
Message message = new Message();
BeanUtils.copyProperties(messageDTO, message);
BeanCopier copier = BeanCopier.create(MessageDTO.class, Message.class, false);
copier.copy(dto, message, null);

messageRepository.saveAndFlush(message);
return this.convertOuter(message);
}
Expand All @@ -71,8 +73,9 @@ public MessageVO create(MessageDTO messageDTO) {
* @return ExampleMatcher
*/
private MessageVO convertOuter(Message message) {
MessageVO messageVO = new MessageVO();
BeanUtils.copyProperties(message, messageVO);
return messageVO;
MessageVO vo = new MessageVO();
BeanCopier copier = BeanCopier.create(Message.class, MessageVO.class, false);
copier.copy(message, vo, null);
return vo;
}
}
Loading

0 comments on commit 2c3b439

Please sign in to comment.