废话不多说,上代码
宗旨:将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