如何使用mapper转换变量类型?(DTO到DAO)



我想使用生成器&用于发送和接收DTO-DAO-DTO的映射器。

这是我的Dto类、Mapper、DAO、Controller&服务功能。

我想通过从ConfigureRepository中选择字符串'voucherType'来将其设置为Configure'voucherType'。

Dto类

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ItemDto {
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class CreateReq {
@NotBlank
private String name;
@NotNull
private String voucherType;
public Item createReqToEntity() {
/* additional jobs for create entity? */
return ItemMapper.INSTANCE.createReqToEntity(this);
}
}
...

映射器

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface ItemMapper {
ItemMapper INSTANCE = Mappers.getMapper(ItemMapper.class);
Item createReqToEntity(ItemDto.CreateReq createReq);
Item updateReqToEntity(ItemDto.UpdateReq updateReq);
ItemDto.CreateRes entityToCreateRes(Item item);
ItemDto.UpdateRes entityToUpdateRes(Item item);
}

这是映射程序将使用的构造函数

public class Item {
@Builder
private Voucher(String name, String voucherType) {
this.name = name;
this.voucherType = configureRepository.findByConfigName(voucherType); // FK but i cannot import 'configureRepository.. why?'
this.id = FMSFactory.uuid();
this.created = new Date();
this.updated = new Date();
}

控制器

public ItemrDto.CreateRes saveItemInfo(@RequestBody @Valid ItemDto.CreateReq 
createItemReq) throws Exception {
return ItemService.saveItemInfo(createItemReq);
}

服务

public ItemDto.CreateRes saveItemInfo(ItemDto.CreateReq reqDto) {
Item newItem = ItemRepository.save(reqDto.createReqToEntity());
ItemDto.CreateRes result = ItemDto.CreateRes.entityToCreateRes(newItem);
return result;
}

生成输出

错误:找不到符号"this.voucherType=configureRepository.findByConfigName(voucherType(">

我需要将createReq中的String vouerType更改为Item类(DAO(中的Configure vouerType,但我不知道在哪里&如何使用Mapper设置它。

有人能帮我吗?

我之前的问题得到了一个很好的答案。

我仍然可以使用"mapStruct"来完成此操作。

ItemMapper

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface ItemMapper {
ItemMapper INSTANCE = Mappers.getMapper(ItemMapper.class);
Item createReqToEntity(ItemDto.CreateReq createReq, Configure A);
Item updateReqToEntity(ItemDto.UpdateReq updateReq, configure A);
// just overwrite specific variable like this.
// Configure A = creqteReq.A; will be replaced to
// Configure A = A;
ItemDto.CreateRes entityToCreateRes(Item item);
ItemDto.UpdateRes entityToUpdateRes(Item item);
}

只需覆盖我想转换类型的变量。

最新更新