我正在努力将字符串对象从source(Relation.class)映射到目标列表(RelationListDTO.class)。
Relation.java
public class Relation {
private String name;
private String email;
private String completeAddress;
// getters and setters
}
RelationListDTO.java
public class RelationListDTO {
private String name;
private String email;
private List<Address> address;
// getters and setters
}
Address.java
public class Address{
private String street;
private String city;
// getters and setters
}
映射器类
@Mapper
public interface RelationMapper {
@Mapping(source = "completeAddress", target = "address.get(0).city")
RelationListDTO relationToListDto(Relation relation);
}
但是它不起作用。谁能帮忙。
答案 0 :(得分:0)
无法使用MapStruct进行操作。因为MapStruct不适用于运行时对象。 MapStruct仅生成用于映射两个bean的纯Java代码。而且我发现您的要求很少。您有一个地址列表,但只想映射源对象中的城市?您仍然可以这样做
DECLARE @tbl TABLE(SomeData VARCHAR(100));
INSERT INTO @tbl VALUES('A');
WITH cte AS
(
select SomeData as SzamlatetelId
from @tbl K
where K.SomeData = 'B' --try this with 'A' to get a result
)
--the query will use the cte instead of the actual query
select
(select *
from cte
for xml raw('Kiertekeles'), TYPE)
--and there will be no result, if the cte has no rows
WHERE (SELECT COUNT(*) FROM cte)>0
for xml raw('Kiertekelesek'), ELEMENTS, TYPE
答案 1 :(得分:0)
不确定在接受答案时这是否可行,但我和您遇到了同样的问题,最终还是这样做了。
@Mapper(imports = Collections.class)
public interface RelationMapper {
@Mapping(expression = "java(Collections.singletonList(relation.getCompleteAddress()))", target = "address")
RelationListDTO relationToListDto(Relation relation);
}