mapstruct 将一个 id 映射到一个对象
mapstruct map an id to an object
我有一个对象学校,对象学校有一个对象人,人已经保存在数据库中,当我保存学校对象时,我给它一个人 ID
所以在 class 学校我有一个 Person 类型的属性 person,在 SchoolDTO 我有一个 Long
类型的属性 personId
@Mapper(componentModel = "spring", uses = { PersonMapper.class })
public interface SchoolMapper extends EntityMapper<SchoolDTO, School>{
@Mapping(source = "personId", target = "person")
School toEntity(SchoolDTO schoolDTO);
}
School school = schoolMapper.toEntity(schoolDTO);
log.info(school.getPerson());
public interface EntityMapper <D, E> {
E toEntity(D dto);
D toDto(E entity);
List <E> toEntity(List<D> dtoList);
List <D> toDto(List<E> entityList);
}
@Mapper(componentModel = "spring", uses = {})
public interface PersonMapper extends EntityMapper<PersonDTO, Person> {
default Person fromId(Long id) {
if (id == null) {
return null;
}
Person person= new Person();
person.setId(id);
return person;
}
}
这里的问题是当我显示这个人时它向我显示了 id 及其值和另一个属性 null
您的 Person
仅在设置了 id 值的情况下显示的原因是因为您的 fromId
方法创建了一个空的 Person
并且仅设置了 id。
我假设您想从数据库中获取 Person
。
要实现这一点,您只需告诉 MapStruct 使用服务,或者您可以将其注入映射器并执行获取。
如果您有这样的服务:
public interface PersonService {
Person findById(Long id);
}
你的映射器:
@Mapper(componentModel = "spring", uses = { PersonService.class })
public interface SchoolMapper extends EntityMapper<SchoolDTO, School>{
@Mapping(source = "personId", target = "person")
School toEntity(SchoolDTO schoolDTO);
}
我们可以通过引入 ReferenceMapper 来概括前面的答案,如下所示:
@Component
public class ReferenceMapper {
@PersistenceContext
private EntityManager entityManager;
@ObjectFactory
public <T> T map(@NonNull final Long id, @TargetType Class<T> type) {
return entityManager.getReference(type, id);
}
}
那么,PersonMapper 将是:
@Mapper(componentModel = "spring", uses = {ReferenceMapper.class})
public interface PersonMapper {
Person toEntity(Long id);
}
最后是 SchoolMapper:
@Mapper(componentModel = "spring",uses = {PersonMapper.class})
public interface SchoolMapper {
@Mapping(source = "personId", target = "person")
School toEntity(SchoolDTO schoolDTO);
}
生成的源将是:
@Override
public School toEntity(InDto dto) {
if ( dto == null ) {
return null;
}
School school = new School();
school.setPerson( personMapper.toEntity( dto.getPersonId() ) );
// other setters
return school;
}
我有一个对象学校,对象学校有一个对象人,人已经保存在数据库中,当我保存学校对象时,我给它一个人 ID
所以在 class 学校我有一个 Person 类型的属性 person,在 SchoolDTO 我有一个 Long
类型的属性 personId @Mapper(componentModel = "spring", uses = { PersonMapper.class })
public interface SchoolMapper extends EntityMapper<SchoolDTO, School>{
@Mapping(source = "personId", target = "person")
School toEntity(SchoolDTO schoolDTO);
}
School school = schoolMapper.toEntity(schoolDTO);
log.info(school.getPerson());
public interface EntityMapper <D, E> {
E toEntity(D dto);
D toDto(E entity);
List <E> toEntity(List<D> dtoList);
List <D> toDto(List<E> entityList);
}
@Mapper(componentModel = "spring", uses = {})
public interface PersonMapper extends EntityMapper<PersonDTO, Person> {
default Person fromId(Long id) {
if (id == null) {
return null;
}
Person person= new Person();
person.setId(id);
return person;
}
}
这里的问题是当我显示这个人时它向我显示了 id 及其值和另一个属性 null
您的 Person
仅在设置了 id 值的情况下显示的原因是因为您的 fromId
方法创建了一个空的 Person
并且仅设置了 id。
我假设您想从数据库中获取 Person
。
要实现这一点,您只需告诉 MapStruct 使用服务,或者您可以将其注入映射器并执行获取。
如果您有这样的服务:
public interface PersonService {
Person findById(Long id);
}
你的映射器:
@Mapper(componentModel = "spring", uses = { PersonService.class })
public interface SchoolMapper extends EntityMapper<SchoolDTO, School>{
@Mapping(source = "personId", target = "person")
School toEntity(SchoolDTO schoolDTO);
}
我们可以通过引入 ReferenceMapper 来概括前面的答案,如下所示:
@Component
public class ReferenceMapper {
@PersistenceContext
private EntityManager entityManager;
@ObjectFactory
public <T> T map(@NonNull final Long id, @TargetType Class<T> type) {
return entityManager.getReference(type, id);
}
}
那么,PersonMapper 将是:
@Mapper(componentModel = "spring", uses = {ReferenceMapper.class})
public interface PersonMapper {
Person toEntity(Long id);
}
最后是 SchoolMapper:
@Mapper(componentModel = "spring",uses = {PersonMapper.class})
public interface SchoolMapper {
@Mapping(source = "personId", target = "person")
School toEntity(SchoolDTO schoolDTO);
}
生成的源将是:
@Override
public School toEntity(InDto dto) {
if ( dto == null ) {
return null;
}
School school = new School();
school.setPerson( personMapper.toEntity( dto.getPersonId() ) );
// other setters
return school;
}