我正在尝试使用表示DAO和模型类的http://modelmapper.org/库映射两个泛型-
模型类-
public class PageModel<T> {
@JsonProperty("items")
private List<T> items;
@JsonProperty("meta")
private MetaModel meta;
public PageModel() {
}
public PageModel(int current, int limit) {
this.items = new ArrayList<>();
this.meta = new MetaModel(current, limit);
}
@JsonProperty("items")
public List<T> getItems() {
return items;
}
@JsonProperty("items")
public void setItems(List<T> items) {
this.items = items;
}
@JsonProperty("meta")
public MetaModel getMeta() {
return meta;
}
@JsonProperty("meta")
public void setMeta(MetaModel meta) {
this.meta = meta;
}
}
public class MetaModel {
@JsonProperty("total")
private Integer total;
public MetaModel() {
}
public MetaModel(int page, int limit) {
this.page = new PageMetaModel(page, limit);
}
@JsonProperty("total")
public Integer getTotal() {
return total;
}
@JsonProperty("total")
public void setTotal(Integer total) {
this.total = total;
}
}
public class SubjectModel {
private String name;
private StatusModel status;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public StatusModel getStatus() {
return status;
}
public void setStatus(StatusModel status) {
this.status = status;
}
}
DAO类-
public class Page<T> {
private List<T> items;
private int count;
public Page(List<T> items, int count) {
if(items == null)
throw new IllegalArgumentException("items can not be null");
this.items = items;
this.count = count;
}
public List<T> getItems() {
return items;
}
public void setItems(List<T> items) {
if(items == null)
throw new IllegalArgumentException("items can not be null");
this.items = items;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
公共课主题{ 私有字符串名称;
private Integer passCount;
private Integer failCount;
public Subject(String name, Integer passCount, Integer failCount) {
this.name = name;
this.passCount = passCount;
this.failCount = failCount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPassCount() {
return passCount;
}
public void setPassCount(Integer passCount) {
this.passCount = passCount;
}
public Integer getFailCount() {
return failCount;
}
public void setFailCount(Integer failCount) {
this.failCount = failCount;
}
}
映射器逻辑-
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration()
.setMatchingStrategy(MatchingStrategies.STRICT);
modelMapper.addMappings(new PropertyMap<Page<?>, PageModel<?>>() {
@Override
protected void configure() {
map().getMeta().setTotal(source.getCount());
}
});
modelMapper.addMappings(new PropertyMap<Subject, SubjectModel>() {
@Override
protected void configure() {
map().getStatus().setPassCount(source.getPassCount());
map().getStatus().setFailCount(source.getFailCount());
}
});
List<Subject> subjects = new ArrayList<>();
subjects.add(new Subject("Subject A", 10, 20));
subjects.add(new Subject("Subject B", 8, 7));
Page<Subject> page = new Page<>(subjects, 10);
Type pageType = new TypeToken<PageModel<SubjectModel>>() {}.getType();
PageModel<SubjectModel> pageModel = modelMapper.map(page, pageType);
似乎ModelMapper无法正常工作,并且给我pageModel
的主题类项目而不是主题模型类项目