栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

优雅的使用BeanUtils对List集合的操作

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

优雅的使用BeanUtils对List集合的操作

摘要

我们在Entity、Bo、Vo层数据间可能经常转换数据,Entity对应的是持久层数据结构(一般是数据库表的映射模型)、Bo对应的是业务层操作的数据结构、Vo就是Controller和客户端交互的数据结构。在这些数据结构之间很大一部分属性都可能会相同,我们在使用的时候会不断的重新赋值。
如:客户端传输管理员信息的到Web层,我们会使用AdminVo接收,但是到了Service层时,我就需要使用AdminBo,这时候就需要把AdminVo实例的属性一个一个赋值到AdminBo实例中。

BeanUtils

Spring 提供了 org.springframework.beans.BeanUtils 类进行快速赋值,如:
AdminEntity类

public class AdminEntity{
    private Integer id;
    
    private String password;

    private String username;

    private String userImg;
    .... //一些 Set Get方法
}

AdminVo类,因为是和客户端打交道的,所以password属性就不适合在这里了

public class AdminVo{
    private Integer id;

    private String username;

    private String userImg;
    .... //一些 Set Get方法
}

假如我们需要把AdminEntity实例属性值赋值到AdminVo实例中(暂时忽略Bo层吧)

    AdminEntity entity = ...;
    AdminVo vo = new AdminEntity();
    // org.springframework.beans.BeanUtils
    BeanUtils.copyProperties(entity, vo); // 赋值

那么这样AdminVo实例中的属性值就和AdminEntity实例中的属性值一致了。
但是如果我们是一个集合的时候就不能这样直接赋值了。如:

 List adminList = ...;
 List adminVoList = new ArrayList<>(adminList.size());
 BeanUtils.copyProperties(adminList, adminVoList); // 赋值失败

这样直接赋值是不可取的,由方法名(copyProperties)可知,只会复制他们的属性值,那么上述的adminList属性和adminVoList的属性是没有半毛钱关系的。那么怎么解决了?

方式一(暴力解决,不推荐)

代码如下:

 List adminList = ...;
 List adminVoList = new ArrayList<>(adminList.size());
 for (Admin admin : adminList) {
     AdminVo adminVo = new AdminVo();
     BeanUtils.copyProperties(admin, adminVo);
     adminVoList.add(adminVo);
 }
	return adminVoList;

虽然for循环可以解决,但是一点都不优雅,这样的代码也会陆续增多,代码多了,就会看出来了重复的地方,没错!就是for循环赋值的地方,完全可以优化掉(代码写多了一眼就看出来了)。那么请看优雅的方式二

方式二 (优雅、推荐)

这也是我第一次写泛型的代码,可能有待提高,如下:
ColaBeanUtils类(Cola是我家的狗狗名,哈哈)

import org.springframework.beans.BeanUtils;

public class ColaBeanUtils extends BeanUtils {

    public static  List copyListProperties(List sources, Supplier target) {
 return copyListProperties(sources, target, null);
    }

    
    public static  List copyListProperties(List sources, Supplier target, ColaBeanUtilsCallBack callBack) {
 List list = new ArrayList<>(sources.size());
 for (S source : sources) {
     T t = target.get();
     copyProperties(source, t);
     list.add(t);
     if (callBack != null) {
 		// 回调
  callBack.callBack(source, t);
     }
 }
 return list;
    }

ColaBeanUtilsCallBack接口,使用java8的lambda表达式注解:

@FunctionalInterface
public interface ColaBeanUtilsCallBack {

    void callBack(S t, T s);
}

使用方式如下:

	List adminList = ...;
 return ColaBeanUtils.copyListProperties(adminList, AdminVo::new);

如果需要在循环中做处理(回调),那么可使用lambda表达式:

 List
adminEntityList = articleMapper.getAllArticle(); List articleVoList = ColaBeanUtils.copyListProperties(adminEntityList , ArticleVo::new, (articleEntity, articleVo) -> { // 回调处理 }); return articleVoList;

简直不要太简单了!!!

总结

AdminVo::new配合Supplier target这里我完全是参考《Java核心技术第十版 卷一》的第八章泛型,因为之前看过有点印象,没想到今天用到了。@FunctionalInterface这个是java8的lambda表达式的注解类,参考java.util.function.Consumer接口。没想到懵懵懂懂的,就把之前看过的知识写出来了,惊呆了,哈哈。
代码如果雷同,纯属巧合。若泛型的代码还有可以改进的地方,可在下方留言,非常感谢,Thanks♪(・ω・)ノ。

个人博客网址: https://colablog.cn/

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号