如何用mapper翻译变量类型(dto到dao)

bwitn5fc  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(395)

我想使用生成器和Map器发送和接收dto-dao-dto。
这是我的dto类、Map器、dao、控制器和服务函数。
我想通过从configurerepository中选择字符串'vouchertype'来设置'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);
}

这是一个将由Map器使用的构造函数

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 vouchertype'更改为item class(dao)中的'configure vouchertype',但我不知道如何使用mapper设置它。
有人能帮我吗?

lnlaulya

lnlaulya1#

我的前一个问题得到了很好的答案。
我仍然可以用“mapstruct”来做这个。
项目Map器

@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);
}

只需覆盖我要翻译的变量类型。

相关问题