java - 从具有多个实体的映射器中获取一个实体
问题描述
假设您有两个实体在数据库 Ent1 和 Ent2 中定义对象,以及一个描述二合一的 DTO。
我的 MapStruct EntitiesDtoMapper 映射器看起来像:
@Mapper
interface EntitiesDtoMapper{
DTO EntitiesToDto(Ent1 ent1, Ent2 ent2);
//It is possible to do this?
Ent1 DtoToEnt1(DTO dto);
}
我喜欢从 DTO 获取 Ent1 和 Ent2,这可能吗?
解决方案
对谁感兴趣;这是可能的,但要注意实体参数的名称。
这里有一个例子:
@Data @Entity
public class Ent1{
public Ent1() {}
String id;
String name;
}
@Data @Entity
public class Ent2{
public Ent2() {}
String id;
String name;
}
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DTO{
String id1,id2,name1,name2;
}
@Mapper
public interface EntitiesDtoMapper{
EntitiesIcspDtoMapper INSTANCE = Mappers.getMapper(EntitiesDtoMapper.class);
@Mappings({
@Mapping(source="ent1.id", target = "id1"),
@Mapping(source="ent1.name", target = "name1"),
@Mapping(source="ent2.id", target = "id2"),
@Mapping(source="ent2.name", target = "name2")
})
DTO EntitiesToDto(Ent1 ent1, Ent2 ent2);
@Mappings({
@Mapping(source="id1", target = "id"),
@Mapping(source="name1", target = "name"),
})
Ent1 DtoToEnt1(DTO dto);
@Mappings({
@Mapping(source="id2", target = "id"),
@Mapping(source="name2", target = "name"),
})
Ent2 DtoToEnt2(DTO dto);
}
推荐阅读
- android - 不能将两个按钮并排放置
- python - 我不明白这个递归是如何打印树的
- css - 如何在 SASS 中设置不同的值?
- javascript - Javascript 字符串文字:它们是引用类型还是值类型?
- neo4j - Neo4j Sink 未收到 Kafka 主题事件
- c# - 对角线移动精灵
- python - 如何在 django 中将变量的值从视图发送到表单?
- c# - C#、YouTube 数据 API、CommentThreads.List()、使用 OAuth 时身份验证范围不足
- excel - 使用 VBA 从表中选择行
- c++ - 返回引用后将数组中的值(这是对象的属性)替换为垃圾