我有以下 DTO 和域对象。我正在使用 Mapstruct 将域对象复制到 DTO 对象。
public class AddressDomain {
private String street;
private Telephone telephone;
}
public class CompanyDomain{
private String id;
private Address address;
}
public class AddressDTO {
private String street;
private Telephone telephone;
}
public class CompanyDTO{
private String id;
private Address address;
}
Run Code Online (Sandbox Code Playgroud)
使用下面的映射器将域映射到 DTO。我不想将电话属性从域映射到 DTO。怎么做?我尝试在映射忽略中提供嵌套目标属性,但它给出了错误:
public interface CompanyMapper {
//**below line gives error**
@Mapping(target = "address.telephone", ignore=true)
CompanyDTO map(AddressDTO dto);
}
Run Code Online (Sandbox Code Playgroud) 我想在 DTO 的单项转换之后使用专用的 @AfterMapping 来应用装饰,并在处理其集合转换风格时使用另一个专用的 @AfterMapping,但不能同时使用两者。
public abstract CatUI convert(Cat cat);
public abstract List<CatUI> convert(List<Cat> cats);
@AfterMapping
public void populateCatName(Cat cat, @MappingTarget CatUI catUI) {
String name = _someRemoteService.getCatName(catUI.getId());
catUI.setName(name);
}
@AfterMapping
public void populateCatNames(List<Cat> cats, @MappingTarget List<CatUI> catUIs) {
Map<Integer,String> idToNameMap = _someRemoteService.getCatNames(catUIs.stream().map((c) -> c.getId() ).collect(Collectors.toList());
catUIs.forEach((c) -> c.setName(idToNameMap(c.getId())));
}
Run Code Online (Sandbox Code Playgroud)
我不希望在处理列表转换时调用 populateCatName ,从而重复我的装饰。
无论如何要这样做吗?
我正在使用 MapStruct 将值从源映射到目标类。两个类都应具有日期属性,但日期格式不同。如何在使用 MapStruct 映射属性时转换日期格式?
源类的日期格式:2018-05-18T18:43:33.623+0200
目标班级日期格式:2018-05-18
我正在努力将字符串对象从 source(Relation.class) 映射到目标列表(RelationListDTO.class) 。
关系.java
public class Relation {
private String name;
private String email;
private String completeAddress;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
关系列表DTO.java
public class RelationListDTO {
private String name;
private String email;
private List<Address> address;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
地址.java
public class Address{
private String street;
private String city;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
映射器类
@映射器
public interface RelationMapper {
@Mapping(source = "completeAddress", target = "address.get(0).city")
RelationListDTO relationToListDto(Relation relation);
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用。有人可以帮忙吗?
下面是 POJO:
public class TransferObjectListTO {
private List<A> transferList;
public List<A> getTransferList() {
return transferList;
}
public void setList(List<A> transferList) {
this.transferList= transferList;
}
public List<A> getList() {
return getTransferList();
}
public void incrementList(List<A> transferList) {
getTransferList().addAll(transferList);
}
}
Run Code Online (Sandbox Code Playgroud)
它有一种加法器方法 -
增量列表
以及复制到 getter 方法 -
获取列表
Mapstruct 生成的代码具有以下不必要的内容,其中添加了列表,再次添加了源类型:
if ( targetTypeTransferObjectListTO.getList() != null ) {
List sourceTypeList = sourceTypeTransferObjectListTO.getList();
if ( sourceTypeList != null ) {
targetTypeTransferObjectListTO.getList().addAll( sourceTypeList );
}
}
Run Code Online (Sandbox Code Playgroud)
incrementList() and getList()我们无法删除上面 POJO 中的这些方法,因为它在很多地方都使用了。现在,当mapstruct生成映射的实现时,我们如何才能忽略这些方法呢?
我的映射器的映射器装饰器未编译。映射器正在编译,但装饰器未编译。因为,在构建过程中,即使我在映射器装饰器中执行此操作,我也会遇到类型转换错误。还有什么要补充的吗?
映射器代码:
@Mapper
@DecoratedWith(OneMapperDecorator.class)
public interface OneMapper {
public TwoObject convertToTwoObject(OneObject one);
}
Run Code Online (Sandbox Code Playgroud)
装饰器代码:
public abstract class OneMapperDecorator implements OneMapper {
private final OneMapper delegate;
public OneMapperDecorator (OneMapper delegate) {
this.delegate = delegate;
}
@Override
public TwoObject convertToTwoObject(OneObject one)
{
TwoObject two=delegate.convertToTwoObject(one);
two.setTotalFare(new BigDecimal(one.getPrice()));//string to bigdecimal conversion
return two;
}
}
Run Code Online (Sandbox Code Playgroud) 我想将模型对象映射到 dto 模型。我已经有一个对象的映射器。如何在另一个类中的另一个映射器中重用这个映射器?
我有以下作为模型
@Getter
@AllArgsConstructor
@ToString
public class History {
@JsonProperty("identifier")
private final Identifier identifier;
@JsonProperty("submitTime")
private final ZonedDateTime submitTime;
@JsonProperty("method")
private final String method;
@JsonProperty("reason")
private final String reason;
@JsonProperty("dataList")
private final List<Data> dataList;
}
@DynamoDBTable(tableName = "history")
@Data
@NoArgsConstructor
public class HistoryDynamo {
@DynamoDBRangeKey(attributeName = "submitTime")
@DynamoDBTypeConverted(converter = ZonedDateTimeType.Converter.class)
private ZonedDateTime submitTime;
@DynamoDBAttribute(attributeName = "identifier")
@NonNull
private Identifier identifier;
@DynamoDBAttribute(attributeName = "method")
private String method;
@DynamoDBAttribute(attributeName = "reason")
private String reason;
@DynamoDBAttribute(attributeName = "dataList")
private List<Data> dataList; …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 MapStruct 在 dto 和实体对象之间进行映射转换,但是生成的映射器实现只返回空的映射对象。
BeerMapperImpl
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2020-11-05T07:42:21+0800",
comments = "version: 1.3.0.Final, compiler: javac, environment: Java 11 (Oracle Corporation)"
)
@Component
public class BeerMapperImpl implements BeerMapper {
@Override
public BeerDto beerToBeerDto(Beer beer) {
if ( beer == null ) {
return null;
}
BeerDto beerDto = new BeerDto();
return beerDto;
}
}
Run Code Online (Sandbox Code Playgroud)
下面是我的代码。
pom.xml
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.3.0.Final</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId> …Run Code Online (Sandbox Code Playgroud) 我有两节课。模型:
@Data
public class ExampleModel {
private String someField;
private String fieldInModel;
}
Run Code Online (Sandbox Code Playgroud)
和 DTO:
@Data
public class ExampleDto {
private String someField;
private String fieldInDto;
}
Run Code Online (Sandbox Code Playgroud)
我想配置简单的映射器(使用不同的字段名称):
@Mapper(componentModel = "spring")
public interface ExampleMapper {
@Mapping(target = "fieldInModel", source = "fieldInDto")
ExampleModel mapToExample(ExampleDto exampleDto);
}
Run Code Online (Sandbox Code Playgroud)
我的项目是一个简单的 Spring Initializr,我在其中添加了 Lombok 和 MapStruct 的配置:
...
<properties>
<java.version>11</java.version>
<mapstruct.version>1.4.1.Final</mapstruct.version>
<lombok.version>1.18.16</lombok.version>
</properties>
<dependencies>
...
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> …Run Code Online (Sandbox Code Playgroud) 我正在使用MapStruct将实体转换为 DTO。
我有一个带有实体 B 列表的实体 A:
public class A {
List<B> bs;
}
Run Code Online (Sandbox Code Playgroud)
我想在 ADto 类中有一个 B id 列表:
public class ADto {
List<Long> bIds;
}
Run Code Online (Sandbox Code Playgroud)