PageInfoDto<T>
:
public class PageInfoDto<T> {
private int currentPageNum;
private int totalPageNum;
private int perPageNum;
private int totalItemNum;
private List<T> list;
}
Page<T>
:
public class Page<T> {
private int current;
private int total;
private int size;
private int items;
private List<T> list;
}
I have a school list and a student list.
I want to map Page<School>
to PageInfoDto<SchoolDto>
and map Page<Student>
to PageInfoDto<StudentDto>
。
This is how my mapper looks like:
@Mapper(config = CentralConfig.class, uses = {StudentMapper.class, SchoolMapper.class}, componentModel = "spring")
public interface PageInfoMapper {
@Mappings({
@Mapping(target = "list", source = "pageInfo.records"),
@Mapping(target = "currentPageNum", source = "pageInfo.current"),
@Mapping(target = "totalPageNum", source = "pageInfo.pages"),
@Mapping(target = "perPageNum", source = "pageInfo.size"),
@Mapping(target = "totalItemNum", source = "pageInfo.total"),
})
PageInfoDto<SchoolDto> toPageInfoDto1(Page<School> pageInfo);
@Mappings({
@Mapping(target = "list", source = "pageInfo.records"),
@Mapping(target = "currentPageNum", source = "pageInfo.current"),
@Mapping(target = "totalPageNum", source = "pageInfo.pages"),
@Mapping(target = "perPageNum", source = "pageInfo.size"),
@Mapping(target = "totalItemNum", source = "pageInfo.total"),
})
PageInfoDto<StudentDto> toPageInfoDto2(Page<Student> pageInfo);
}
This is obviously not a clever way to do, having to repeat the same code. Is there a better way to do this?