地图列表<对象>列表< objectsrv>

发布于 2025-01-22 18:33:41 字数 1378 浏览 0 评论 0原文

我想映射列表<城市>列表< Citysrv>在服务层中,但我不知道该怎么做。

我想映射的City SRV:(SRV =服务器响应值)

@Getter
@Setter
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
    public class CitySrv {
        private Long id;
        private String name;
        private Long state;
    }

想要映射的城市实体:

@AllArgsConstructor
@NoArgsConstructor
@Entity
@SuperBuilder
@Getter
@Setter
@Table(name = "IOTP_CITY")
public class City extends BaseEntity {
    @Column(name = "NAME")
    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "state_id")
    private State state;

    @OneToMany(mappedBy = "city")
    private List<Branch> branches = new ArrayList<Branch>();

}

我以前使用过Model Mapper,但由于某些原因,我无法再使用它。

        @Override
    public GenericResponse<List<CitySrv>> getAllCities() throws ProjectException {
        Optional<List<City>> citiiesSrvResponse = cityRepository.getAllCities();
        if (citiiesSrvResponse.isPresent()) {
            List<CitySrv> citiesList = modelMapper.map(citiiesSrvResponse.get(), new 
                TypeToken<List<CitySrv>>() {
            }.getType());
            return ResponseUtil.getResponse(citiesList);
        } else throw new ProjectException(ExceptionStatus.NOT_FOUND);
    }

I want to Map List < City > to List < CitySrv > in the service layer but i dont have any idea how to do that.

the City srv that i want to Map: (srv = server response value)

@Getter
@Setter
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
    public class CitySrv {
        private Long id;
        private String name;
        private Long state;
    }

the City Entity that want to map From:

@AllArgsConstructor
@NoArgsConstructor
@Entity
@SuperBuilder
@Getter
@Setter
@Table(name = "IOTP_CITY")
public class City extends BaseEntity {
    @Column(name = "NAME")
    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "state_id")
    private State state;

    @OneToMany(mappedBy = "city")
    private List<Branch> branches = new ArrayList<Branch>();

}

i used Model mapper before but for some reason i cant use it anymore.

        @Override
    public GenericResponse<List<CitySrv>> getAllCities() throws ProjectException {
        Optional<List<City>> citiiesSrvResponse = cityRepository.getAllCities();
        if (citiiesSrvResponse.isPresent()) {
            List<CitySrv> citiesList = modelMapper.map(citiiesSrvResponse.get(), new 
                TypeToken<List<CitySrv>>() {
            }.getType());
            return ResponseUtil.getResponse(citiesList);
        } else throw new ProjectException(ExceptionStatus.NOT_FOUND);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沦落红尘 2025-01-29 18:33:41

我像这样处理它,但不是很好

public GenericResponse<List<CitySrv>> getAllCities() throws ProjectException 
    {
    Optional<List<City>> citiiesSrvResponse = cityRepository.getAllCities();
    CitySrv citySrv = new CitySrv();
    List<CitySrv> citySrvList = new ArrayList<>();
    if (citiiesSrvResponse.isPresent()) {
        for (City eachCity : citiiesSrvResponse.get()) {
            citySrv.setId(eachCity.getId());
            citySrv.setName(eachCity.getName());
            citySrv.setState(eachCity.getState().getId());
            citySrvList.add(citySrv);
        }
        return ResponseUtil.getResponse(citySrvList);
    } else throw new ProjectException(ExceptionStatus.NOT_FOUND);
}

i handle it just like this but its not so good

public GenericResponse<List<CitySrv>> getAllCities() throws ProjectException 
    {
    Optional<List<City>> citiiesSrvResponse = cityRepository.getAllCities();
    CitySrv citySrv = new CitySrv();
    List<CitySrv> citySrvList = new ArrayList<>();
    if (citiiesSrvResponse.isPresent()) {
        for (City eachCity : citiiesSrvResponse.get()) {
            citySrv.setId(eachCity.getId());
            citySrv.setName(eachCity.getName());
            citySrv.setState(eachCity.getState().getId());
            citySrvList.add(citySrv);
        }
        return ResponseUtil.getResponse(citySrvList);
    } else throw new ProjectException(ExceptionStatus.NOT_FOUND);
}
假面具 2025-01-29 18:33:41

为了使模型设备工作要工作,您的来源和目的地必须足够相似。您想将城市中的实体状态映射到CitySRV的ID。我猜想无法满足这一标准。您必须告诉ModelMapper如何通过配置

TypeMap<City, CitySrv> propertyMapper = modelMapper.createTypeMap(City.class, CitySrv.class);
propertyMapper.addMappings(
    mapper -> mapper.map(src -> src.getState().getId, CitySrv::setState)
);

最佳映射您在全局配置中进行映射。
您可以在此

For modelMapper to work your source and destination have to be similar enough. You want to map the entity State in City to a Long which is its id in CitySrv. Which I would guess doesn't fulfill this criteria. You have to tell modelMapper how to map that by configuring a mapping

TypeMap<City, CitySrv> propertyMapper = modelMapper.createTypeMap(City.class, CitySrv.class);
propertyMapper.addMappings(
    mapper -> mapper.map(src -> src.getState().getId, CitySrv::setState)
);

Best you do that in a global config.
You can find more info in this baeldung article

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文