From 49c40a05c468c28b0990461a3b70410210a5e321 Mon Sep 17 00:00:00 2001 From: w-yuki Date: Mon, 28 Sep 2026 14:53:31 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[add]=E7=B5=9E=E3=82=8A=E8=BE=BC=E3=81=BF?= =?UTF-8?q?=E6=A9=9F=E8=83=BD=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/controller/BookInsertController.java | 6 ++-- src/main/resources/templates/bookInsert.html | 35 ++++++++++++------- .../templates/bookInsertConfirm.html | 2 +- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/example/demo/controller/BookInsertController.java b/src/main/java/com/example/demo/controller/BookInsertController.java index e3d5b2f..f1b903a 100644 --- a/src/main/java/com/example/demo/controller/BookInsertController.java +++ b/src/main/java/com/example/demo/controller/BookInsertController.java @@ -1,7 +1,5 @@ package com.example.demo.controller; -import java.util.ArrayList; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -51,7 +49,7 @@ private StorageLocationRepository storageLocationRepository; model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); //そのカテゴリに属するサブカテゴリを持ってくる } else{ - model.addAttribute("subCategoryList",new ArrayList<>()); + model.addAttribute("subCategoryList",subCategoryRepository.findAll()); } model.addAttribute("locationList",storageLocationRepository.findAll()); model.addAttribute("bookInsert", bookInsert); @@ -71,7 +69,7 @@ private StorageLocationRepository storageLocationRepository; if(bookInsert.getBookCategoryId() != null) { model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); } else{ - model.addAttribute("subCategoryList",new ArrayList<>()); + model.addAttribute("subCategoryList",subCategoryRepository.findAll()); } model.addAttribute("locationList",storageLocationRepository.findAll()); model.addAttribute("bookInsert",bookInsert); diff --git a/src/main/resources/templates/bookInsert.html b/src/main/resources/templates/bookInsert.html index be4be18..9ca1fdc 100644 --- a/src/main/resources/templates/bookInsert.html +++ b/src/main/resources/templates/bookInsert.html @@ -8,10 +8,11 @@

新規図書登録入力

-

-

新しく図書を登録します。必要事項を入力し、「登録」ボタンを押してください

+
+

新しく図書を登録します。必要事項を入力し、「登録」ボタンを押してください。

※は必須項目です -

+
+

図書情報

@@ -19,17 +20,23 @@ -

+
+ ※例:BK-1、BK-2... +
+




- -
+ +
+ ※ハイフン(4つ)を含む形式で入力してください +
+
-

+

@@ -50,30 +57,34 @@


- +
+



- +
+



