Spring 在JPA+THYMELEAF中按标题查找
我的数据库中有一个多行列表,但我的查询只返回一行,而不是更多行 我想按标题查找我的书籍,但例如,当我有多本书的标题中有相同的单词时,我想显示这两本书,但查询不会返回它 HTML页面 图书管理员 书库 图书服务 控制台日志 在控制台中,我看到3行搜索到的单词,但在我的网页上,我只看到1行。我想我的控制器model.addAttribute可能有问题 原因可能是什么 我已经修改了控制器:Spring 在JPA+THYMELEAF中按标题查找,spring,spring-boot,spring-mvc,thymeleaf,Spring,Spring Boot,Spring Mvc,Thymeleaf,我的数据库中有一个多行列表,但我的查询只返回一行,而不是更多行 我想按标题查找我的书籍,但例如,当我有多本书的标题中有相同的单词时,我想显示这两本书,但查询不会返回它 HTML页面 图书管理员 书库 图书服务 控制台日志 在控制台中,我看到3行搜索到的单词,但在我的网页上,我只看到1行。我想我的控制器model.addAttribute可能有问题 原因可能是什么 我已经修改了控制器: @GetMapping({"/searchbook","/searchbook{title}"}) public
@GetMapping({"/searchbook","/searchbook{title}"})
public String searchBookByTitle(@ModelAttribute("title") @RequestParam("title") Optional<String> title, Model model,Book book){
System.out.println(title);
if(title.isPresent()) {
List<Book> bookList = bookService.findBookByTitle(title.get());
model.addAttribute("book",book);
model.addAttribute("book2", bookList);
return "book/searchbook";
}
else
{
return "book/searchbook";}
}
还有百里香
2. Thymeleaf code
<form action="#" th:action="@{/searchbook(param=${book?.getBookTitle()})}" th:object="${book}" >
<label for="title">Find Book by Title</label>
<input type="text" id="title" name="title" th:value="${book?.getBookTitle()}" placeholder="Book Title"/>
<input type="submit" value="Search"/>
<table id="table1" th:object="${book2}">
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
<th>Copies</th>
</tr>
<tr th:each="carte, state :${book2}" id="tr1"
th:classappend="${state.odd}?'odd-row':'even-row'">
<td th:text="${state.count}">id</td>
<td th:text="${carte.bookTitle}">title</td>
<td th:text="${carte.bookAuthor}">author</td>
<td th:text="${carte.bookCopies}">copies</td>
</tr>
</table>
<!-- <a th:href="@{/update(idNumber=${reader?.idNumber},firstname=${reader?.firstname})}" th:field="*{firstname}">Update</a>
<a th:href="@{/delete(idNumber=${reader?.idNumber})}" th:value="${reader?.idNumber}">Delete</a>
-->
</form>
您在模型中只存储了一本书,或者更确切地说,您存储了所有的书,但最后一本书覆盖了前一本书。如果要显示书籍列表,则模型必须包含。。。书籍列表:model.addAttributebooks、bookList;非常感谢。我已经决定了。
@Repository
public interface BookRepository extends CrudRepository<Book,Long> {
@Query(value = "SELECT * FROM BOOK where book_title like %:bookTitle%",nativeQuery = true)
List<Book> findBooksByBookTitle(String bookTitle);
}
@Override
public List<Book> findBookByTitle(String title) {
List<Book> books = new ArrayList<>();
bookRepository.findBooksByBookTitle(title)
.forEach(books::add);
for(Book book:books)
System.out.println(book);
return books;
}
2019-10-13 20:50:44.879 DEBUG 16322 --- [nio-8080-exec-6] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public java.lang.String vleunti.springbootframework.libraryapp.controllers.BookController.searchBookByTitle(java.util.Optional<java.lang.String>,org.springframework.ui.Model)
Optional[Amintiri]
2019-10-13 20:50:44.880 DEBUG 16322 --- [nio-8080-exec-6] org.hibernate.SQL : SELECT * FROM BOOK where book_title like ?
Book{id=1, bookTitle='Amintiri din Copilarie', bookAuthor='Ion Creanga', bookCopies=3}
Book{id=2, bookTitle='Amintiri de Acasa', bookAuthor='Victor Leunti', bookCopies=1}
Book{id=3, bookTitle='Amintiri de altadata', bookAuthor='Vasea Mure', bookCopies=1}
@GetMapping({"/searchbook","/searchbook{title}"})
public String searchBookByTitle(@ModelAttribute("title") @RequestParam("title") Optional<String> title, Model model,Book book){
System.out.println(title);
if(title.isPresent()) {
List<Book> bookList = bookService.findBookByTitle(title.get());
model.addAttribute("book",book);
model.addAttribute("book2", bookList);
return "book/searchbook";
}
else
{
return "book/searchbook";}
}
2. Thymeleaf code
<form action="#" th:action="@{/searchbook(param=${book?.getBookTitle()})}" th:object="${book}" >
<label for="title">Find Book by Title</label>
<input type="text" id="title" name="title" th:value="${book?.getBookTitle()}" placeholder="Book Title"/>
<input type="submit" value="Search"/>
<table id="table1" th:object="${book2}">
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
<th>Copies</th>
</tr>
<tr th:each="carte, state :${book2}" id="tr1"
th:classappend="${state.odd}?'odd-row':'even-row'">
<td th:text="${state.count}">id</td>
<td th:text="${carte.bookTitle}">title</td>
<td th:text="${carte.bookAuthor}">author</td>
<td th:text="${carte.bookCopies}">copies</td>
</tr>
</table>
<!-- <a th:href="@{/update(idNumber=${reader?.idNumber},firstname=${reader?.firstname})}" th:field="*{firstname}">Update</a>
<a th:href="@{/delete(idNumber=${reader?.idNumber})}" th:value="${reader?.idNumber}">Delete</a>
-->
</form>