标签: mapstruct

使用MapStruct进行转换时防止循环引用

今天我开始使用MapStruct为我的项目创建我的Model to DTO转换器,我想知道它是否自动处理循环引用,但事实证明它没有.

这是我测试它的转换器:

package it.cdc.snp.services.rest.giudizio;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import org.springframework.stereotype.Component;

import it.cdc.snp.dto.entita.Avvisinotifica;
import it.cdc.snp.dto.entita.Corrispondenza;
import it.cdc.snp.model.notifica.AvvisoDiNotificaModel;
import it.cdc.snp.model.notifica.NotificaModel;
import it.cdc.snp.model.procedimento.ProcedimentoModel;

@Component
@Mapper(componentModel="spring")
public interface NotificaMapper {

    NotificaMapper INSTANCE = Mappers.getMapper( NotificaMapper.class );

    @Mappings({
        @Mapping(source = "avvisinotificas", target = "avvisinotificas"),
    })
    NotificaModel<ProcedimentoModel> corrispondenzaToNotificaModel(Corrispondenza notifica);

    @Mappings({
        @Mapping(source = "corrispondenza", target = "notifica"),
    })
    AvvisoDiNotificaModel avvisinotificaToAvvisoDiNotificaModel(Avvisinotifica avvisinotifica);


}
Run Code Online (Sandbox Code Playgroud)

这是测试:

        Notifica sourceObject1 = new Notifica();
        sourceObject1.setId(new Long(1));
        Avvisinotifica sourceObject2 = new Avvisinotifica();
        sourceObject2.setId(new Long(11));
        List<Avvisinotifica> tests= new ArrayList<>(); …
Run Code Online (Sandbox Code Playgroud)

java cyclic-reference mapstruct

4
推荐指数
4
解决办法
4378
查看次数

如果Dto使用MapStruct具有ID,则将dto映射到从数据库检索到的实体

我正在使用MapStruct进行dto <-> entity映射。相同的映射器用于从dto 创建 更新实体。完成dto的id验证以了解是否必须创建一个新实体(id == null)还是应该从数据库中检索它(id!= null)。

我实际上正在使用MapperDecorator作为解决方法。范例:

映射器

@Mapper
@DecoratedWith(UserAccountDecorator.class)
public interface UserAccountMapper {

    UserAccountDto map(User user);

    User map(UserAccountDto dto);

    User map(UserAccountDto dto, @MappingTarget User user);
}
Run Code Online (Sandbox Code Playgroud)

装饰器

public abstract class UserAccountDecorator implements UserAccountMapper {

    @Autowired
    @Qualifier("delegate")
    private UserAccountMapper delegate;

    @Autowired
    private UserRepository userRepository;

