简体   繁体   English

我想在 mapstruct 生成的 ~impl 文件中为 list.add 提供条件。 我应该怎么办?

[英]I want to give condition to list.add in the ~impl file generated by mapstruct. What should I do?

I wonder if it is possible to apply a conditional expression when converting an entity to dto using mapstruct.我想知道在使用 mapstruct 将实体转换为 dto 时是否可以应用条件表达式。 There are many search results for property, but I can't find it when I'm curious.有很多关于房产的搜索结果,但我很好奇时找不到。

In the example code below, in the StudentMenuMapperImpl.java file list.add( toDto( studentMenu ) ) I want to give conditional expression to在下面的示例代码中,在 StudentMenuMapperImpl.java 文件list.add( toDto( studentMenu ) )中,我想给条件表达式

I know how to override toDto in StudentMenuMapper Interface.我知道如何在 StudentMenuMapper 接口中覆盖 toDto。 But I want to know another simple way.但我想知道另一种简单的方法。 (like annotation) (如注释)

Alternatively, is it possible to return null from the toDto function with @AfterMapping and @BeforeMapping?或者,是否可以使用@AfterMapping 和@BeforeMapping 从 toDto function 返回 null?

[StudentMenuDto.java] [StudentMenuDto.java]

public class StudentMenuDto {
    private Long id;
    private Long parentId;
    List<StudentMenuDto> childStudentMenuList;
}

[GenericMapper.java] [通用映射器.java]

public interface GenericMapper<D, E> {
    D toDto(E entity);
    List<D> toDto(List<E> entityList);
}

[StudentMenuMapper.java] [StudentMenuMapper.java]

@Mapper
public interface StudentMenuMapper extends GenericMapper<StudentMenuDto, StudentMenu> {
    StudentMenuMapper INSTANCE = Mappers.getMapper(StudentMenuMapper.class);
    
    @Override
    StudentMenuDto toDto(StudentMenu entity);
}

[StudentMenuMapperImpl.java] - impl file genereated by mapstruct [StudentMenuMapperImpl.java] - mapstruct 生成的 impl 文件

@Component
public class StudentMenuMapperImpl implements StudentMenuMapper {

    @Override
    public List<StudentMenuDto> toDto(List<StudentMenu> entityList) {
        if ( entityList == null ) {
            return null;
        }

        List<StudentMenuDto> list = new ArrayList<StudentMenuDto>( entityList.size() );
        for ( StudentMenu studentMenu : entityList ) {
            /* [AS-IS] */
            list.add( toDto( studentMenu ) );
            /* [TO-BE] I want to add various conditional statements like this
                if(studentMenu.getId != 1){
                    list.add( toDto( studentMenu ) );
                }
            */
        }

        return list;
    }

    @Override
    public StudentMenuDto toDto(StudentMenu entity) {

        if ( entity == null ) {
            return null;
        }

        StudentMenuDto studentMenuDto = new StudentMenuDto();

        studentMenuDto.setId( entity.getId() );
        studentMenuDto.setParentId( entityParentStudentMenuId( entity ) );
        studentMenuDto.setChildStudentMenuList( toDto( entity.getChildStudentMenuList() ) );

        return studentMenuDto;
    }

    private Long entityParentStudentMenuId(StudentMenu studentMenu) {
        if ( studentMenu == null ) {
            return null;
        }
        StudentMenu parentStudentMenu = studentMenu.getParentStudentMenu();
        if ( parentStudentMenu == null ) {
            return null;
        }
        Long id = parentStudentMenu.getId();
        if ( id == null ) {
            return null;
        }
        return id;
    }
}

They are working on this type of filter, which will be released with version 1.6.0他们正在研究这种类型的过滤器,它将与版本 1.6.0 一起发布
You can follow the request on github: Allow conditions/filters mapping iterables and maps您可以按照 github 上的请求:允许条件/过滤器映射迭代和映射

At the moment the solution is to create a method annoted with @AfterMapping which simply remove useless object from the list.目前解决方案是创建一个用@AfterMapping 注释的方法,它只是从列表中删除无用的 object。

If you need, you can take a look to: After-Mapping Annotations如果需要,可以查看:映射后注释

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法缩短这些命令,这样我就不必重复给 list.add (对于每个所有者)? - Is there a way to shorten these commands so that I don't have to give the list.add repeatedly (for each owner)? 地图结构。 通过表达式映射列表元素的字段 - MapStruct. Mapping fields of list element by expression 为什么我需要做list.add(new ArrayList &lt;&gt;(temp)); 将列表添加到2D列表时? - Why do I need to do list.add(new ArrayList<>(temp)); when adding a list to a 2D list? 如果我想在单击“like[i]”按钮时打印“i”的值,该怎么办? 就像当我点击“按钮 2”时,它应该给我值“2” - What should I do if I want to print value of ' i ' when I clicked 'like[i]' button? Like when I click on 'Button 2' then it should give me value '2' 我应该为已创建并要存储在远程tomcat服务器中的文件(最好是json)提供什么文件路径? - What file path should I give for the file(preferrably json) which I have created and want to store in remote tomcat server? list.add上的NullPointerException - NullPointerException on list.add 我想运行“ HttpAsyncTask” ...我该怎么办? - I want run “HttpAsyncTask”… What should i do? 我想把这个SQL变成QueryDsl,怎么办? - I want to make this SQL to QueryDsl, what should I do? MapStruct:如何过滤到我想要和不想映射的字段? - MapStruct: How can I do filter to fields which I want and don't want to mapping? List.add()预期的类 - List.add() Class Expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM