英文:
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<BookWithAuthor> getBooksWithAuthors(Set<Integer> ids) {
List<Book> books = getBooksByIds(ids);
List<Author> authors = getAuthors(books);
return books.stream()
.map(book -> getBookWithAuthors(book, authors))
.collect(Collectors.toList());
}
private List<Author> getAuthors(List<Book> books) {
Set<Integer> authorIds = books.stream()
.map(Book::getAuthorId)
.collect(toSet());
return authorService.getAuthorsByIds(authorIds);
}
private BookWithAuthor getBookWithAuthors(Book book, List<Author> 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<BookWithAuthor> getBooksWithAuthors(Set<Integer> ids) {
List<Book> books = getBooksByIds(ids);
List<Author> authors = getAuthors(books);
return books.stream()
.map(book -> getBooksWithAuthors(book, authors)
.collect(Collectors.toList());
}
private List<Author> getAuthors(List<Books> books) {
Set<Integer> getAuthorsIds = books.stream()
.map(Books::getAuthorId)
.collect(toSet());
return authorService.getAuthorsByIds(getAuthorsIds );
}
private BookWithAuthor getBooksWithAuthors(Book book, List<Author> 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.
专注分享java语言的经验与见解,让所有开发者获益!
评论