gpt4 book ai didi

java - 如何使用模型映射器将通用类型与集合对象映射?

转载 作者:行者123 更新时间:2023-11-30 06:03:14 25 4
gpt4 key购买 nike

我正在尝试使用 http://modelmapper.org/ 映射两个泛型类型表示 DAO 和模型类的库 -

模型类 -

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)字符串名称;

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 与主题类项目而不是主题模型类项目

最佳答案

我已解决此问题:modelmapper#380并将在本周末发布版本。请查看这是否可以解决您的问题。谢谢。

关于java - 如何使用模型映射器将通用类型与集合对象映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51854040/

25 4 0
文章推荐: javascript - 检测元素外部的点击(带框架)
文章推荐: java - 了解 Hybris 中的 Impex 语法语句
文章推荐: javascript - jqgrid在navgrid中添加个人按钮
文章推荐: java - 将泛型 Collection 转换为 Collection