- + From 89171b8561a6c814bd3f478f076382ae1506fa7b Mon Sep 17 00:00:00 2001 From: w-yuki Date: Tue, 29 Sep 2026 09:52:36 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[edit]=E6=96=B0=E8=A6=8F=E5=9B=B3=E6=9B=B8?= =?UTF-8?q?=E7=99=BB=E9=8C=B2=E3=83=9D=E3=83=83=E3=83=97=E3=82=A2=E3=83=83?= =?UTF-8?q?=E3=83=97=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/controller/BookInsertController.java | 17 +++++++++---- .../com/example/demo/entity/BookInsert.java | 5 ++-- .../demo/repository/BookInsertRepository.java | 6 ++++- .../demo/service/BookInsertService.java | 3 ++- .../demo/service/BookInsertServiceImpl.java | 21 ++++++++++------ src/main/resources/templates/bookInsert.html | 14 ++++------- .../templates/bookInsertConfirm.html | 24 ------------------- 7 files changed, 40 insertions(+), 50 deletions(-) diff --git a/src/main/java/com/example/demo/controller/BookInsertController.java b/src/main/java/com/example/demo/controller/BookInsertController.java index f1b903a..9d3faf8 100644 --- a/src/main/java/com/example/demo/controller/BookInsertController.java +++ b/src/main/java/com/example/demo/controller/BookInsertController.java @@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.PostMapping; import com.example.demo.entity.BookInsert; import com.example.demo.repository.BookCategoryRepository; +import com.example.demo.repository.BookInsertRepository; import com.example.demo.repository.StorageLocationRepository; import com.example.demo.repository.SubCategoryRepository; import com.example.demo.service.BookInsertService; @@ -29,8 +30,13 @@ private SubCategoryRepository subCategoryRepository; @Autowired private StorageLocationRepository storageLocationRepository; +@Autowired +private BookInsertRepository bookInsertRepository; + @GetMapping("/bookInsert") public String bookInsert(Model model) { + model.addAttribute("nextBookManageId",bookInsertService.createBookManageId()); + model.addAttribute("bookInsert",new BookInsert()); model.addAttribute("categoryList",categoryRepository.findAll()); model.addAttribute("subCategoryList",subCategoryRepository.findAll()); model.addAttribute("locationList",storageLocationRepository.findAll()); @@ -44,6 +50,7 @@ private StorageLocationRepository storageLocationRepository; @PostMapping("/bookInsertConfirm") public String bookInsertConfirm(@Validated BookInsert bookInsert,BindingResult result,Model model) { if(result.hasErrors()) { + model.addAttribute("nextBookManageId",bookInsertService.createBookManageId()); model.addAttribute("categoryList",categoryRepository.findAll()); if(bookInsert.getBookCategoryId() != null) { //nullじゃなかったら model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); @@ -56,7 +63,6 @@ private StorageLocationRepository storageLocationRepository; return "bookInsert"; } - System.out.println(bookInsert); bookInsertService.save(bookInsert); model.addAttribute("bookInsert",bookInsert); return "bookInsertConfirm"; @@ -65,8 +71,9 @@ private StorageLocationRepository storageLocationRepository; //確認画面から「もどる」ボタンを押したら入力画面に戻る @PostMapping("/bookInsert") public String bookInsertBack(BookInsert bookInsert,Model model) { + model.addAttribute("nextBookManageId",bookInsertService.createBookManageId()); model.addAttribute("categoryList",categoryRepository.findAll()); - if(bookInsert.getBookCategoryId() != null) { + if(bookInsert.getBookCategoryId() !=null) { model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); } else{ model.addAttribute("subCategoryList",subCategoryRepository.findAll()); @@ -79,6 +86,7 @@ private StorageLocationRepository storageLocationRepository; @PostMapping("/bookInsert/category") public String changeCategory(BookInsert bookInsert,Model model) { //カテゴリプルダウンに表示するデータを取得 + model.addAttribute("nextBookManageId",bookInsertService.createBookManageId()); model.addAttribute("categoryList",categoryRepository.findAll()); model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); model.addAttribute("locationList",storageLocationRepository.findAll()); @@ -87,9 +95,8 @@ private StorageLocationRepository storageLocationRepository; return "bookInsert"; } +} - - } //画面で選択したカテゴリbookInsert.getBookCategoryId()に選択した結果をいれる @@ -97,6 +104,6 @@ private StorageLocationRepository storageLocationRepository; //「カテゴリIDが1のサブカテゴリを取ってくる」 //model.addAttribute("subCategoryList",取得結果) - + diff --git a/src/main/java/com/example/demo/entity/BookInsert.java b/src/main/java/com/example/demo/entity/BookInsert.java index be1fd47..2e5d442 100644 --- a/src/main/java/com/example/demo/entity/BookInsert.java +++ b/src/main/java/com/example/demo/entity/BookInsert.java @@ -28,8 +28,7 @@ public class BookInsert { @Column(name = "title") private String title; - @Pattern(regexp = "^(?=(?:.*-){4})[0-9-]{17}$", - message = "ISBNは半角数字(13文字)とハイフン(4つ)を含めて入力してください") + @Pattern(regexp = "^(?=(?:.*-){4})[0-9-]{17}$",message = "ISBNは半角数字(13文字)とハイフン(4つ)を含めて入力してください") @Column(name = "isbn") private String isbn; @@ -69,7 +68,7 @@ public class BookInsert { @Column(name = "book_approval") private Integer bookApproval; - @Transient //DB保存無効 + @Transient private Integer bookCategoryId; diff --git a/src/main/java/com/example/demo/repository/BookInsertRepository.java b/src/main/java/com/example/demo/repository/BookInsertRepository.java index 16ab016..7efc619 100644 --- a/src/main/java/com/example/demo/repository/BookInsertRepository.java +++ b/src/main/java/com/example/demo/repository/BookInsertRepository.java @@ -3,9 +3,13 @@ package com.example.demo.repository; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; import com.example.demo.entity.BookInsert; +@Repository public interface BookInsertRepository extends JpaRepository { -} \ No newline at end of file + BookInsert findFirstByOrderByBookIdDesc();//bookIDが一番大きいデータを取得 + + } \ No newline at end of file diff --git a/src/main/java/com/example/demo/service/BookInsertService.java b/src/main/java/com/example/demo/service/BookInsertService.java index 51a4f71..101512e 100644 --- a/src/main/java/com/example/demo/service/BookInsertService.java +++ b/src/main/java/com/example/demo/service/BookInsertService.java @@ -8,4 +8,5 @@ public interface BookInsertService { String regist(BookInsert book); List findAll(); void save(BookInsert bookInsert); -} \ No newline at end of file + String createBookManageId(); + } \ No newline at end of file diff --git a/src/main/java/com/example/demo/service/BookInsertServiceImpl.java b/src/main/java/com/example/demo/service/BookInsertServiceImpl.java index d8e6c32..7e7f51c 100644 --- a/src/main/java/com/example/demo/service/BookInsertServiceImpl.java +++ b/src/main/java/com/example/demo/service/BookInsertServiceImpl.java @@ -12,9 +12,17 @@ import com.example.demo.repository.BookInsertRepository; @Service public class BookInsertServiceImpl implements BookInsertService { - @Autowired - private BookInsertRepository bookInsertRepository; + @Autowired + private BookInsertRepository bookInsertRepository; + @Override + public String createBookManageId() { //次に表示する図書管理IDを作る + BookInsert book =bookInsertRepository.findFirstByOrderByBookIdDesc(); + if(book ==null){ + return "BK-1";} + return "BK-"+(book.getBookId()+1); //いまのIDの次のIDを返す + } + @Override public String regist(BookInsert book) { // DTO → エンティティへ詰め替えて保存 @@ -33,15 +41,14 @@ import com.example.demo.repository.BookInsertRepository; bookInsert.setIsDeleted(0);//削除フラグ設定 bookInsert.setBookApproval(0);//承認フラグ(承認されていないため0) bookInsertRepository.save(bookInsert); - //BK〇〇〇〇形式で管理IDを作る - String manageId="BK-" + bookInsert.getBookId();//BookId取得→BK形式に変換 + //BK-〇形式で管理IDを作る + String manageId="BK-"+bookInsert.getBookId();//BookId取得→BK形式に変換 bookInsert.setBookManageId(manageId); bookInsertRepository.save(bookInsert);//データベースに保存 } - } - - + } // ※BookService インタフェースにも List findAll(); を追加すること + \ No newline at end of file diff --git a/src/main/resources/templates/bookInsert.html b/src/main/resources/templates/bookInsert.html index 9ca1fdc..c25f0b7 100644 --- a/src/main/resources/templates/bookInsert.html +++ b/src/main/resources/templates/bookInsert.html @@ -17,12 +17,10 @@
- -
- ※例:BK-1、BK-2... -
+ +
+ ※例:BK-1、BK-2... +


@@ -109,11 +107,9 @@

--> + - - - \ No newline at end of file diff --git a/src/main/resources/templates/bookInsertConfirm.html b/src/main/resources/templates/bookInsertConfirm.html index 3bab11d..ab920fa 100644 --- a/src/main/resources/templates/bookInsertConfirm.html +++ b/src/main/resources/templates/bookInsertConfirm.html @@ -9,30 +9,6 @@

登録内容確認

- - -
- - - - - - - - - - - - - - -
- - -
- -
-