0
我被一個問題困住了。我想從我的數據庫中顯示所有書籍作爲列表,更重要的是,他們每個人都顯示它的列表作者。這是我的控制器方法看起來像:百里香顯示本書作者列表
@RequestMapping(value="/new",method = RequestMethod.GET)
public ModelAndView newBooks() {
List<Book> books = bookRepository.findAllByOrderByPremiereDateDesc();
Set<Author> authors = new HashSet<>();
for(Book b:books){
authors = b.getAuthors();
}
Map<String, Object> model = new HashMap<String, Object>();
model.put("books",books);
model.put("authors", authors);
return new ModelAndView("books/new","model",model);
}
,但我不知道如何鏈接書及其作者在我看來,正確顯示它們:
<table>
<tr>
<th>Title</th>
<th>Title original</th>
<th>Premiere date</th>
<th>Authors</th>
</tr>
<tr th:each="b : ${model.books}">
<td th:text="${b.title}"></td>
<td th:text="${b.titleOriginal}"></td>
<td th:text="${b.premiereDate}"></td>
<tr th:each="a: ${b.authors}">
<td th:text="${a.name}"></td>
<td th:text="${a.surname}"></td>
</tr>
</table>
而且我書(實體)模型類:
@Entity
@Table(name = "book")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "book_id")
private int bookId;
@Column(name = "isbn")
private long isbn;
@Column(name = "title")
private String title;
@Column(name = "title_original")
private String titleOriginal;
@Column(name = "premiere_date")
private Date premiereDate;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "book_author", joinColumns = {@JoinColumn(name = "book_id")}, inverseJoinColumns = {@JoinColumn(name = "author_id")})
private Set<Author> authors = new HashSet<Author>();
如果我希望它看起來像:Book1:author1,author2,author3等。 Book2:author1,author3等。? –
Nvm,明白了!感謝您的幫助隊友:) –