Spring Boot 在JPA+THYMELEAF中按标题查找

zi8p0yeb  于 2023-01-05  发布在  Spring
关注(0)|答案(2)|浏览(118)

我的数据库中有一个包含多行的列表,但查询只返回1行,而不是更多行。
我想按标题查找我的书,但例如,当我有多本书的标题中有相同的单词时,我想显示这两本书,但查询没有返回它。

    • HTML页面**
<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">

          <tr>
             <th>ID</th>
             <th>Title</th>
             <th>Author</th>
             <th>Copies</th>
          </tr>

          <tr th:each="re:${book}" id="tr1">
               <td th:text="${book?.getId()}">id</td>
               <td th:text="${book?.getBookTitle()}" id="ln">title</td>
               <td th:text="${book?.getBookAuthor()}">author</td>
               <td th:text="${book.getBookCopies()}">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>
    • 图书管理员**
@GetMapping({"/searchbook","/searchbook{title}"})
public String searchBookByTitle(@RequestParam("title") Optional<String> title, Model model){

    if(title.isPresent()) {
         List<Book> bookList = bookService.findBookByTitle(title.get());
         for(Book book1:bookList) {
              model.addAttribute("book", book1);
         }
         return "book/searchbook";
    }
    else
    {
         return "book/searchbook";
    }
}
    • 书库**
@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}

在控制台中,我看到3行与搜索的单词,但在我的网页上,我只看到1行。我猜有什么问题,我的控制器,与model.addAttribute ...
可能是什么原因呢?

fbcarpbf

fbcarpbf1#

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>
mec1mxoz

mec1mxoz2#

对于th:action,你不能只是把(parm=$..)放在那里来检查参数,你必须先定义它,这样看起来就像:

<form action="#" th:action="@{/searchbook{param}(param=${BookTitle})}" th:object="${book}" >

    <label for="BookTitle">Find Book by Title</label>
    <input type="text" id="BookTitle" name="BookTitle" th:value="${BookTitle}" 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>

相关问题