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,8 +6,11 @@ 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
|
||||||
@@ -15,6 +18,7 @@ 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) {
|
||||||
|
|
||||||
@@ -22,7 +26,35 @@ public class BookController {
|
|||||||
|
|
||||||
// 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";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
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;
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -15,21 +14,12 @@ 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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
Reference in New Issue
Block a user