Java 使用mapper类将DTO转换为实体

Java 使用mapper类将DTO转换为实体,java,spring,dto,Java,Spring,Dto,我有一个类似这样的实体类: @Entity public class Website { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String name; private String url; public Website() { //Constructor //getters and sett

我有一个类似这样的实体类:

@Entity
public class Website {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Integer id;
    private String name;
    private String url;
    public Website() {
     //Constructor 
      //getters and setters
    }
@Component 
public class WebsiteMapper {

    public List<WebsiteDto> getWebsiteList() {
        return repository.findAll().stream().map(w -> {
            WebsiteDto dto = new WebsiteVo(w.getId(), w.getName(), w.getUrl());
            return dto;
        }).collect(Collectors.toList());
以下是DTO类:

public class WebsiteDto { 
    private Integer id;
    private String name;
    private String url;
    public WebsiteVo() {

      //Constructor 
      //getters and setters
    }
我有这样的WebItemApper:

@Entity
public class Website {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Integer id;
    private String name;
    private String url;
    public Website() {
     //Constructor 
      //getters and setters
    }
@Component 
public class WebsiteMapper {

    public List<WebsiteDto> getWebsiteList() {
        return repository.findAll().stream().map(w -> {
            WebsiteDto dto = new WebsiteVo(w.getId(), w.getName(), w.getUrl());
            return dto;
        }).collect(Collectors.toList());
@组件
公共类WebItemApper{
公共列表getWebsiteList(){
返回repository.findAll().stream().map(w->{
WebsiteDto dto=新的WebsiteVo(w.getId(),w.getName(),w.getUrl());
返回dto;
}).collect(Collectors.toList());
我还有存储库接口:

public interface WebsiteRepository extends JpaRepository<Website, Integer> {
}
公共接口网站存储库扩展了JpaRepository{
}

现在,我想使用我的类WebItemApper将DTO转换为实体。因为我在这个类中进行了转换。我怎么做?

您好,我想您希望将实体转换为DTO。这很简单。在DTO类或任何UTI类中创建静态方法。返回类型应该是DTO类型

e、 g

公共类网站{
私有整数id;
私有字符串名称;
私有字符串url;
要导出的公共静态网站(网站){
//返回网站DTO的新实例
将新网站返回到(
website.getId(),
website.getName(),
website.getUrl()
);
}
公共静态列表导出(列表网站){
//返回网站DTO列表的新实例
返回websites.stream().map(网站->{
将新网站返回到(
website.getName(),
website.getUrl()
}).collect(Collectors.toList());
}
}

注意您也可以使用类似的方法将DTO转换为实体。

使用spring
org.springframework.beans.BeanUtils
提供的
BeanUtils
怎么样

public List<WebsiteDto> getWebsiteList() {
        return repository.findAll().stream().map(w -> {
            WebsiteDto dto = new WebsiteVo();
            BeanUtils.copyProperties(w, dto);  // copys all variables with same name and type
            return dto;
        })
        .collect(Collectors.toList());
}
public List getWebsiteList(){
返回repository.findAll().stream().map(w->{
WebsiteDto dto=新的WebsiteVo();
copyProperties(w,dto);//复制具有相同名称和类型的所有变量
返回dto;
})
.collect(Collectors.toList());
}

byteHungt3r。因此,我有一个接口类和一个实现我的接口的类。我也在类和WebItemApper中调用存储库。我只需要使用@Autowiredy调用WebItemApper。您的DTO对象不应该包含从实体->DTO转换的逻辑,您应该始终在两者之间有一个transformer类。DTO应该不包含逻辑,它们应该是简单的模型类。什么是WebItemApper?这个类注入WebsiteRepository吗?为什么WebItemApper是@Component?看看这里@Lemmy WebItemApper提供了将DTO转换为实体的方法,反之亦然。看看Mapstruct或ModelMapper库。