[英]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.