我有两个模型和一个映射器,
public class Form {
private int x1;
private int x2;
private int x3;
private int x4;
// Constructor and getters setters ommited.
}
public class Domain {
private int x1;
private int x2;
private int x3;
private int x4;
// Constructor and getters setters ommited.
}
@Mapper
public interface DomainMapper {
@Mappings({
@Mapping(target = "x1", ignore = true),
@Mapping(target = "x2", ignore = true)})
Domain toDomain(Form form);
}
这是我的例子,
// I create a form object.
Form form = new Form();
form.setX1(1);
form.setX2(2);
form.setX3(3);
form.setX4(4);
// I create a Domain object.
Domain domain = new Domain();
domain.setX1(100);
domain.setX2(200);
// Map the form to domain.
domain = domainMapper.toDomain(form);
System.out.print(domain.getX1()); // => shows "0" instead of "100"
MapStruct 正在生成源代码,其中创建了一个新的 Domain 对象。
运行最后一个命令domain = DomainMapper.toDomain(form);
我以前的域对象将丢失。有没有办法使用 MapStruct 将属性从 Form 对象传输到我已经创建的 Domain 对象?
我试过这样的东西,但它不起作用,
@Mapper
public interface DomainMapper {
@Mappings({
@Mapping(target = "x1", ignore = true),
@Mapping(target = "x2", ignore = true)})
Domain toDomain(Form form, Domain referenceToExistingObject);
}
我是这样用的,但是不行
domainMapper.toDomain(form, domain);