Merge branch 's-tajihi' of https://gitea.it-college.co.jp/2026_09_5H_Spring_KIZAKI/ShareBooks_new into h-seya
This commit is contained in:
@@ -6,23 +6,55 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
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.form.BookSearchForm;
|
||||
import com.example.demo.service.BookServiceImpl;
|
||||
|
||||
@Controller
|
||||
public class BookController {
|
||||
@Autowired
|
||||
private BookServiceImpl bookService;
|
||||
|
||||
|
||||
/* 一覧表示 */
|
||||
@GetMapping("/book")
|
||||
public String book(Model model) {
|
||||
|
||||
|
||||
List<Book> aaa = bookService.findAll();
|
||||
|
||||
|
||||
// DBから取得したデータをHTMLで扱えるようにする
|
||||
model.addAttribute("book",bookService.findAll());
|
||||
model.addAttribute("book", bookService.findAll());
|
||||
|
||||
model.addAttribute("bookSearchForm", new BookSearchForm());
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
10
src/main/java/com/example/demo/form/BookSearchForm.java
Normal file
10
src/main/java/com/example/demo/form/BookSearchForm.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.example.demo.form;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class BookSearchForm {
|
||||
private String keyword;
|
||||
}
|
||||
@@ -1,10 +1,20 @@
|
||||
package com.example.demo.repository;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.example.demo.service;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -10,26 +9,17 @@ import com.example.demo.entity.Book;
|
||||
import com.example.demo.repository.BookRepository;
|
||||
|
||||
@Service
|
||||
public class BookServiceImpl{
|
||||
|
||||
public class BookServiceImpl {
|
||||
|
||||
@Autowired
|
||||
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() {
|
||||
return bookRepository.findAll();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<Book> findBooks(String keyword){
|
||||
return bookRepository.searchBooks(keyword);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,6 +12,27 @@
|
||||
<h1>社内図書一覧</h1>
|
||||
<div class="float-end">
|
||||
</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">
|
||||
|
||||
<table class="table table-striped">
|
||||
@@ -68,7 +89,7 @@
|
||||
貸出
|
||||
</button>
|
||||
</form>
|
||||
<form method="get" action="/updateconfirm">
|
||||
<form method="post" action="/updateconfirm">
|
||||
<input type="hidden" name="bookManageId" th:value="*{book.bookManageId}">
|
||||
<button type="submit">更新</button>
|
||||
</form>
|
||||
@@ -77,8 +98,12 @@
|
||||
<button type="submit">削除</button>
|
||||
</form>
|
||||
</td>
|
||||
|
||||
|
||||
</tr>
|
||||
|
||||
<tr th:if="${#lists.isEmpty(book)}">
|
||||
<td colspan="15">
|
||||
該当する図書がありません
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
Reference in New Issue
Block a user