标签: mapstruct

Mapstruct 忽略目标嵌套属性映射

我有以下 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)

java mapstruct

2
推荐指数
1
解决办法
6314
查看次数

使用 @aftermapping 进行 MapStruct 批量转换

我想在 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

2
推荐指数
1
解决办法
1万
查看次数

如何使用 MapStruct 将日期字符串转换为另一种格式?

我正在使用 MapStruct 将值从源映射到目标类。两个类都应具有日期属性,但日期格式不同。如何在使用 MapStruct 映射属性时转换日期格式?

源类的日期格式:2018-05-18T18:43:33.623+0200

目标班级日期格式:2018-05-18

format date mapstruct

2
推荐指数
1
解决办法
1万
查看次数

Mapstruct 映射 - 字符串到列表&lt;String&gt;

我正在努力将字符串对象从 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)

但它不起作用。有人可以帮忙吗?

java mapping mapstruct

2
推荐指数
1
解决办法
1万
查看次数

MapStruct - 如何忽略 POJO 中不必要的方法/非 getter-setter 方法

下面是 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生成映射的实现时,我们如何才能忽略这些方法呢?

java mapping list pojo mapstruct

2
推荐指数
1
解决办法
2986
查看次数

映射器装饰器未编译

我的映射器的映射器装饰器未编译。映射器正在编译,但装饰器未编译。因为,在构建过程中,即使我在映射器装饰器中执行此操作,我也会遇到类型转换错误。还有什么要补充的吗?

映射器代码:

 @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)

spring-mvc java-8 mapstruct

2
推荐指数
1
解决办法
1587
查看次数

如何在 mapstruct 中使用来自不同类的另一个映射

我想将模型对象映射到 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)

java spring java-8 mapstruct

2
推荐指数
2
解决办法
1万
查看次数

MapStruct 映射器返回空的映射对象

我正在尝试使用 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)

lombok spring-boot mapstruct

2
推荐指数
2
解决办法
1082
查看次数

MapStruct 无法识别 @Mapping 中的字段 - 简单配置

我有两节课。模型:

@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)

java maven lombok mapstruct java-11

2
推荐指数
1
解决办法
1613
查看次数

使用 Mapstruct 将对象列表转换为长 ID 列表

我正在使用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)

java spring-boot mapstruct

2
推荐指数
2
解决办法
225
查看次数

标签 统计

mapstruct ×10

java ×6

java-8 ×2

lombok ×2

mapping ×2

spring-boot ×2

date ×1

format ×1

java-11 ×1

list ×1

maven ×1

pojo ×1

spring ×1

spring-mvc ×1