Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用mapStruct进行修补映射?_Java_Spring Boot_Mapstruct - Fatal编程技术网

Java 如何使用mapStruct进行修补映射?

Java 如何使用mapStruct进行修补映射?,java,spring-boot,mapstruct,Java,Spring Boot,Mapstruct,我想知道我是否可以忽略空字段,只转换我在请求中输入的属性 我的实体: public class Entity{ private Long id; private String name; private String description; } 我的DTO: public class EntityDTO{ private String name; private String description; } 我的地图绘制者: @Mapper(componentModel = "

我想知道我是否可以忽略空字段,只转换我在请求中输入的属性

我的实体:

public class Entity{

private Long id;
private String name;
private String description;
}
我的DTO:

public class EntityDTO{
 private String name;
 private String description;
}
我的地图绘制者:

@Mapper(componentModel = "spring")
public interface EntityMapper {

    @BeanMapping(nullValueCheckStrategy = NullValueCheckStrategy.ON_IMPLICIT_CONVERSION)
    Entity toEntity(EntityDTO entityDTO);
}
我生成的代码:

@Component
public class EntityMapperImpl implements EntityMapper {

    @Override
    public Entity toEntity(EntityDTO entityDTO) {
        if ( entityDTO == null ) {
            return null;
        }

        EntityBuilder entity = Entity.builder();

        entity.name( tipoOperacaoParcialUpdate.getName());
        entity.description( tipoOperacaoParcialUpdate.getDescription());
        return entity.build();
    }
}
我希望生成如下代码:

@Component
public class EntityMapperImpl implements EntityMapper {

    @Override
    public Entity toEntity(EntityDTO entityDTO) {
        if ( entityDTO.getName() != null ) {
            entityDTO.setName(entityDTO.getName());
        }
        if ( entityDTO.getDescription() != null ) {
            entityDTO.setDescription(entityDTO.getDescription());
        }

        EntityBuilder entity = Entity.builder();

        entity.name( entityDTO.getName());
        entity.description( entityDTO.getDescription());
        return entity.build();
    }
}
我想要一个这样的东西,我怎么能用mapStruct做到这一点


谢谢大家!

我想你要找的是

e、 问题

这将产生如下结果:

@Component
public class EntityMapperImpl implements EntityMapper {

    @Override
    public Entity updateEntity(Entity entity, EntityDTO dto) {
        if ( dto == null ) {
            return null;
        }

        if ( dto.getName() != null ) {
            entity.setName(dto.getName());
        } else {
            entity.setName( null );
        }

        if ( dto.getDescription() != null ) {
            entity.setDescription(dto.getDescription());
        } else {
            entity.setDescription( null );
        }

        return entity;
    }
}

如果您想避免设置为null,可以使用
NullValuePropertyMappingStrategy#IGNORE

我想您要找的是

e、 问题

这将产生如下结果:

@Component
public class EntityMapperImpl implements EntityMapper {

    @Override
    public Entity updateEntity(Entity entity, EntityDTO dto) {
        if ( dto == null ) {
            return null;
        }

        if ( dto.getName() != null ) {
            entity.setName(dto.getName());
        } else {
            entity.setName( null );
        }

        if ( dto.getDescription() != null ) {
            entity.setDescription(dto.getDescription());
        } else {
            entity.setDescription( null );
        }

        return entity;
    }
}

如果要避免设置为null,可以使用
NullValuePropertyMappingStrategy#IGNORE

希望生成的代码可以抛出NullPointerException(如果entityDto为null)。除此之外,它只包含额外的和非必需的代码行,这些代码将执行与自动生成代码完全相同的功能。您希望生成的代码可以抛出NullPointerException(如果entityDto为null)。除此之外,它只包含额外的和非必需的代码行,这些代码将执行与自动生成的代码完全相同的功能