2017-08-16 238 views
0

在SpringBoot中創建圖書數據庫應用程序。目前我試圖讓我的控制器返回書籍表的所有字段。它與作者表ManyToMany關係(與第三個表稱爲書籍作者)。問題是,我得到這樣的輸出:從多對多關係中返回值

Title Title original Premiere date Authors 
Harry Potter 1 Harry Potter One 2017-08-02 [[email protected], [email protected] 

,我希望回到的姓作者的值。

這是我的控制方法是如何的樣子:

@RequestMapping(value = "/{id}", method = RequestMethod.GET) 
public ModelAndView oneBook(@PathVariable int id) { 
    Book book = bookRepository.findOne(id); 
    bookRepository.setCounterForBook(id); 
    Publisher publisher = book.getPublisher(); 
    Set<Author> authors = book.getAuthors(); 

    Map<String, Object> model = new HashMap<String, Object>(); 
    model.put("book", book); 
    model.put("publisher", publisher); 
    model.put("authors", authors); 
    return new ModelAndView("books/one", "model", model); 
} 

這是我的書(實體模型)類:

@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>(); 

Thymeleaf的模板:

<!DOCTYPE HTML> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head> 
    <title>Bookweb</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 

    <!--<link rel="stylesheet" type="text/css" 
      href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" />--> 

    <link th:href="@{/static/bootstrap.css}" rel="stylesheet" /> 

</head> 
<body> 
<div th:replace="fragments/header :: header"/> 
<div class="container"> 
    <h2>Pojedyncza książka</h2> 
    <table> 
     <tr> 
      <th>Title</th> 
      <th>Title original</th> 
      <th>Premiere date</th> 
      <th>Authors</th> 
     </tr> 
     <tr> 
      <td th:text="${model.book.title}"></td> 
      <td th:text="${model.book.titleOriginal}"></td> 
      <td th:text="${model.book.premiereDate}"></td> 
      <td th:text="${model.book.authors}"></td> 
     </tr> 
    </table> 
</div> 
<script type="text/javascript" 
     src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script> 

<div th:replace="fragments/footer :: footer"/> 
</body> 
</html> 
+0

你可以分享你的JSP文件?或者你想讓它休息? –

+0

我正在使用Thymeleaf,在帖子中添加了模板。 –

+2

你需要循環你的作者 –

回答

1

要正確查看作者,只需在Thymeleaf t中創建foreach循環emplate:

 <h4 th:text="${model.book.titleOriginal}" style="color:slategrey"></h4> 
     <a th:each="a: ${model.authors}" th:href="@{/authors/}+${a.authorId}"><h5 th:text="${a.name}+' '+${a.surname}" style="color:#337ab7"></h5></a> 
      <small th:text="${'Views: '+model.book.counter}" style="color:#337ab7"> </small> 

     <h4 style="margin-top:30px;">Publisher:</h4> 
     <h6 th:text="${model.publisher.name}" style="margin-top:0px;"></h6> 
+0

這將產生無效的HTML。 – holmis83

+0

它僅用於測試目的,重要的是它獲取我想要的數據。但我會在一秒鐘內爲你解決,我的朋友。 –