假设我有这样的映射:
@Mapping(source = "parentId", target = "parent.id")
Child map(ChildDto dto, Parent parent);
Run Code Online (Sandbox Code Playgroud)
现在我需要将 ChildDto 列表映射到 Child 列表,但它们都具有相同的父级。我希望做这样的事情:
List<Child> map(List<ChildDto> dtoList, Parent parent);
Run Code Online (Sandbox Code Playgroud)
但它不起作用。有机会做到吗?
我是 Spring Boot 和MapStruct Tool的新手。
早些时候,一个项目(由其他团队使用这些技术编写)没有启动。然后,我在 Mapper Abstract Class 中做了一些更改,但现在 mapper 对象在应用程序启动时变为 null。
映射器抽象类:
@Mapper(componentModel = "spring")
public abstract class UserAndEmployeeMapper {
public UserAndEmployeeMapper INSTANCE = Mappers.getMapper( UserAndEmployeeMapper.class );
@Mapping(source = "username", target = "name")
@Mapping(source = "ssn", target = "ssn", defaultValue = "xxxxxx" )
@Mapping(target = "salary", constant = "34.67")
@Mapping(target = "dob", dateFormat = "dd/MM/yyyy", constant = "10/12/2002")
public abstract Employee mapToEmployee(User user);
public abstract List<Employee> mapToEmployee(List<User> users);
@Mapping(source = "name", target = "username")
public abstract User …Run Code Online (Sandbox Code Playgroud) 我最近在我的项目中添加了 mapStruct。这个框架很酷,但我想不出一件事。
这是我的情况:我有类型的Profile实体和字段Person。我想使用ProfileDto. 我正在void fromDto(ProfileDto dto, @MappingTarget Profile entity)为此使用方法。问题是映射器总是创建新的人而不是使用profile实体中的人
我的实体是:
public class Profile {
private Person person;
.. setters, getters and constructors
}
public class Person extends AbstractEntity {
private String name;
private String surname;
.. setters, getters and constructors
}
Run Code Online (Sandbox Code Playgroud)
到
public class ProfileDto extends AbstractDto {
private String name;
private String surname;
.. setters, getters and constructors
}
Run Code Online (Sandbox Code Playgroud)
我的映射器
public abstract class ProfileMapper {
@Mappings({
@Mapping(target …Run Code Online (Sandbox Code Playgroud) 我有2个实体:
实体1:
public class Master {
private int id;
private Set<SubMaster> subMasters= new HashSet<SubMaster>(0);
}
public class SubMaster{
private int subId;
private String subName;
}
Run Code Online (Sandbox Code Playgroud)
实体2:
public class MasterDTO {
private int id;
private Set<SubMaster> subMasters= new HashSet<SubMaster>(0);
}
public class SubMasterDTO{
private int subId;
private String subName;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用MapStruct映射器将POJO的值映射到另一个。
public interface MasterMapper{
MasterDTO toDto(Master entity);
}
Run Code Online (Sandbox Code Playgroud)
我能够成功映射Master到MasterDTO。但是,SubMasterin 的嵌套集合Master未映射到in中的对应集合MasterDTO。
谁能在正确的方向帮助我?
我有一个列表List<Payment>,我想将其映射到另一个列表List<PaymentPlan>。这些类型如下所示:
public class Payment {
@XmlElement(name = "Installment")
@JsonProperty("Installment")
private List<Installment> installments = new ArrayList<>();
@XmlElement(name = "OriginalAmount")
@JsonProperty("OriginalAmount")
private BigDecimal originalAmount;
//getters setters, more attributes
}
Run Code Online (Sandbox Code Playgroud)
和....
public class PaymentPlan {
//(Installment in different package)
private List<Installment> installments;
@XmlElement(name = "OriginalAmount")
@JsonProperty("OriginalAmount")
private BigDecimal originalAmount;
//getters setters, more attributes
}
Run Code Online (Sandbox Code Playgroud)
我希望这样的事情正在起作用......
@Mappings({
@Mapping(//other mappings...),
@Mapping(source = "payments", target = "paymentInformation.paymentPlans")
})
ResultResponse originalResponseToResultResponse(OrigResponse originalResponse);
Run Code Online (Sandbox Code Playgroud)
...但我得到:
Can't map property java.util.List<Payment> to java.util.List<PaymentPlan>.
Consider to declare/implement a …Run Code Online (Sandbox Code Playgroud) 嗨我在使用mapstruct从Child Source类设置它时,在DTO中为List操作获取null.有人可以帮我解决这个问题.请在这里找到我的代码
实体类:
public class Source {
int id;
String name;
List<ChildSource> childSource;
//getters and setters
}
public class ChildSource {
String code;
String action;
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
DestinationDTO:
public class TargetDTO{
int sNo;
String mName;
List<String> actions;
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
MApper类:
@Mapper(componentModel = "spring")
public abstract class SampleMapper {
@Mappings({
@Mapping(target = "id", source = "sno"),
@Mapping(target = "name", source = "mNAme")
})
public abstract TargetDTO toDto(Source source);
@IterableMapping(elementTargetType = String.class)
protected abstract List<String> mapStringtoList(List<ChildSource> childSource);
protected …Run Code Online (Sandbox Code Playgroud) 假设我有以下对象:
class Person {
String firstName;
String lastName;
}
class PersonBLO {
Person person;
Integer foo; // Some calculated business property
}
class PersonDTO {
String firstName;
String lastName;
Integer foo;
}
Run Code Online (Sandbox Code Playgroud)
我发现自己正在编写以下映射器:
@Mapping(target = "firstName", source = "person.firstName")
@Mapping(target = "lastName", source = "person.lastName")
PersonDTO personBLOToPersonDTO(PersonBLO personBLO);
Run Code Online (Sandbox Code Playgroud)
是否可以自动将所有person.*属性映射到相应的*属性?
使用MapStruct。举个例子:
class Dto {
DtoA a;
DtoB b;
}
class DtoA {
Long id;
//...
}
class DtoB {
Long id;
//...
}
class Entity {
AB ab;
}
Run Code Online (Sandbox Code Playgroud)
如何映射DtoA,并DtoB以AB?
我试过了:
public abstract Entity toEntity(Dto dto);
@Mappings({
@Mapping(source = "a", target = "ab.a"),
@Mapping(source = "b", target = "ab.b")
)}
public abstract AB toABEntity(DtoA a, DtoB b);
Run Code Online (Sandbox Code Playgroud)
已经生成了toABEntity足够的代码*,但未调用该方法。
*很糟糕,因为它首先设置a,然后设置b会创建的新实例ab,因此a会丢失。
我愿意在一些官方项目中使用 MapStruct,所以我决定先对其进行一些测试;我需要让它与 eclipse 集成工作,并遵循 MapStruct 网站上提供的所有说明,但是......到目前为止还没有运气。有没有人成功地进行了这种整合?如果是的话,我会错过什么?
我的测试从更大的东西开始,但是当我意识到它不起作用时,我决定使用一个更小的例子,所以我这样做了:
...无法说服 eclipse 自动生成映射器实现,我什至在 pom.xml 文件中添加了 jdt_apt 行。
这是 pom.xml 的一个片段 - 请参阅他的 mapstruct-clone 项目以获取整个代码。
<properties>
<org.mapstruct.version>1.3.0.Final</org.mapstruct.version>
<m2e.apt.activation>jdt_apt</m2e.apt.activation>
</properties>
Run Code Online (Sandbox Code Playgroud)
预期结果是:
重要编辑:我也在使用 lombok javaagent
我有一个带有默认值的 kotlin 数据类,当我尝试使用 MapStruct 映射它时,它会在运行时抛出错误,因为它会尝试将 null 值分配给具有默认值的属性的不可空类型。我知道在 @Mapping 注释中分配默认值,但是 MapStruct 有没有办法考虑数据类默认值,而不是我必须这样做两次?
这是例子:
data class A(val property1: String = "prop 1", val property2: String)
data class B(val property2: String)
@Mapper
interface SomeMapper {
...
fun mapBtoA(b: B): A
}
val b = B("prop 2 val")
val a: A = SomeMapper.INSTANCE.mapBtoA(b)
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,它将尝试将 null 分配给 property1 而不是 prop 1 默认值。
mapstruct ×10
java ×7
dto ×2
collections ×1
data-class ×1
eclipse ×1
entity ×1
kotlin ×1
lombok ×1
mapping ×1
spring-boot ×1