Java 将DTO用作另一个DTO中的字段
我在DB中有两个实体和两个对应的表Java 将DTO用作另一个DTO中的字段,java,spring-boot,jdbctemplate,dto,Java,Spring Boot,Jdbctemplate,Dto,我在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 last
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;
}
我需要发送下一个DTO的列表(列表簿)
因此,我需要下一个DTO
public class AuthorDTO {
private int id;
private String firstName;
private String lastName;
// getter setter;
}
在哪里以及如何聚合这两个DTO?
为了从DB中获取数据,我使用JdbcTemplate。我有方法getAuthorById(返回作者)、getAuthorsById(返回列表)、getBookById(返回书)、getBookById(返回列表)
现在我创建中间类
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 -> 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;
}
列出GetBookWithAuthors(设置ID){
List books=getbooksbyid(id);
列出作者=获取作者(书籍);
还书。stream()
.map(book->getBooksWithAuthors(book,authors)
.collect(Collectors.toList());
}
私人列表作者(列表书籍){
Set getAuthorsIds=books.stream()
.map(书籍::getAuthorId)
.收集(toSet());
返回authorService.getAuthorsByIds(getAuthorsIds);
}
private Book With Author GetBooks With authors(图书,列出作者){
BookWithAuthor BookWithAuthor=新建BookWithAuthor();
bookWithAuthor.setId(book.getId());
bookWithAuthor.setAuthor(getAuthor(authors,book.getAuthorId());
//下一个场景呢
还书与作者;
}
我不确定这是正确的方法还是更简单的方法。您可以定义Book和Author实体之间的一对多映射,并在一个查询中获取它。您可以用json显示预期的响应吗?{“id”:1,“Author”:{“id”:1,“firstName”:“Stephen”,“lastName”:“King”,},“title”:“黑塔”,“年份”:1986}这个对象的列表可能与这个问题无关,但JPA有一种巧妙的方法,可以通过
@Embedded
和@embeddeble
注释来实现这一点。我必须使用JdbcTemplate。单个qeury也不是一个好主意,因为我的每个存储库都必须使用自己的实体。您可以定义书籍和作者之间的一对多映射您能用json显示预期的响应吗?{“id”:1,“author”:{“id”:1,“firstName”:“Stephen”,“lastName”:“King”,“title”:“Dark Tower”,“year”:1986}这个对象的列表可能与问题无关,但JPA有一种巧妙的方法,可以通过@Embedded
和@embeddeble
注释来实现。我必须使用JdbcTemplate。单个qeury也不是个好主意,因为我的每个存储库都必须使用自己的实体
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;
}