地图结构。通过表达式映射列表元素的字段
MapStruct. Mapping fields of list element by expression
下午好!
有一个对象包含类型为 List 的字段,是否可以通过表达式参数在注释中生成的值来设置类型 T 的每个(某些)字段?
例如:
目标对象:
public class CustomList<T extends CustomEntity> extends CustomEntity {
private List<T> field;
public CustomList() {
field = new ArrayList();
}
}
映射器接口:
@Mapper
public interface Mapper {
@Mapping(target = "java(field.foreach(f -> f.getId))", expression = "java(UUID.randomUUID().toString())")
CustomList<SomeObject> map (Object object);
}
如何实现这样的想法?在文档中,我只找到了带有 1:1 映射的示例。
已编辑:
此外,我尝试使用这个:
public class IterableNonIntegrableUtil {
@SetElements
public CustomList<SomeObject> map(Object object) {
CustomList<SomeObject> customList= new CustomList<>();
customList.getField()
.forEach(item -> item.setUid(UUID.randomUUID().toString()));
return customList;
}}
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
public @interface SetElements{}
Mapper interface:
@Mapper(uses = IterableNonIntegrableUtil.class)
public interface Mapper {
@Mapping(target = "field", souce = "object",
qualifiedBy=SetElements.class)
CustomList<SomeObject> map (Object object);}
但在这种情况下,我对限定符有一些错误。
可以通过将对象映射到相同的类型:
CustomList<SomeObject> map(CustomList<SomeObject> src);
// in case you only want to set the ones that are null.
@Mapping(target = "someField1", default = "someValue")
@Mapping(target = "someField2", defaultExpression = "java(someMethod())")
// in case you always want to set the value.
@Mapping(target = "someField3", constant = "someValue")
@Mapping(target = "someField4", expression = "java(someMethod())")
SomeObject map(SomeObject src);
我自己会考虑在您构造对象时设置默认值。
例如:
class SomeObject {
private String someField;
/**
* When called with a null or a blank value, the default value will be restored.
*/
void setSomeField(String value) {
// there is probably an utility out there which can make this a one-liner.
if (value != null && !value.isBlank()){
someField = value;
} else {
someField = "defaultValue";
}
}
}
下午好! 有一个对象包含类型为 List 的字段,是否可以通过表达式参数在注释中生成的值来设置类型 T 的每个(某些)字段?
例如:
目标对象:
public class CustomList<T extends CustomEntity> extends CustomEntity {
private List<T> field;
public CustomList() {
field = new ArrayList();
}
}
映射器接口:
@Mapper
public interface Mapper {
@Mapping(target = "java(field.foreach(f -> f.getId))", expression = "java(UUID.randomUUID().toString())")
CustomList<SomeObject> map (Object object);
}
如何实现这样的想法?在文档中,我只找到了带有 1:1 映射的示例。
已编辑:
此外,我尝试使用这个:
public class IterableNonIntegrableUtil {
@SetElements
public CustomList<SomeObject> map(Object object) {
CustomList<SomeObject> customList= new CustomList<>();
customList.getField()
.forEach(item -> item.setUid(UUID.randomUUID().toString()));
return customList;
}}
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
public @interface SetElements{}
Mapper interface:
@Mapper(uses = IterableNonIntegrableUtil.class)
public interface Mapper {
@Mapping(target = "field", souce = "object",
qualifiedBy=SetElements.class)
CustomList<SomeObject> map (Object object);}
但在这种情况下,我对限定符有一些错误。
可以通过将对象映射到相同的类型:
CustomList<SomeObject> map(CustomList<SomeObject> src);
// in case you only want to set the ones that are null.
@Mapping(target = "someField1", default = "someValue")
@Mapping(target = "someField2", defaultExpression = "java(someMethod())")
// in case you always want to set the value.
@Mapping(target = "someField3", constant = "someValue")
@Mapping(target = "someField4", expression = "java(someMethod())")
SomeObject map(SomeObject src);
我自己会考虑在您构造对象时设置默认值。
例如:
class SomeObject {
private String someField;
/**
* When called with a null or a blank value, the default value will be restored.
*/
void setSomeField(String value) {
// there is probably an utility out there which can make this a one-liner.
if (value != null && !value.isBlank()){
someField = value;
} else {
someField = "defaultValue";
}
}
}