使用DTO作为另一个DTO中的字段

huangapple 未分类评论46阅读模式
英文:

Using DTO as fields in another DTO

问题

我有两个实体和两个对应的数据库表格。

public class Book {
    private int id;
    private int authorId;
    private String title;
    private int year;

    // getter setter;
}

public class Author {
    private int id;
    private String firstName;
    private String lastName;

    // getter setter;
}

我需要在 RestController 中发送下一个 DTO 列表(List<BookDto> books)。

public class BookDto {
    private int id;
    private AuthorDTO author;
    private String title;
    private int year;

    // getter setter;
}

相应地,我需要下一个 DTO。

public class AuthorDTO {
    private int id;
    private String firstName;
    private String lastName;

    // getter setter;
}

我在哪里以及如何聚合这两个 DTO?

从数据库获取数据我使用 JdbcTemplate。我有以下方法:getAuthorById(返回 Author)、getAuthorsByIds(返回 List<Author>)、getBookById(返回 Book)、getBooksByIds(返回 List<Book>)。

现在我创建了一个中间类。

public class BookWithAuthor {
    private int id;
    private Author author;
    private String title;
    private int year;

    // getter setter;
}

并在服务中实现了以下逻辑。

List&lt;BookWithAuthor&gt; getBooksWithAuthors(Set&lt;Integer&gt; ids) {
    List&lt;Book&gt; books = getBooksByIds(ids);

    List&lt;Author&gt; authors = getAuthors(books);

    return books.stream()
            .map(book -&gt; getBookWithAuthors(book, authors))
            .collect(Collectors.toList());
}

private List&lt;Author&gt; getAuthors(List&lt;Book&gt; books) {
    Set&lt;Integer&gt; authorIds = books.stream()
            .map(Book::getAuthorId)
            .collect(toSet());

    return authorService.getAuthorsByIds(authorIds);
}

private BookWithAuthor getBookWithAuthors(Book book, List&lt;Author&gt; authors) {
    BookWithAuthor bookWithAuthor = new BookWithAuthor();

    bookWithAuthor.setId(book.getId());
    bookWithAuthor.setAuthor(getAuthor(authors, book.getAuthorId()));
    // and next setting

    return bookWithAuthor;
}

我不确定这是否是正确的方法,或者是否有更简单的方法。

英文:

I have two entity and two corresponding tables in DB

public class Book {
    private int id;
    private int authorId;
    private String title;
    private int year;
    
    // getter setter;
}

public class Author {
   private int id;
   private String firstName;
   private String lastName; 

   // getter setter;
}

I need to send in RestController list of next DTO (List<BookDto> books)

public class BookDto {
   private int id;
   private AuthorDTO author;
   private String title;
   private int year;
    
   // getter setter; 
} 

Accordingly I need next DTO

public class AuthorDTO {
   private int id;
   private String firstName;
   private String lastName; 

   // getter setter;
}

Where and how I can aggregate this two DTOs?
For gettind data from DB i use JdbcTemplate. And I have methods getAuthorById (return Author), getAuthorsByIds (return List<Author>), getBookById (return Book), getBooksByIds (return List<Book>).

Now I create intermediate class

public class BookWithAuthor {
   private int id;
   private Author author;
   private String title;
   private int year;
    
   // getter setter; 
}

and next logic in service

List&lt;BookWithAuthor&gt; getBooksWithAuthors(Set&lt;Integer&gt; ids) {
    List&lt;Book&gt; books = getBooksByIds(ids);

    List&lt;Author&gt; authors = getAuthors(books);

    return books.stream()
            .map(book -&gt; getBooksWithAuthors(book, authors)
            .collect(Collectors.toList());
}

private List&lt;Author&gt; getAuthors(List&lt;Books&gt; books) {
    Set&lt;Integer&gt; getAuthorsIds = books.stream()
            .map(Books::getAuthorId)
            .collect(toSet());

    return authorService.getAuthorsByIds(getAuthorsIds );
}

private BookWithAuthor getBooksWithAuthors(Book book, List&lt;Author&gt; authors) {
    BookWithAuthor bookWithAuthor= new BookWithAuthor();

    bookWithAuthor.setId(book.getId());
    bookWithAuthor.setAuthor(getAuthor(authors, book.getAuthorId()));
    // and next setting

    return bookWithAuthor;
}

I'm not sure is it right way or, maybe here is simplier ways.

huangapple
  • 本文由 发表于 2020年4月10日 15:39:18
  • 转载请务必保留本文链接:https://java.coder-hub.com/61135916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定