This commit is contained in:
h-seya
2026-09-25 16:36:29 +09:00
5 changed files with 90 additions and 23 deletions

View File

@@ -6,23 +6,55 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import com.example.demo.entity.Book; import com.example.demo.entity.Book;
import com.example.demo.form.BookSearchForm;
import com.example.demo.service.BookServiceImpl; import com.example.demo.service.BookServiceImpl;
@Controller @Controller
public class BookController { public class BookController {
@Autowired @Autowired
private BookServiceImpl bookService; private BookServiceImpl bookService;
/* 一覧表示 */
@GetMapping("/book") @GetMapping("/book")
public String book(Model model) { public String book(Model model) {
List<Book> aaa = bookService.findAll(); List<Book> aaa = bookService.findAll();
// DBから取得したデータをHTMLで扱えるようにする // DBから取得したデータをHTMLで扱えるようにする
model.addAttribute("book",bookService.findAll()); model.addAttribute("book", bookService.findAll());
model.addAttribute("bookSearchForm", new BookSearchForm());
return "book"; return "book";
} }
/* 検索処理 */
@GetMapping("/searchBooks")
public String searchBooks(@ModelAttribute BookSearchForm bookSearchForm,
Model model) {
model.addAttribute("book", bookService.findBooks(bookSearchForm.getKeyword()));
model.addAttribute("bookSearchForm", bookSearchForm);
return "book";
}
/* クリア処理 */
@PostMapping("/clearBookList")
public String clearBookList(Model model) {
model.addAttribute("book", bookService.findAll());
model.addAttribute("bookSearchForm", new BookSearchForm());
// 一覧表示画面に戻る
return "/book";
}
} }

View File

@@ -0,0 +1,10 @@
package com.example.demo.form;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class BookSearchForm {
private String keyword;
}

View File

@@ -1,10 +1,20 @@
package com.example.demo.repository; package com.example.demo.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import com.example.demo.entity.Book; import com.example.demo.entity.Book;
public interface BookRepository extends JpaRepository<Book, Integer>{ public interface BookRepository extends JpaRepository<Book, Integer>{
@Query("""
SELECT b FROM Book b
WHERE b.title LIKE %:keyword%
OR b.publisher LIKE %:keyword%
OR b.author LIKE %:keyword%
""")
List<Book> searchBooks(@Param("keyword")String keyword);
} }

View File

@@ -1,6 +1,5 @@
package com.example.demo.service; package com.example.demo.service;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -10,26 +9,17 @@ import com.example.demo.entity.Book;
import com.example.demo.repository.BookRepository; import com.example.demo.repository.BookRepository;
@Service @Service
public class BookServiceImpl{ public class BookServiceImpl {
@Autowired @Autowired
private BookRepository bookRepository; private BookRepository bookRepository;
// @Override
// public String booklist(com.example.demo.entity.Book book) {
// Book booklist = new Book();
// booklist.setBookId(book.getBookId());
//
// return null;
// }
public List<Book> findAll() { public List<Book> findAll() {
return bookRepository.findAll(); return bookRepository.findAll();
} }
public List<Book> findBooks(String keyword){
return bookRepository.searchBooks(keyword);
}
} }

View File

@@ -12,6 +12,27 @@
<h1>社内図書一覧</h1> <h1>社内図書一覧</h1>
<div class="float-end"> <div class="float-end">
</div> </div>
<form class="input-group" th:action="@{/searchBooks}" th:object="${bookSearchForm}" method="get">
<input type="text" th:field="*{keyword}" class="form-control" placeholder="キーワードを入力">
<button class="btn btn-outline-success" type="submit">
検索
</button>
</form>
<form method="post" th:action="@{/clearBookList}">
<button class="button button-secondary" type="submit">
クリア
</button>
</form>
<p>※「タイトル」「出版社」「著者名」で検索</p>
<form method="get" action="bookLend"> <form method="get" action="bookLend">
<table class="table table-striped"> <table class="table table-striped">
@@ -68,7 +89,7 @@
貸出 貸出
</button> </button>
</form> </form>
<form method="get" action="/updateconfirm"> <form method="post" action="/updateconfirm">
<input type="hidden" name="bookManageId" th:value="*{book.bookManageId}"> <input type="hidden" name="bookManageId" th:value="*{book.bookManageId}">
<button type="submit">更新</button> <button type="submit">更新</button>
</form> </form>
@@ -77,8 +98,12 @@
<button type="submit">削除</button> <button type="submit">削除</button>
</form> </form>
</td> </td>
</tr>
<tr th:if="${#lists.isEmpty(book)}">
<td colspan="15">
該当する図書がありません
</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>