    @Override
    public User map(UserAccountDto dto) {
        if (dto == null) {
            return null;
        }

        User user = new User();
        if (dto.getId() != null) {
            user = …
Run Code Online (Sandbox Code Playgroud)

java mapping data-transfer-objects mapstruct

4
推荐指数
1
解决办法
7562
查看次数

Mapstruct 映射:如果所有源参数属性都为空,则返回空对象

如果@Mapping/source 中引用的所有属性都为null,我希望生成的mapstruct 映射方法返回null。例如,我有以下映射:

@Mappings({
      @Mapping(target = "id", source = "tagRecord.tagId"),
      @Mapping(target = "label", source = "tagRecord.tagLabel")
})
Tag mapToBean(TagRecord tagRecord);
Run Code Online (Sandbox Code Playgroud)

生成的方法是:

public Tag mapToBean(TagRecord tagRecord) {
    if ( tagRecord == null ) {
        return null;
    }

    Tag tag_ = new Tag();

    if ( tagRecord.getTagId() != null ) {
        tag_.setId( tagRecord.getTagId() );
    }
    if ( tagRecord.getTagLabel() != null ) {
        tag_.setLabel( tagRecord.getTagLabel() );
    }

    return tag_;
}
Run Code Online (Sandbox Code Playgroud)

测试用例:TagRecord 对象不为空,但具有 tagId==null 和 tagLibelle==null。

当前行为:返回的 Tag 对象不为 null,但具有 tagId==null 和 tagLibelle==null

如果(tagRecord.getTagId() == …

java object-object-mapping mapstruct

4
推荐指数
1
解决办法
6269
查看次数

更改生成的映射器类的位置

我按照文档(http://mapstruct.org/documentation/stable/reference/html/)中的描述将 MapStruct 与 maven 一起使用。现在我想将生成的映射器类的位置从目标文件夹更改为源文件夹。我已经阅读了如何更改 mapstruct 生成的类位置M2E 并让 maven 生成源文件夹作为 eclipse 源文件夹,并通过使用maven-processor-pluginapt-maven-plugin也可能工作)使其工作。除此之外,我必须从maven-compiler-plugin 中删除annotationProcessorPaths。否则 maven 构建失败,因为生成的代码在“generate-sources ”(更改后的输出文件夹)和“ target\generated-sources\annotations ”(默认输出文件夹)。但是现在每次我在 Eclipse 中点击“ Maven ”-->“ Update Project ”我都必须重新-在项目属性中启用“启用注释处理”。当我手动更改 eclipse 中的“生成的源目录”时,它也将被覆盖。

那么是否可以在 maven 中更改输出目录并在 eclipse 中启用“启用注释处理”?我正在使用安装了 MapStruct 和 m2e-apt 插件的 Eclipse JEE 氧气。如果有帮助,我也使用 Spring Boot 1.5.9

java eclipse maven mapstruct

4
推荐指数
1
解决办法
3373
查看次数

Migrating from Gradle 4 to 5. How to get mapstruct 1.20.final working with it

We've use mapstruct 1.20.final for approx 1.5 years with various Gradle versions - latest gradle 4.10.2. We want to switch to Gradle 5.4.1, which works with everything except mapstruct. Our working setup was not clean. Hence decided to start over. Old working setup was a hybrid form of the example on Github and the now obsolete setup.

Started again with http://mapstruct.org/news/2013-07-08-using-mapstruct-with-gradle as a base. Have this strong feeling this is NOT compatible with Gradle 5. Release notes Gradle 5 …

gradle mapstruct

4
推荐指数
1
解决办法
806
查看次数

如何将 MapStruct 用于不同的数据类型?

我有两种类型的数据要映射:

注册用户Dto:

public class SignUpUserDto {
    private String firstName;
    private String lastName;
    private String username;
    private String email;
    private String password;
    private String title;
}
Run Code Online (Sandbox Code Playgroud)

注册用户:

@Entity
public class SignUpUser {
    private Long id;
    private String firstName;
    private String lastName;
    private String username;
    private String email;
    private String password;
    private Title title;
}
Run Code Online (Sandbox Code Playgroud)

标题:

public enum Title {
    JUNIOR("junior"),
    MIDDLE("middle"),
    SENIOR("senior"),
    MANAGER("manager");

    private final String title;

    Title(final String title) {
        this.title = title;
    }

    public String toString() {
        return this.title; …
Run Code Online (Sandbox Code Playgroud)

java spring-boot mapstruct

4
推荐指数
1
解决办法
3987
查看次数

Mapstruct Java11 兼容吗?

我有点困惑。有一些文档说 java 9 是“实验性的”:

https://mapstruct.org/documentation/stable/reference/html/#_using_mapstruct_on_java_9

我发现了一个帖子,其中有人在 Java 10 中遇到了问题。所以我们将前往 Java 11,我想知道 Mapstuct 是否可以在该环境中工作。具体来说,它会在编译时生成代码并且生成的代码在那里工作(我想后者会)。

mapstruct java-11

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

从 ModelMapper 升级到 MapStruct 好不好?

目前我们在我们的项目中使用 ModelMapper。然而,在该站点中,我看到 MapStruct 有很多人喜欢。

不确定差异以及我们是否真的需要升级。

ModelMapper 和 MapStruct 之间有什么区别?

谢谢。

modelmapper mapstruct

4
推荐指数
1
解决办法
1653
查看次数

“Mapstruct”有没有办法使用mapstruct将空字符串映射到null?

我在Spring Boot应用程序中使用mapstructDto映射到实体,反之亦然。

我想知道,有没有一种方法可以使用 mapstruct @Mapping() 将任何 String 值作为值映射到null值 ?

是否可以?

提前致谢

spring-boot mapstruct

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

映射嵌套元素 - Mapstruct

我正在尝试使用 MapStruct 将以下源类映射到目标类。

目标班级:

public class Response {
    private List<Customer> customer = new ArrayList<Customer>();
}

public class Customer {
    private String customerId;
    private List<Product> products = new ArrayList<Product>();
}

public class CustProduct {
    private String CustProductId;
    private String CustPdtName;
    private List<productDetail> CustProductDetails = new ArrayList<productDetail>();
}
Run Code Online (Sandbox Code Playgroud)

源类:

public class UserList {
    protected List<User> user;
}

public class User {
    protected String userId;
    protected List<String> productRefId;  //List of products for that particular user
}

public class ProductList {
    protected List<Product> product; …
Run Code Online (Sandbox Code Playgroud)

java nested-lists object-object-mapping spring-boot mapstruct

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