首页 > 其他分享 >MapperStruct 嵌套模型中 List<> 转 List<String>

MapperStruct 嵌套模型中 List<> 转 List<String>

时间:2024-07-26 15:44:07浏览次数:7  
标签:String List MapperStruct private Source import public

废话不多说,上代码

 

宗旨:将List<A> 映射为 List<String>

一,实体类

Source

//Source中有一个List<A>
public class Source{
    private String id;
    private String from;
    private List<A> to;
 
}
 
//A对象中有个B
public class A{
    private B b;
}
 
//B有个address属性
public class B{
   private String address;
}

Target

public class Target {
    private String id;
    private String from;
    private List<String> to;
}

 

二,Mapper映射接口

 
import com.microsoft.graph.models.Message;
import com.microsoft.graph.models.Recipient;
import io.github.linpeilie.BaseMapper;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;
 
import javax.xml.transform.Source;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @author: Tyler
 * @create: 2024-07-26
 */
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING, unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface MyMapper {
    MyMapper INSTANCE = Mappers.getMapper(MyMapper.class);
 
    @Mappings({
        @Mapping(source = "id", target = "id"),
        @Mapping(source = "from", target = "from"),
        @Mapping(source = "toRecipients", target = "to"),
    })
        //实体转换
    Target convert(Source entity);
 
    //List转换
    List<MyMapper> convertList(List<Source> entitys);
 
    //自定义 List<A> 映射 List<String>
    default List<String> convertToString(List<A> entitys) {
        List<String> list=entitys.stream().map(x->x.getB().getAddress()).collect(Collectors.toList());;
        return list;
    }
 
}

 

三,调用

List<Target> list = MyMapper.INSTANCE.convertList(List<Source>);

 

标签:String,List,MapperStruct,private,Source,import,public
From: https://www.cnblogs.com/hanjun0612/p/18325484

相关文章

  • C++ primer plus 第16章string 类和标准模板库, 函数符概念
    C++primerplus第16章string类和标准模板库,函数符概念C++primerplus第16章string类和标准模板库,函数符概念文章目录C++primerplus第16章string类和标准模板库,函数符概念16.5.1函数符概念程序清单16.15functor.cpp16.5.1函数符概念正如STL定......
  • C++ primer plus 第16章string 类和标准模板库, 函数对象
    C++primerplus第16章string类和标准模板库,函数对象C++primerplus第16章string类和标准模板库,函数对象文章目录C++primerplus第16章string类和标准模板库,函数对象16.5函数对象16.5函数对象很多STL算法都使用函数对象–也叫函数符(fiunctor)。......
  • 当你第一次用C++string的assign会遇到这种情况
    当你第一次用string的assign时,会发现有一点小区别,见以下代码:stringstr1;str1.assign("helloC++");cout<<str1<<endl;stringstr2;str2.assign(str1,5);cout<<str1<<endl;stringstr3;str3.assign("helloC++",5);cout<<......
  • python-myStudyList
     1  下载软件1.1下载python最新版本并安装下载地址:百度搜索python官网。WelcometoPython.org。 1.2官网学习网页:PythonTutorials–RealPython   1.3也可以下载集成环境软件Anaconda。 Anaconda软件商城官方正版免费下载(msc23.cn) 2 ......
  • JAVA初级之集合(List集合)
    目录1、数组和集合的区别2、集合的体系结构3、collection集合3.1collection集合的概述 3.2 Collection集合常用方法 3.3 Collection的增删查实现3.3.1增加3.3.2移除功能3.3.3查找功能4、List集合4.1List集合的概述和特点4.2List集合的常用方法 4.3常......
  • List<T> HashSet<T> ConcurrentBag<T> 通常会在什么场景下使用 性能对比 .container
    List<T>,HashSet<T>,和ConcurrentBag<T>是.NET中常用的集合类型,它们在不同的场景下各有优势。下面我们来详细介绍它们的使用场景、性能比较以及.Contains()方法的性能。ListList<T>是一个动态数组,提供了顺序访问和按索引访问的能力。使用场景:需要维护元素的顺序。......
  • Java筛选数据:List的contains和Map的get哪个快?
    在Java中,List的contains方法和Map的get方法在性能上有一些区别,主要取决于数据结构的特性和使用场景:List的contains方法:List是一个有序集合,使用线性查找来确定列表中是否包含某个元素。时间复杂度为O(n),其中n是列表的大小。对于小型的List或者在列表中的......
  • 沃尔玛拉取listing和库存
    CREATETABLE`yibai_walmart_listing_task`(`id`int(11)unsignedNOTNULLAUTO_INCREMENTCOMMENT'ID',`account_id`int(11)NOTNULLDEFAULT'0'COMMENT'账户ID',`account_name`varchar(50)NOTNULLDEFAULT''......
  • 【网站开发系列】SDL checklist
    前言SDL的前期阶段包含了需求分析和设计阶段,这个阶段有不少沟通工作,项目经理、产品经理甚至需求方都可能会涉及,在梳理各开发条线的项目情况后,需要给出相应的建议。在这里,一份Checklist可能会很有帮助。美的金融科技就发布了一个金融科技SDL安全设计Checklist,内容涵盖输入验证、......
  • HarmonyOS:组件Navigation使用中List显示不全的问题探究以及解决办法
    1.线性布局中在使用NavPathStack布局中中发现如果使用List组件会发现item显示不全,在使用官方提供的例子中也发现此问题。如图所示:底部被遮挡,官方示例的写法2.如果线性布局中,不显示导航栏,也会出现List被遮挡的问题如图所示:我们的页面布局中的List缺失一部分根据Previ......