@k2works/claude-code-booster 3.2.0 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/assets/docs/article/index.md +4 -1
- package/lib/assets/docs/article/practical-database-design/index.md +121 -0
- package/lib/assets/docs/article/practical-database-design/part1/chapter01.md +288 -0
- package/lib/assets/docs/article/practical-database-design/part1/chapter02.md +518 -0
- package/lib/assets/docs/article/practical-database-design/part1/chapter03.md +557 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter04.md +924 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter05.md +1627 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter06.md +2716 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter07.md +2082 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter08.md +2105 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter09.md +2031 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter10.md +1387 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter11.md +1677 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter12.md +1417 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter13.md +1434 -0
- package/lib/assets/docs/article/practical-database-design/part3/chapter14.md +667 -0
- package/lib/assets/docs/article/practical-database-design/part3/chapter15.md +1625 -0
- package/lib/assets/docs/article/practical-database-design/part3/chapter16.md +1915 -0
- package/lib/assets/docs/article/practical-database-design/part3/chapter17.md +1708 -0
- package/lib/assets/docs/article/practical-database-design/part3/chapter18.md +2095 -0
- package/lib/assets/docs/article/practical-database-design/part3/chapter19.md +1123 -0
- package/lib/assets/docs/article/practical-database-design/part3/chapter20.md +1031 -0
- package/lib/assets/docs/article/practical-database-design/part3/chapter21.md +1382 -0
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter14-orm.md +991 -0
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter15-orm.md +1300 -0
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter16-orm.md +1166 -0
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter17-orm.md +1584 -0
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter18-orm.md +1183 -0
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter19-orm.md +1016 -0
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter20-orm.md +1753 -0
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter21-orm.md +1447 -0
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter22-orm.md +1878 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter22.md +965 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter23.md +2069 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter24.md +2439 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter25.md +3661 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter26.md +2916 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter27.md +3105 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter28.md +2697 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter29.md +2544 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter30.md +2180 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter31.md +1192 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter32.md +2101 -0
- package/lib/assets/docs/article/practical-database-design/part5/chapter33.md +1032 -0
- package/lib/assets/docs/article/practical-database-design/part5/chapter34.md +1609 -0
- package/lib/assets/docs/article/practical-database-design/part5/chapter35.md +1453 -0
- package/lib/assets/docs/article/practical-database-design/part5/chapter36.md +1292 -0
- package/lib/assets/docs/article/practical-database-design/part5/chapter37.md +1470 -0
- package/lib/assets/docs/article/practical-database-design/part5/chapter38.md +1698 -0
- package/lib/assets/docs/article/practical-database-design/part5/chapter39.md +2334 -0
- package/lib/assets/docs/article/practical-database-design/study/study2-1.md +1693 -0
- package/lib/assets/docs/article/practical-database-design/study/study2-2.md +1347 -0
- package/lib/assets/docs/article/practical-database-design/study/study2-3.md +2044 -0
- package/lib/assets/docs/article/practical-database-design/study/study2-4.md +2229 -0
- package/lib/assets/docs/article/practical-database-design/study/study2-5.md +2418 -0
- package/lib/assets/docs/article/practical-database-design/study/study3-1.md +2205 -0
- package/lib/assets/docs/article/practical-database-design/study/study3-2.md +2221 -0
- package/lib/assets/docs/article/practical-database-design/study/study3-3.md +2253 -0
- package/lib/assets/docs/article/practical-database-design/study/study3-4.md +2106 -0
- package/lib/assets/docs/article/practical-database-design/study/study3-5.md +2507 -0
- package/lib/assets/docs/article/practical-database-design/study/study4-1.md +2587 -0
- package/lib/assets/docs/article/practical-database-design/study/study4-2.md +2075 -0
- package/lib/assets/docs/article/practical-database-design/study/study4-3.md +1805 -0
- package/lib/assets/docs/article/practical-database-design/study/study4-4.md +1895 -0
- package/lib/assets/docs/article/practical-database-design/study/study4-5.md +2878 -0
- package/lib/assets/docs/reference//351/201/213/345/226/266/347/256/241/347/220/206.md +131 -39
- package/package.json +1 -1
|
@@ -0,0 +1,2587 @@
|
|
|
1
|
+
# 実践データベース設計:生産管理システム 研究 1 - モノリスサービスの実装
|
|
2
|
+
|
|
3
|
+
## はじめに
|
|
4
|
+
|
|
5
|
+
本研究では、API サーバー構成(第32章)とは異なるアプローチとして、**モノリスアーキテクチャ**による生産管理システムを実装します。UI(テンプレートエンジン)、ビジネスロジック、データベースアクセスがすべて同一サーバー内で動作する、伝統的かつ堅実なアーキテクチャです。
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 第33章:モノリスアーキテクチャの基礎
|
|
10
|
+
|
|
11
|
+
### 33.1 モノリスアーキテクチャとは
|
|
12
|
+
|
|
13
|
+
**モノリス(Monolith / Monolithic Architecture)**とは、「API」と「UI」と「ビジネスロジック」が **1つの実行ユニット**(1つのプロジェクトやバイナリ)にまとまっているアーキテクチャを指します。
|
|
14
|
+
|
|
15
|
+
```plantuml
|
|
16
|
+
@startuml monolith_architecture
|
|
17
|
+
!define RECTANGLE class
|
|
18
|
+
|
|
19
|
+
title モノリスアーキテクチャ
|
|
20
|
+
|
|
21
|
+
package "モノリスサーバー (単一の実行ユニット)" {
|
|
22
|
+
|
|
23
|
+
package "プレゼンテーション層" {
|
|
24
|
+
[Controller]
|
|
25
|
+
[Thymeleaf テンプレート]
|
|
26
|
+
[静的リソース (CSS/JS)]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
package "ビジネスロジック層" {
|
|
30
|
+
[Application Service]
|
|
31
|
+
[Domain Model]
|
|
32
|
+
[Domain Service]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
package "データアクセス層" {
|
|
36
|
+
[Repository]
|
|
37
|
+
[MyBatis Mapper]
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
database "PostgreSQL" as DB
|
|
42
|
+
|
|
43
|
+
actor "ブラウザ" as Browser
|
|
44
|
+
|
|
45
|
+
Browser --> [Controller] : HTTP リクエスト
|
|
46
|
+
[Controller] --> [Thymeleaf テンプレート] : モデル渡し
|
|
47
|
+
[Thymeleaf テンプレート] --> Browser : HTML レスポンス
|
|
48
|
+
[Controller] --> [Application Service]
|
|
49
|
+
[Application Service] --> [Domain Model]
|
|
50
|
+
[Application Service] --> [Repository]
|
|
51
|
+
[Repository] --> [MyBatis Mapper]
|
|
52
|
+
[MyBatis Mapper] --> DB
|
|
53
|
+
|
|
54
|
+
note right of "モノリスサーバー (単一の実行ユニット)"
|
|
55
|
+
すべてが1つのプロセスで動作
|
|
56
|
+
- 同一JVMで実行
|
|
57
|
+
- 同一デプロイユニット
|
|
58
|
+
- 密結合だが開発・運用がシンプル
|
|
59
|
+
end note
|
|
60
|
+
|
|
61
|
+
@enduml
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
**モノリスの主な特徴:**
|
|
65
|
+
|
|
66
|
+
| 特徴 | 説明 |
|
|
67
|
+
|------|------|
|
|
68
|
+
| **単一デプロイ** | アプリケーション全体が1つのアーティファクト(JAR/WAR)としてデプロイ |
|
|
69
|
+
| **同一プロセス** | UI、ビジネスロジック、データアクセスが同じ JVM で動作 |
|
|
70
|
+
| **テンプレートエンジン** | サーバーサイドで HTML を生成してブラウザに返却 |
|
|
71
|
+
| **セッション管理** | サーバーサイドでユーザーセッションを管理 |
|
|
72
|
+
| **トランザクション境界** | 単一プロセス内でのローカルトランザクション |
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
### 33.2 API サーバー vs モノリス
|
|
77
|
+
|
|
78
|
+
```plantuml
|
|
79
|
+
@startuml api_vs_monolith
|
|
80
|
+
!define RECTANGLE class
|
|
81
|
+
|
|
82
|
+
left to right direction
|
|
83
|
+
|
|
84
|
+
package "API サーバー構成(疎結合)" {
|
|
85
|
+
package "フロントエンド" as FE {
|
|
86
|
+
[React/Vue/Angular]
|
|
87
|
+
[SPA]
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
package "バックエンド" as BE {
|
|
91
|
+
[REST API]
|
|
92
|
+
[JSON レスポンス]
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
database "DB1" as DB_API
|
|
96
|
+
|
|
97
|
+
FE --> BE : HTTP/JSON
|
|
98
|
+
BE --> DB_API
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
package "モノリス構成(密結合)" {
|
|
102
|
+
package "モノリスサーバー" as Mono {
|
|
103
|
+
[Controller + View]
|
|
104
|
+
[テンプレートエンジン]
|
|
105
|
+
[ビジネスロジック]
|
|
106
|
+
[データアクセス]
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
database "DB2" as DB_Mono
|
|
110
|
+
|
|
111
|
+
Mono --> DB_Mono
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
actor "ブラウザ (API)" as Browser1
|
|
115
|
+
actor "ブラウザ (Mono)" as Browser2
|
|
116
|
+
|
|
117
|
+
Browser1 --> FE
|
|
118
|
+
Browser2 --> Mono : HTTP (HTML)
|
|
119
|
+
|
|
120
|
+
@enduml
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
| 観点 | API サーバー構成 | モノリス構成 |
|
|
124
|
+
|------|-----------------|-------------|
|
|
125
|
+
| **結合度** | 疎結合(フロントエンドとバックエンドが分離) | 密結合(すべてが同一サーバー) |
|
|
126
|
+
| **フロントエンド** | SPA(React/Vue/Angular) | テンプレートエンジン(Thymeleaf) |
|
|
127
|
+
| **通信方式** | REST API(JSON) | サーバーサイドレンダリング(HTML) |
|
|
128
|
+
| **開発チーム** | フロント/バックエンドで分業可能 | フルスタックで開発 |
|
|
129
|
+
| **デプロイ** | 別々にデプロイ可能 | 単一アーティファクトをデプロイ |
|
|
130
|
+
| **スケーリング** | 個別にスケール可能 | 全体をスケール |
|
|
131
|
+
| **複雑さ** | API 設計・認証・CORS などが必要 | シンプル、設定が少ない |
|
|
132
|
+
| **初期開発速度** | 環境構築に時間がかかる | 素早く開発開始できる |
|
|
133
|
+
| **SEO** | SSR/SSG が必要な場合あり | サーバーサイドレンダリングで SEO フレンドリー |
|
|
134
|
+
|
|
135
|
+
#### モノリスを選択すべき場面
|
|
136
|
+
|
|
137
|
+
**モノリスが適している状況:**
|
|
138
|
+
|
|
139
|
+
1. **小〜中規模のチーム**:専門のフロントエンドチームがいない場合
|
|
140
|
+
2. **社内システム**:SEO 不要、限られたユーザー数
|
|
141
|
+
3. **業務アプリケーション**:複雑な業務フローをサーバーサイドで処理
|
|
142
|
+
4. **迅速な開発**:MVP やプロトタイプの素早い構築
|
|
143
|
+
5. **運用コスト重視**:インフラ構成をシンプルに保ちたい場合
|
|
144
|
+
|
|
145
|
+
**生産管理システムにおけるモノリスのメリット:**
|
|
146
|
+
|
|
147
|
+
```
|
|
148
|
+
生産管理システムの特性
|
|
149
|
+
├── 社内利用が中心 → SEO 不要、SPA 不要
|
|
150
|
+
├── 複雑な業務フロー → サーバーサイドで制御が容易
|
|
151
|
+
├── 帳票出力 → サーバーサイドで PDF/Excel 生成
|
|
152
|
+
├── 大量データ処理 → MRP などバッチ処理との親和性
|
|
153
|
+
└── 運用担当者のスキル → シンプルな構成が望ましい
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
### 33.3 モノリスにおけるヘキサゴナルアーキテクチャ
|
|
159
|
+
|
|
160
|
+
モノリスであっても、ヘキサゴナルアーキテクチャ(Ports and Adapters)を採用することで、保守性の高い設計を実現できます。
|
|
161
|
+
|
|
162
|
+
```plantuml
|
|
163
|
+
@startuml hexagonal_monolith
|
|
164
|
+
!define RECTANGLE class
|
|
165
|
+
|
|
166
|
+
package "Hexagonal Architecture (モノリス版)" {
|
|
167
|
+
|
|
168
|
+
RECTANGLE "Application Core\n(Domain + Use Cases)" as core {
|
|
169
|
+
- Item (品目)
|
|
170
|
+
- Bom (部品構成表)
|
|
171
|
+
- Order (オーダ)
|
|
172
|
+
- Stock (在庫)
|
|
173
|
+
- ItemUseCase
|
|
174
|
+
- OrderUseCase
|
|
175
|
+
- InventoryUseCase
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
RECTANGLE "Input Adapters\n(Driving Side)" as input {
|
|
179
|
+
- Thymeleaf Controller
|
|
180
|
+
- フォーム処理
|
|
181
|
+
- セッション管理
|
|
182
|
+
- リクエスト検証
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
RECTANGLE "Output Adapters\n(Driven Side)" as output {
|
|
186
|
+
- MyBatis Repository
|
|
187
|
+
- Database Access
|
|
188
|
+
- Entity Mapping
|
|
189
|
+
- 帳票出力
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
input --> core : "Input Ports\n(Use Cases)"
|
|
194
|
+
core --> output : "Output Ports\n(Repository Interfaces)"
|
|
195
|
+
|
|
196
|
+
note top of core
|
|
197
|
+
純粋なビジネスロジック
|
|
198
|
+
UI 技術(Thymeleaf)に依存しない
|
|
199
|
+
テスト可能な状態を維持
|
|
200
|
+
end note
|
|
201
|
+
|
|
202
|
+
note left of input
|
|
203
|
+
HTML フォームからの入力
|
|
204
|
+
サーバーサイドレンダリング
|
|
205
|
+
セッションベースの認証
|
|
206
|
+
end note
|
|
207
|
+
|
|
208
|
+
note right of output
|
|
209
|
+
PostgreSQL + MyBatis
|
|
210
|
+
帳票生成(PDF/Excel)
|
|
211
|
+
ファイル出力
|
|
212
|
+
end note
|
|
213
|
+
|
|
214
|
+
@enduml
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
**モノリスでもヘキサゴナルアーキテクチャを適用する理由:**
|
|
218
|
+
|
|
219
|
+
1. **テスト容易性**:ドメインロジックを UI から分離してテスト可能
|
|
220
|
+
2. **将来の移行**:必要に応じて API 層を追加可能
|
|
221
|
+
3. **保守性**:関心の分離により変更影響を局所化
|
|
222
|
+
4. **再利用性**:ドメインロジックを複数の UI で共有可能
|
|
223
|
+
|
|
224
|
+
---
|
|
225
|
+
|
|
226
|
+
### 33.4 ディレクトリ構成
|
|
227
|
+
|
|
228
|
+
```
|
|
229
|
+
src/main/java/com/example/production/
|
|
230
|
+
├── domain/ # ドメイン層(API版と共通)
|
|
231
|
+
│ ├── model/
|
|
232
|
+
│ │ ├── item/
|
|
233
|
+
│ │ ├── bom/
|
|
234
|
+
│ │ ├── order/
|
|
235
|
+
│ │ └── inventory/
|
|
236
|
+
│ └── exception/
|
|
237
|
+
│
|
|
238
|
+
├── application/ # アプリケーション層(API版と共通)
|
|
239
|
+
│ ├── port/
|
|
240
|
+
│ │ ├── in/ # Input Port(ユースケース)
|
|
241
|
+
│ │ └── out/ # Output Port(リポジトリ)
|
|
242
|
+
│ └── service/
|
|
243
|
+
│
|
|
244
|
+
├── infrastructure/
|
|
245
|
+
│ ├── persistence/ # Output Adapter(DB実装)
|
|
246
|
+
│ │ ├── mapper/
|
|
247
|
+
│ │ └── repository/
|
|
248
|
+
│ └── web/ # Input Adapter(Web実装)
|
|
249
|
+
│ ├── controller/ # Thymeleaf Controller
|
|
250
|
+
│ ├── form/ # フォームオブジェクト
|
|
251
|
+
│ └── helper/ # ビューヘルパー
|
|
252
|
+
│
|
|
253
|
+
├── config/
|
|
254
|
+
│
|
|
255
|
+
└── resources/
|
|
256
|
+
├── templates/ # Thymeleaf テンプレート
|
|
257
|
+
│ ├── layout/ # 共通レイアウト
|
|
258
|
+
│ ├── items/ # 品目マスタ画面
|
|
259
|
+
│ ├── bom/ # BOM 画面
|
|
260
|
+
│ ├── orders/ # オーダ画面
|
|
261
|
+
│ └── inventory/ # 在庫画面
|
|
262
|
+
├── static/ # 静的リソース
|
|
263
|
+
│ ├── css/
|
|
264
|
+
│ ├── js/
|
|
265
|
+
│ └── images/
|
|
266
|
+
└── messages.properties # メッセージリソース
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
---
|
|
270
|
+
|
|
271
|
+
### 33.5 技術スタックの導入
|
|
272
|
+
|
|
273
|
+
<details>
|
|
274
|
+
<summary>build.gradle.kts</summary>
|
|
275
|
+
|
|
276
|
+
```kotlin
|
|
277
|
+
plugins {
|
|
278
|
+
id("java")
|
|
279
|
+
id("org.springframework.boot") version "3.2.0"
|
|
280
|
+
id("io.spring.dependency-management") version "1.1.4"
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
dependencies {
|
|
284
|
+
// Spring Boot Web(REST APIではなくMVCとして使用)
|
|
285
|
+
implementation("org.springframework.boot:spring-boot-starter-web")
|
|
286
|
+
implementation("org.springframework.boot:spring-boot-starter-validation")
|
|
287
|
+
|
|
288
|
+
// Thymeleaf(テンプレートエンジン)
|
|
289
|
+
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
|
|
290
|
+
implementation("nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:3.3.0")
|
|
291
|
+
|
|
292
|
+
// Thymeleaf + Spring Security(認証機能を使う場合)
|
|
293
|
+
implementation("org.thymeleaf.extras:thymeleaf-extras-springsecurity6")
|
|
294
|
+
|
|
295
|
+
// htmx(モダンなインタラクティブUI)
|
|
296
|
+
// CDN から読み込むため依存関係は不要
|
|
297
|
+
|
|
298
|
+
// MyBatis
|
|
299
|
+
implementation("org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3")
|
|
300
|
+
|
|
301
|
+
// PostgreSQL
|
|
302
|
+
runtimeOnly("org.postgresql:postgresql")
|
|
303
|
+
|
|
304
|
+
// Flyway
|
|
305
|
+
implementation("org.flywaydb:flyway-core")
|
|
306
|
+
implementation("org.flywaydb:flyway-database-postgresql")
|
|
307
|
+
|
|
308
|
+
// Webjars(Bootstrap等のフロントエンドライブラリ)
|
|
309
|
+
implementation("org.webjars:bootstrap:5.3.2")
|
|
310
|
+
implementation("org.webjars:webjars-locator-core:0.55")
|
|
311
|
+
|
|
312
|
+
// 帳票出力
|
|
313
|
+
implementation("org.apache.poi:poi-ooxml:5.2.5") // Excel
|
|
314
|
+
implementation("com.itextpdf:itext7-core:8.0.2") // PDF
|
|
315
|
+
|
|
316
|
+
// Test
|
|
317
|
+
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
|
318
|
+
testImplementation("org.testcontainers:postgresql:1.19.3")
|
|
319
|
+
testImplementation("org.testcontainers:junit-jupiter:1.19.3")
|
|
320
|
+
|
|
321
|
+
// Lombok
|
|
322
|
+
compileOnly("org.projectlombok:lombok")
|
|
323
|
+
annotationProcessor("org.projectlombok:lombok")
|
|
324
|
+
}
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
</details>
|
|
328
|
+
|
|
329
|
+
**パッケージの説明:**
|
|
330
|
+
|
|
331
|
+
| パッケージ | 用途 |
|
|
332
|
+
|-----------|------|
|
|
333
|
+
| spring-boot-starter-thymeleaf | サーバーサイドテンプレートエンジン |
|
|
334
|
+
| thymeleaf-layout-dialect | レイアウト継承(共通ヘッダー・フッター) |
|
|
335
|
+
| webjars:bootstrap | CSS フレームワーク(サーバーから配信) |
|
|
336
|
+
| poi-ooxml | Excel ファイル出力 |
|
|
337
|
+
| itext7-core | PDF ファイル出力 |
|
|
338
|
+
|
|
339
|
+
---
|
|
340
|
+
|
|
341
|
+
### 33.6 基本的な Spring Boot + Thymeleaf アプリケーション
|
|
342
|
+
|
|
343
|
+
<details>
|
|
344
|
+
<summary>ProductionManagementApplication.java</summary>
|
|
345
|
+
|
|
346
|
+
```java
|
|
347
|
+
package com.example.production;
|
|
348
|
+
|
|
349
|
+
import org.springframework.boot.SpringApplication;
|
|
350
|
+
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
351
|
+
|
|
352
|
+
@SpringBootApplication
|
|
353
|
+
public class ProductionManagementApplication {
|
|
354
|
+
|
|
355
|
+
public static void main(String[] args) {
|
|
356
|
+
SpringApplication.run(ProductionManagementApplication.class, args);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
</details>
|
|
362
|
+
|
|
363
|
+
<details>
|
|
364
|
+
<summary>application.yml</summary>
|
|
365
|
+
|
|
366
|
+
```yaml
|
|
367
|
+
spring:
|
|
368
|
+
datasource:
|
|
369
|
+
url: jdbc:postgresql://localhost:5432/production
|
|
370
|
+
username: postgres
|
|
371
|
+
password: postgres
|
|
372
|
+
|
|
373
|
+
thymeleaf:
|
|
374
|
+
cache: false # 開発時はキャッシュ無効
|
|
375
|
+
prefix: classpath:/templates/
|
|
376
|
+
suffix: .html
|
|
377
|
+
encoding: UTF-8
|
|
378
|
+
mode: HTML
|
|
379
|
+
|
|
380
|
+
messages:
|
|
381
|
+
basename: messages
|
|
382
|
+
encoding: UTF-8
|
|
383
|
+
|
|
384
|
+
mybatis:
|
|
385
|
+
mapper-locations: classpath:mapper/*.xml
|
|
386
|
+
configuration:
|
|
387
|
+
map-underscore-to-camel-case: true
|
|
388
|
+
|
|
389
|
+
server:
|
|
390
|
+
servlet:
|
|
391
|
+
session:
|
|
392
|
+
timeout: 30m # セッションタイムアウト
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
</details>
|
|
396
|
+
|
|
397
|
+
<details>
|
|
398
|
+
<summary>layout/default.html(共通レイアウト)</summary>
|
|
399
|
+
|
|
400
|
+
```html
|
|
401
|
+
<!DOCTYPE html>
|
|
402
|
+
<html xmlns:th="http://www.thymeleaf.org"
|
|
403
|
+
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
|
404
|
+
lang="ja">
|
|
405
|
+
<head>
|
|
406
|
+
<meta charset="UTF-8">
|
|
407
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
408
|
+
<title layout:title-pattern="$CONTENT_TITLE - $LAYOUT_TITLE">生産管理システム</title>
|
|
409
|
+
|
|
410
|
+
<!-- Bootstrap CSS -->
|
|
411
|
+
<link rel="stylesheet" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}">
|
|
412
|
+
|
|
413
|
+
<!-- カスタム CSS -->
|
|
414
|
+
<link rel="stylesheet" th:href="@{/css/style.css}">
|
|
415
|
+
|
|
416
|
+
<!-- htmx(部分更新用) -->
|
|
417
|
+
<script src="https://unpkg.com/htmx.org@1.9.10"></script>
|
|
418
|
+
</head>
|
|
419
|
+
<body>
|
|
420
|
+
<!-- ナビゲーションバー -->
|
|
421
|
+
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
|
|
422
|
+
<div class="container-fluid">
|
|
423
|
+
<a class="navbar-brand" th:href="@{/}">生産管理システム</a>
|
|
424
|
+
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
|
|
425
|
+
data-bs-target="#navbarNav">
|
|
426
|
+
<span class="navbar-toggler-icon"></span>
|
|
427
|
+
</button>
|
|
428
|
+
<div class="collapse navbar-collapse" id="navbarNav">
|
|
429
|
+
<ul class="navbar-nav">
|
|
430
|
+
<li class="nav-item dropdown">
|
|
431
|
+
<a class="nav-link dropdown-toggle" href="#" role="button"
|
|
432
|
+
data-bs-toggle="dropdown">マスタ</a>
|
|
433
|
+
<ul class="dropdown-menu">
|
|
434
|
+
<li><a class="dropdown-item" th:href="@{/items}">品目マスタ</a></li>
|
|
435
|
+
<li><a class="dropdown-item" th:href="@{/bom}">部品構成表</a></li>
|
|
436
|
+
<li><a class="dropdown-item" th:href="@{/suppliers}">取引先マスタ</a></li>
|
|
437
|
+
<li><a class="dropdown-item" th:href="@{/locations}">場所マスタ</a></li>
|
|
438
|
+
</ul>
|
|
439
|
+
</li>
|
|
440
|
+
<li class="nav-item dropdown">
|
|
441
|
+
<a class="nav-link dropdown-toggle" href="#" role="button"
|
|
442
|
+
data-bs-toggle="dropdown">計画</a>
|
|
443
|
+
<ul class="dropdown-menu">
|
|
444
|
+
<li><a class="dropdown-item" th:href="@{/mps}">基準生産計画</a></li>
|
|
445
|
+
<li><a class="dropdown-item" th:href="@{/mrp}">MRP実行</a></li>
|
|
446
|
+
<li><a class="dropdown-item" th:href="@{/orders}">オーダ照会</a></li>
|
|
447
|
+
</ul>
|
|
448
|
+
</li>
|
|
449
|
+
<li class="nav-item dropdown">
|
|
450
|
+
<a class="nav-link dropdown-toggle" href="#" role="button"
|
|
451
|
+
data-bs-toggle="dropdown">購買</a>
|
|
452
|
+
<ul class="dropdown-menu">
|
|
453
|
+
<li><a class="dropdown-item" th:href="@{/purchase-orders}">発注</a></li>
|
|
454
|
+
<li><a class="dropdown-item" th:href="@{/receiving}">入荷・検収</a></li>
|
|
455
|
+
</ul>
|
|
456
|
+
</li>
|
|
457
|
+
<li class="nav-item dropdown">
|
|
458
|
+
<a class="nav-link dropdown-toggle" href="#" role="button"
|
|
459
|
+
data-bs-toggle="dropdown">製造</a>
|
|
460
|
+
<ul class="dropdown-menu">
|
|
461
|
+
<li><a class="dropdown-item" th:href="@{/work-orders}">作業指示</a></li>
|
|
462
|
+
<li><a class="dropdown-item" th:href="@{/completions}">完成実績</a></li>
|
|
463
|
+
<li><a class="dropdown-item" th:href="@{/labor-hours}">工数実績</a></li>
|
|
464
|
+
</ul>
|
|
465
|
+
</li>
|
|
466
|
+
<li class="nav-item dropdown">
|
|
467
|
+
<a class="nav-link dropdown-toggle" href="#" role="button"
|
|
468
|
+
data-bs-toggle="dropdown">在庫</a>
|
|
469
|
+
<ul class="dropdown-menu">
|
|
470
|
+
<li><a class="dropdown-item" th:href="@{/inventory}">在庫照会</a></li>
|
|
471
|
+
<li><a class="dropdown-item" th:href="@{/issues}">払出</a></li>
|
|
472
|
+
<li><a class="dropdown-item" th:href="@{/stocktaking}">棚卸</a></li>
|
|
473
|
+
</ul>
|
|
474
|
+
</li>
|
|
475
|
+
</ul>
|
|
476
|
+
</div>
|
|
477
|
+
</div>
|
|
478
|
+
</nav>
|
|
479
|
+
|
|
480
|
+
<!-- フラッシュメッセージ -->
|
|
481
|
+
<div class="container mt-3">
|
|
482
|
+
<div th:if="${successMessage}" class="alert alert-success alert-dismissible fade show" role="alert">
|
|
483
|
+
<span th:text="${successMessage}"></span>
|
|
484
|
+
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
|
485
|
+
</div>
|
|
486
|
+
<div th:if="${errorMessage}" class="alert alert-danger alert-dismissible fade show" role="alert">
|
|
487
|
+
<span th:text="${errorMessage}"></span>
|
|
488
|
+
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
|
489
|
+
</div>
|
|
490
|
+
</div>
|
|
491
|
+
|
|
492
|
+
<!-- メインコンテンツ -->
|
|
493
|
+
<main class="container mt-4">
|
|
494
|
+
<div layout:fragment="content">
|
|
495
|
+
<!-- 各ページのコンテンツがここに挿入される -->
|
|
496
|
+
</div>
|
|
497
|
+
</main>
|
|
498
|
+
|
|
499
|
+
<!-- フッター -->
|
|
500
|
+
<footer class="footer mt-5 py-3 bg-light">
|
|
501
|
+
<div class="container text-center">
|
|
502
|
+
<span class="text-muted">生産管理システム © 2025</span>
|
|
503
|
+
</div>
|
|
504
|
+
</footer>
|
|
505
|
+
|
|
506
|
+
<!-- Bootstrap JS -->
|
|
507
|
+
<script th:src="@{/webjars/bootstrap/js/bootstrap.bundle.min.js}"></script>
|
|
508
|
+
|
|
509
|
+
<!-- ページ固有のスクリプト -->
|
|
510
|
+
<th:block layout:fragment="scripts"></th:block>
|
|
511
|
+
</body>
|
|
512
|
+
</html>
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
</details>
|
|
516
|
+
|
|
517
|
+
<details>
|
|
518
|
+
<summary>HomeController.java</summary>
|
|
519
|
+
|
|
520
|
+
```java
|
|
521
|
+
package com.example.production.infrastructure.web.controller;
|
|
522
|
+
|
|
523
|
+
import com.example.production.application.port.in.InventoryUseCase;
|
|
524
|
+
import com.example.production.application.port.in.OrderUseCase;
|
|
525
|
+
import org.springframework.stereotype.Controller;
|
|
526
|
+
import org.springframework.ui.Model;
|
|
527
|
+
import org.springframework.web.bind.annotation.GetMapping;
|
|
528
|
+
|
|
529
|
+
import java.time.LocalDate;
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* ホーム画面 Controller
|
|
533
|
+
*/
|
|
534
|
+
@Controller
|
|
535
|
+
public class HomeController {
|
|
536
|
+
|
|
537
|
+
private final OrderUseCase orderUseCase;
|
|
538
|
+
private final InventoryUseCase inventoryUseCase;
|
|
539
|
+
|
|
540
|
+
public HomeController(OrderUseCase orderUseCase, InventoryUseCase inventoryUseCase) {
|
|
541
|
+
this.orderUseCase = orderUseCase;
|
|
542
|
+
this.inventoryUseCase = inventoryUseCase;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
@GetMapping("/")
|
|
546
|
+
public String home(Model model) {
|
|
547
|
+
// ダッシュボード情報を取得
|
|
548
|
+
model.addAttribute("today", LocalDate.now());
|
|
549
|
+
model.addAttribute("pendingOrders", orderUseCase.countPendingOrders());
|
|
550
|
+
model.addAttribute("shortageItems", inventoryUseCase.getShortageItems());
|
|
551
|
+
model.addAttribute("todayWorkOrders", orderUseCase.getTodayWorkOrders());
|
|
552
|
+
|
|
553
|
+
return "home";
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
```
|
|
557
|
+
|
|
558
|
+
</details>
|
|
559
|
+
|
|
560
|
+
<details>
|
|
561
|
+
<summary>templates/home.html</summary>
|
|
562
|
+
|
|
563
|
+
```html
|
|
564
|
+
<!DOCTYPE html>
|
|
565
|
+
<html xmlns:th="http://www.thymeleaf.org"
|
|
566
|
+
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
|
567
|
+
layout:decorate="~{layout/default}">
|
|
568
|
+
<head>
|
|
569
|
+
<title>ホーム</title>
|
|
570
|
+
</head>
|
|
571
|
+
<body>
|
|
572
|
+
<div layout:fragment="content">
|
|
573
|
+
<h1 class="mb-4">ダッシュボード</h1>
|
|
574
|
+
|
|
575
|
+
<div class="row">
|
|
576
|
+
<!-- 本日の日付 -->
|
|
577
|
+
<div class="col-md-3">
|
|
578
|
+
<div class="card text-white bg-primary mb-3">
|
|
579
|
+
<div class="card-header">本日</div>
|
|
580
|
+
<div class="card-body">
|
|
581
|
+
<h5 class="card-title" th:text="${#temporals.format(today, 'yyyy年MM月dd日')}"></h5>
|
|
582
|
+
</div>
|
|
583
|
+
</div>
|
|
584
|
+
</div>
|
|
585
|
+
|
|
586
|
+
<!-- 未処理オーダ数 -->
|
|
587
|
+
<div class="col-md-3">
|
|
588
|
+
<div class="card text-white bg-warning mb-3">
|
|
589
|
+
<div class="card-header">未処理オーダ</div>
|
|
590
|
+
<div class="card-body">
|
|
591
|
+
<h5 class="card-title">
|
|
592
|
+
<span th:text="${pendingOrders}">0</span> 件
|
|
593
|
+
</h5>
|
|
594
|
+
<a th:href="@{/orders}" class="btn btn-sm btn-light">詳細を見る</a>
|
|
595
|
+
</div>
|
|
596
|
+
</div>
|
|
597
|
+
</div>
|
|
598
|
+
|
|
599
|
+
<!-- 在庫不足品目 -->
|
|
600
|
+
<div class="col-md-3">
|
|
601
|
+
<div class="card text-white bg-danger mb-3">
|
|
602
|
+
<div class="card-header">在庫不足</div>
|
|
603
|
+
<div class="card-body">
|
|
604
|
+
<h5 class="card-title">
|
|
605
|
+
<span th:text="${#lists.size(shortageItems)}">0</span> 品目
|
|
606
|
+
</h5>
|
|
607
|
+
<a th:href="@{/inventory/shortage}" class="btn btn-sm btn-light">詳細を見る</a>
|
|
608
|
+
</div>
|
|
609
|
+
</div>
|
|
610
|
+
</div>
|
|
611
|
+
|
|
612
|
+
<!-- 本日の作業指示 -->
|
|
613
|
+
<div class="col-md-3">
|
|
614
|
+
<div class="card text-white bg-success mb-3">
|
|
615
|
+
<div class="card-header">本日の作業指示</div>
|
|
616
|
+
<div class="card-body">
|
|
617
|
+
<h5 class="card-title">
|
|
618
|
+
<span th:text="${#lists.size(todayWorkOrders)}">0</span> 件
|
|
619
|
+
</h5>
|
|
620
|
+
<a th:href="@{/work-orders}" class="btn btn-sm btn-light">詳細を見る</a>
|
|
621
|
+
</div>
|
|
622
|
+
</div>
|
|
623
|
+
</div>
|
|
624
|
+
</div>
|
|
625
|
+
|
|
626
|
+
<!-- 在庫不足品目一覧 -->
|
|
627
|
+
<div class="card mt-4" th:if="${not #lists.isEmpty(shortageItems)}">
|
|
628
|
+
<div class="card-header bg-danger text-white">
|
|
629
|
+
在庫不足品目
|
|
630
|
+
</div>
|
|
631
|
+
<div class="card-body">
|
|
632
|
+
<table class="table table-striped">
|
|
633
|
+
<thead>
|
|
634
|
+
<tr>
|
|
635
|
+
<th>品目コード</th>
|
|
636
|
+
<th>品名</th>
|
|
637
|
+
<th>現在庫</th>
|
|
638
|
+
<th>安全在庫</th>
|
|
639
|
+
<th>不足数</th>
|
|
640
|
+
</tr>
|
|
641
|
+
</thead>
|
|
642
|
+
<tbody>
|
|
643
|
+
<tr th:each="item : ${shortageItems}">
|
|
644
|
+
<td>
|
|
645
|
+
<a th:href="@{/items/{code}(code=${item.itemCode})}"
|
|
646
|
+
th:text="${item.itemCode}"></a>
|
|
647
|
+
</td>
|
|
648
|
+
<td th:text="${item.itemName}"></td>
|
|
649
|
+
<td th:text="${item.currentStock}"></td>
|
|
650
|
+
<td th:text="${item.safetyStock}"></td>
|
|
651
|
+
<td class="text-danger"
|
|
652
|
+
th:text="${item.safetyStock - item.currentStock}"></td>
|
|
653
|
+
</tr>
|
|
654
|
+
</tbody>
|
|
655
|
+
</table>
|
|
656
|
+
</div>
|
|
657
|
+
</div>
|
|
658
|
+
|
|
659
|
+
<!-- クイックアクセス -->
|
|
660
|
+
<div class="card mt-4">
|
|
661
|
+
<div class="card-header">クイックアクセス</div>
|
|
662
|
+
<div class="card-body">
|
|
663
|
+
<div class="row">
|
|
664
|
+
<div class="col-md-3 mb-2">
|
|
665
|
+
<a th:href="@{/items/new}" class="btn btn-outline-primary w-100">
|
|
666
|
+
品目登録
|
|
667
|
+
</a>
|
|
668
|
+
</div>
|
|
669
|
+
<div class="col-md-3 mb-2">
|
|
670
|
+
<a th:href="@{/purchase-orders/new}" class="btn btn-outline-primary w-100">
|
|
671
|
+
発注登録
|
|
672
|
+
</a>
|
|
673
|
+
</div>
|
|
674
|
+
<div class="col-md-3 mb-2">
|
|
675
|
+
<a th:href="@{/work-orders/new}" class="btn btn-outline-primary w-100">
|
|
676
|
+
作業指示登録
|
|
677
|
+
</a>
|
|
678
|
+
</div>
|
|
679
|
+
<div class="col-md-3 mb-2">
|
|
680
|
+
<a th:href="@{/mrp/execute}" class="btn btn-outline-success w-100">
|
|
681
|
+
MRP 実行
|
|
682
|
+
</a>
|
|
683
|
+
</div>
|
|
684
|
+
</div>
|
|
685
|
+
</div>
|
|
686
|
+
</div>
|
|
687
|
+
</div>
|
|
688
|
+
</body>
|
|
689
|
+
</html>
|
|
690
|
+
```
|
|
691
|
+
|
|
692
|
+
</details>
|
|
693
|
+
|
|
694
|
+
---
|
|
695
|
+
|
|
696
|
+
### 33.7 htmx による部分更新
|
|
697
|
+
|
|
698
|
+
モノリスアプリケーションでも、**htmx** を使用することで SPA のような操作性を実現できます。ページ全体をリロードせず、必要な部分だけを更新します。
|
|
699
|
+
|
|
700
|
+
```plantuml
|
|
701
|
+
@startuml htmx_flow
|
|
702
|
+
!define RECTANGLE class
|
|
703
|
+
|
|
704
|
+
title htmx による部分更新
|
|
705
|
+
|
|
706
|
+
actor "ブラウザ" as Browser
|
|
707
|
+
participant "htmx" as htmx
|
|
708
|
+
participant "Controller" as Controller
|
|
709
|
+
participant "Thymeleaf\nフラグメント" as Fragment
|
|
710
|
+
|
|
711
|
+
Browser -> htmx : ボタンクリック
|
|
712
|
+
htmx -> Controller : AJAX リクエスト\n(hx-get/hx-post)
|
|
713
|
+
Controller -> Fragment : フラグメントを\nレンダリング
|
|
714
|
+
Fragment --> Controller : HTML フラグメント
|
|
715
|
+
Controller --> htmx : HTML レスポンス
|
|
716
|
+
htmx -> Browser : DOM を部分更新\n(hx-target)
|
|
717
|
+
|
|
718
|
+
note right of htmx
|
|
719
|
+
htmx は特別な属性(hx-*)を
|
|
720
|
+
使って AJAX 通信を行う
|
|
721
|
+
JavaScript を書かずに
|
|
722
|
+
インタラクティブな UI を実現
|
|
723
|
+
end note
|
|
724
|
+
|
|
725
|
+
@enduml
|
|
726
|
+
```
|
|
727
|
+
|
|
728
|
+
<details>
|
|
729
|
+
<summary>htmx による品目検索(部分更新)</summary>
|
|
730
|
+
|
|
731
|
+
```html
|
|
732
|
+
<!-- 品目検索(部分更新) -->
|
|
733
|
+
<div class="mb-3">
|
|
734
|
+
<input type="text"
|
|
735
|
+
class="form-control"
|
|
736
|
+
name="keyword"
|
|
737
|
+
placeholder="品目コードまたは品名で検索"
|
|
738
|
+
hx-get="/items/search"
|
|
739
|
+
hx-trigger="keyup changed delay:300ms"
|
|
740
|
+
hx-target="#search-results"
|
|
741
|
+
hx-indicator="#loading">
|
|
742
|
+
<span id="loading" class="htmx-indicator">検索中...</span>
|
|
743
|
+
</div>
|
|
744
|
+
|
|
745
|
+
<!-- 検索結果がここに挿入される -->
|
|
746
|
+
<div id="search-results">
|
|
747
|
+
<!-- Controller から返される HTML フラグメント -->
|
|
748
|
+
</div>
|
|
749
|
+
```
|
|
750
|
+
|
|
751
|
+
</details>
|
|
752
|
+
|
|
753
|
+
<details>
|
|
754
|
+
<summary>検索エンドポイント(htmx 用 Controller)</summary>
|
|
755
|
+
|
|
756
|
+
```java
|
|
757
|
+
@Controller
|
|
758
|
+
@RequestMapping("/items")
|
|
759
|
+
public class ItemController {
|
|
760
|
+
|
|
761
|
+
private final ItemUseCase itemUseCase;
|
|
762
|
+
|
|
763
|
+
// ... コンストラクタ省略
|
|
764
|
+
|
|
765
|
+
/**
|
|
766
|
+
* htmx 用の検索エンドポイント(フラグメントを返す)
|
|
767
|
+
*/
|
|
768
|
+
@GetMapping("/search")
|
|
769
|
+
public String search(
|
|
770
|
+
@RequestParam(required = false) String keyword,
|
|
771
|
+
Model model) {
|
|
772
|
+
|
|
773
|
+
List<Item> items = itemUseCase.searchItems(keyword);
|
|
774
|
+
model.addAttribute("items", items);
|
|
775
|
+
|
|
776
|
+
// フラグメントのみを返す
|
|
777
|
+
return "items/search-results :: results";
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
```
|
|
781
|
+
|
|
782
|
+
</details>
|
|
783
|
+
|
|
784
|
+
<details>
|
|
785
|
+
<summary>items/search-results.html(検索結果フラグメント)</summary>
|
|
786
|
+
|
|
787
|
+
```html
|
|
788
|
+
<!DOCTYPE html>
|
|
789
|
+
<html xmlns:th="http://www.thymeleaf.org">
|
|
790
|
+
<!-- フラグメント定義 -->
|
|
791
|
+
<div th:fragment="results">
|
|
792
|
+
<table class="table table-striped" th:if="${not #lists.isEmpty(items)}">
|
|
793
|
+
<thead>
|
|
794
|
+
<tr>
|
|
795
|
+
<th>品目コード</th>
|
|
796
|
+
<th>品名</th>
|
|
797
|
+
<th>品目区分</th>
|
|
798
|
+
<th>操作</th>
|
|
799
|
+
</tr>
|
|
800
|
+
</thead>
|
|
801
|
+
<tbody>
|
|
802
|
+
<tr th:each="item : ${items}">
|
|
803
|
+
<td th:text="${item.itemCode}"></td>
|
|
804
|
+
<td th:text="${item.itemName}"></td>
|
|
805
|
+
<td th:text="${item.category.displayName}"></td>
|
|
806
|
+
<td>
|
|
807
|
+
<a th:href="@{/items/{code}(code=${item.itemCode})}"
|
|
808
|
+
class="btn btn-sm btn-primary">詳細</a>
|
|
809
|
+
<button class="btn btn-sm btn-danger"
|
|
810
|
+
hx-delete="/items/{code}"
|
|
811
|
+
th:attr="hx-delete=@{/items/{code}(code=${item.itemCode})}"
|
|
812
|
+
hx-confirm="本当に削除しますか?"
|
|
813
|
+
hx-target="closest tr"
|
|
814
|
+
hx-swap="outerHTML">削除</button>
|
|
815
|
+
</td>
|
|
816
|
+
</tr>
|
|
817
|
+
</tbody>
|
|
818
|
+
</table>
|
|
819
|
+
<div th:if="${#lists.isEmpty(items)}" class="alert alert-info">
|
|
820
|
+
該当する品目が見つかりませんでした。
|
|
821
|
+
</div>
|
|
822
|
+
</div>
|
|
823
|
+
</html>
|
|
824
|
+
```
|
|
825
|
+
|
|
826
|
+
</details>
|
|
827
|
+
|
|
828
|
+
---
|
|
829
|
+
|
|
830
|
+
### 33.8 API サーバー版との Controller の違い
|
|
831
|
+
|
|
832
|
+
#### API サーバー版(REST Controller)
|
|
833
|
+
|
|
834
|
+
<details>
|
|
835
|
+
<summary>ItemApiController.java(API サーバー版)</summary>
|
|
836
|
+
|
|
837
|
+
```java
|
|
838
|
+
@RestController // JSON を返す
|
|
839
|
+
@RequestMapping("/api/items")
|
|
840
|
+
public class ItemApiController {
|
|
841
|
+
|
|
842
|
+
@GetMapping
|
|
843
|
+
public ResponseEntity<List<ItemResponse>> getAllItems() {
|
|
844
|
+
List<Item> items = itemUseCase.getAllItems();
|
|
845
|
+
return ResponseEntity.ok(items.stream()
|
|
846
|
+
.map(ItemResponse::from)
|
|
847
|
+
.toList());
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
@PostMapping
|
|
851
|
+
public ResponseEntity<ItemResponse> createItem(
|
|
852
|
+
@Valid @RequestBody CreateItemRequest request) { // JSON リクエスト
|
|
853
|
+
Item item = itemUseCase.createItem(request.toCommand());
|
|
854
|
+
return ResponseEntity.status(HttpStatus.CREATED)
|
|
855
|
+
.body(ItemResponse.from(item));
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
```
|
|
859
|
+
|
|
860
|
+
</details>
|
|
861
|
+
|
|
862
|
+
#### モノリス版(MVC Controller)
|
|
863
|
+
|
|
864
|
+
<details>
|
|
865
|
+
<summary>ItemController.java(モノリス版)</summary>
|
|
866
|
+
|
|
867
|
+
```java
|
|
868
|
+
@Controller // HTML を返す
|
|
869
|
+
@RequestMapping("/items")
|
|
870
|
+
public class ItemController {
|
|
871
|
+
|
|
872
|
+
@GetMapping
|
|
873
|
+
public String list(Model model) {
|
|
874
|
+
List<Item> items = itemUseCase.getAllItems();
|
|
875
|
+
model.addAttribute("items", items);
|
|
876
|
+
return "items/list"; // Thymeleaf テンプレート
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
@PostMapping
|
|
880
|
+
public String create(
|
|
881
|
+
@Valid @ModelAttribute("form") ItemForm form, // フォームデータ
|
|
882
|
+
BindingResult bindingResult,
|
|
883
|
+
RedirectAttributes redirectAttributes) {
|
|
884
|
+
|
|
885
|
+
if (bindingResult.hasErrors()) {
|
|
886
|
+
return "items/new"; // バリデーションエラー時は入力画面に戻る
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
itemUseCase.createItem(form.toCommand());
|
|
890
|
+
redirectAttributes.addFlashAttribute("successMessage", "品目を登録しました");
|
|
891
|
+
return "redirect:/items"; // PRG パターン
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
```
|
|
895
|
+
|
|
896
|
+
</details>
|
|
897
|
+
|
|
898
|
+
**主な違い:**
|
|
899
|
+
|
|
900
|
+
| 観点 | API サーバー版 | モノリス版 |
|
|
901
|
+
|------|--------------|-----------|
|
|
902
|
+
| アノテーション | `@RestController` | `@Controller` |
|
|
903
|
+
| 戻り値 | `ResponseEntity<T>` | テンプレート名(String) |
|
|
904
|
+
| リクエスト | `@RequestBody`(JSON) | `@ModelAttribute`(フォーム) |
|
|
905
|
+
| バリデーション | 例外ハンドラで処理 | `BindingResult` で処理 |
|
|
906
|
+
| レスポンス | JSON | HTML(Thymeleaf) |
|
|
907
|
+
| リダイレクト | 不要 | PRG パターン |
|
|
908
|
+
|
|
909
|
+
---
|
|
910
|
+
|
|
911
|
+
## 第34章:マスタの実装
|
|
912
|
+
|
|
913
|
+
### 34.1 品目マスタ画面の設計
|
|
914
|
+
|
|
915
|
+
| 画面 | URL | メソッド | 説明 |
|
|
916
|
+
|------|-----|---------|------|
|
|
917
|
+
| 品目一覧 | /items | GET | 品目の検索・一覧表示 |
|
|
918
|
+
| 品目詳細 | /items/{itemCode} | GET | 品目の詳細表示 |
|
|
919
|
+
| 品目登録 | /items/new | GET | 登録フォーム表示 |
|
|
920
|
+
| 品目登録処理 | /items | POST | 登録処理 |
|
|
921
|
+
| 品目編集 | /items/{itemCode}/edit | GET | 編集フォーム表示 |
|
|
922
|
+
| 品目更新処理 | /items/{itemCode} | POST | 更新処理(PUT 代替) |
|
|
923
|
+
| 品目削除処理 | /items/{itemCode}/delete | POST | 削除処理(DELETE 代替) |
|
|
924
|
+
|
|
925
|
+
**注意**: HTML フォームは GET/POST のみ対応のため、PUT/DELETE は POST で代替します。
|
|
926
|
+
|
|
927
|
+
---
|
|
928
|
+
|
|
929
|
+
### 34.2 フォームオブジェクトの設計
|
|
930
|
+
|
|
931
|
+
<details>
|
|
932
|
+
<summary>ItemForm.java</summary>
|
|
933
|
+
|
|
934
|
+
```java
|
|
935
|
+
package com.example.production.infrastructure.web.form;
|
|
936
|
+
|
|
937
|
+
import com.example.production.application.port.in.CreateItemCommand;
|
|
938
|
+
import com.example.production.domain.model.item.ItemCategory;
|
|
939
|
+
import jakarta.validation.constraints.NotBlank;
|
|
940
|
+
import jakarta.validation.constraints.NotNull;
|
|
941
|
+
import jakarta.validation.constraints.Positive;
|
|
942
|
+
import jakarta.validation.constraints.Size;
|
|
943
|
+
import lombok.Data;
|
|
944
|
+
|
|
945
|
+
/**
|
|
946
|
+
* 品目登録・編集フォーム
|
|
947
|
+
*/
|
|
948
|
+
@Data
|
|
949
|
+
public class ItemForm {
|
|
950
|
+
|
|
951
|
+
@NotBlank(message = "品目コードは必須です")
|
|
952
|
+
@Size(max = 20, message = "品目コードは20文字以内で入力してください")
|
|
953
|
+
private String itemCode;
|
|
954
|
+
|
|
955
|
+
@NotBlank(message = "品名は必須です")
|
|
956
|
+
@Size(max = 100, message = "品名は100文字以内で入力してください")
|
|
957
|
+
private String itemName;
|
|
958
|
+
|
|
959
|
+
@NotNull(message = "品目区分は必須です")
|
|
960
|
+
private ItemCategory category;
|
|
961
|
+
|
|
962
|
+
private String itemGroupCode;
|
|
963
|
+
|
|
964
|
+
private String unitCode;
|
|
965
|
+
|
|
966
|
+
private String locationCode;
|
|
967
|
+
|
|
968
|
+
@Positive(message = "リードタイムは正の整数で入力してください")
|
|
969
|
+
private Integer leadTime;
|
|
970
|
+
|
|
971
|
+
@Positive(message = "安全在庫は正の整数で入力してください")
|
|
972
|
+
private Integer safetyStock;
|
|
973
|
+
|
|
974
|
+
/**
|
|
975
|
+
* フォームをコマンドに変換
|
|
976
|
+
*/
|
|
977
|
+
public CreateItemCommand toCommand() {
|
|
978
|
+
return CreateItemCommand.builder()
|
|
979
|
+
.itemCode(this.itemCode)
|
|
980
|
+
.itemName(this.itemName)
|
|
981
|
+
.category(this.category)
|
|
982
|
+
.itemGroupCode(this.itemGroupCode)
|
|
983
|
+
.unitCode(this.unitCode)
|
|
984
|
+
.locationCode(this.locationCode)
|
|
985
|
+
.leadTime(this.leadTime)
|
|
986
|
+
.safetyStock(this.safetyStock)
|
|
987
|
+
.build();
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
/**
|
|
991
|
+
* エンティティからフォームを生成
|
|
992
|
+
*/
|
|
993
|
+
public static ItemForm from(Item item) {
|
|
994
|
+
ItemForm form = new ItemForm();
|
|
995
|
+
form.setItemCode(item.getItemCode());
|
|
996
|
+
form.setItemName(item.getItemName());
|
|
997
|
+
form.setCategory(item.getCategory());
|
|
998
|
+
form.setItemGroupCode(item.getItemGroupCode());
|
|
999
|
+
form.setUnitCode(item.getUnitCode());
|
|
1000
|
+
form.setLocationCode(item.getLocationCode());
|
|
1001
|
+
form.setLeadTime(item.getLeadTime());
|
|
1002
|
+
form.setSafetyStock(item.getSafetyStock());
|
|
1003
|
+
return form;
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
```
|
|
1007
|
+
|
|
1008
|
+
</details>
|
|
1009
|
+
|
|
1010
|
+
---
|
|
1011
|
+
|
|
1012
|
+
### 34.3 品目 Controller の TDD 実装
|
|
1013
|
+
|
|
1014
|
+
<details>
|
|
1015
|
+
<summary>ItemControllerTest.java</summary>
|
|
1016
|
+
|
|
1017
|
+
```java
|
|
1018
|
+
package com.example.production.infrastructure.web.controller;
|
|
1019
|
+
|
|
1020
|
+
import com.example.production.domain.model.item.Item;
|
|
1021
|
+
import com.example.production.domain.model.item.ItemCategory;
|
|
1022
|
+
import com.example.production.infrastructure.persistence.mapper.ItemMapper;
|
|
1023
|
+
import org.junit.jupiter.api.*;
|
|
1024
|
+
import org.springframework.beans.factory.annotation.Autowired;
|
|
1025
|
+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
|
1026
|
+
import org.springframework.boot.test.context.SpringBootTest;
|
|
1027
|
+
import org.springframework.test.context.DynamicPropertyRegistry;
|
|
1028
|
+
import org.springframework.test.context.DynamicPropertySource;
|
|
1029
|
+
import org.springframework.test.web.servlet.MockMvc;
|
|
1030
|
+
import org.testcontainers.containers.PostgreSQLContainer;
|
|
1031
|
+
import org.testcontainers.junit.jupiter.Container;
|
|
1032
|
+
import org.testcontainers.junit.jupiter.Testcontainers;
|
|
1033
|
+
|
|
1034
|
+
import java.time.LocalDate;
|
|
1035
|
+
|
|
1036
|
+
import static org.hamcrest.Matchers.*;
|
|
1037
|
+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
|
1038
|
+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
|
1039
|
+
|
|
1040
|
+
@SpringBootTest
|
|
1041
|
+
@AutoConfigureMockMvc
|
|
1042
|
+
@Testcontainers
|
|
1043
|
+
@DisplayName("品目マスタ画面")
|
|
1044
|
+
class ItemControllerTest {
|
|
1045
|
+
|
|
1046
|
+
@Container
|
|
1047
|
+
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15")
|
|
1048
|
+
.withDatabaseName("production_test")
|
|
1049
|
+
.withUsername("test")
|
|
1050
|
+
.withPassword("test");
|
|
1051
|
+
|
|
1052
|
+
@DynamicPropertySource
|
|
1053
|
+
static void configureProperties(DynamicPropertyRegistry registry) {
|
|
1054
|
+
registry.add("spring.datasource.url", postgres::getJdbcUrl);
|
|
1055
|
+
registry.add("spring.datasource.username", postgres::getUsername);
|
|
1056
|
+
registry.add("spring.datasource.password", postgres::getPassword);
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
@Autowired
|
|
1060
|
+
private MockMvc mockMvc;
|
|
1061
|
+
|
|
1062
|
+
@Autowired
|
|
1063
|
+
private ItemMapper itemMapper;
|
|
1064
|
+
|
|
1065
|
+
@BeforeEach
|
|
1066
|
+
void setUp() {
|
|
1067
|
+
itemMapper.deleteAll();
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
@Nested
|
|
1071
|
+
@DisplayName("GET /items - 品目一覧")
|
|
1072
|
+
class ItemList {
|
|
1073
|
+
|
|
1074
|
+
@Test
|
|
1075
|
+
@DisplayName("品目一覧画面を表示できる")
|
|
1076
|
+
void shouldDisplayItemList() throws Exception {
|
|
1077
|
+
// Arrange
|
|
1078
|
+
insertTestItem("PROD-001", "テスト製品", ItemCategory.PRODUCT);
|
|
1079
|
+
|
|
1080
|
+
// Act & Assert
|
|
1081
|
+
mockMvc.perform(get("/items"))
|
|
1082
|
+
.andExpect(status().isOk())
|
|
1083
|
+
.andExpect(view().name("items/list"))
|
|
1084
|
+
.andExpect(model().attributeExists("items"))
|
|
1085
|
+
.andExpect(content().string(containsString("PROD-001")))
|
|
1086
|
+
.andExpect(content().string(containsString("テスト製品")));
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
@Test
|
|
1090
|
+
@DisplayName("品目区分でフィルタリングできる")
|
|
1091
|
+
void shouldFilterByCategory() throws Exception {
|
|
1092
|
+
// Arrange
|
|
1093
|
+
insertTestItem("PROD-001", "製品A", ItemCategory.PRODUCT);
|
|
1094
|
+
insertTestItem("PART-001", "部品A", ItemCategory.PART);
|
|
1095
|
+
|
|
1096
|
+
// Act & Assert
|
|
1097
|
+
mockMvc.perform(get("/items").param("category", "PRODUCT"))
|
|
1098
|
+
.andExpect(status().isOk())
|
|
1099
|
+
.andExpect(model().attribute("items", hasSize(1)))
|
|
1100
|
+
.andExpect(content().string(containsString("製品A")))
|
|
1101
|
+
.andExpect(content().string(not(containsString("部品A"))));
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
@Nested
|
|
1106
|
+
@DisplayName("POST /items - 品目登録処理")
|
|
1107
|
+
class CreateItem {
|
|
1108
|
+
|
|
1109
|
+
@Test
|
|
1110
|
+
@DisplayName("品目を登録できる")
|
|
1111
|
+
void shouldCreateItem() throws Exception {
|
|
1112
|
+
mockMvc.perform(post("/items")
|
|
1113
|
+
.param("itemCode", "NEW-001")
|
|
1114
|
+
.param("itemName", "新規品目")
|
|
1115
|
+
.param("category", "PRODUCT"))
|
|
1116
|
+
.andExpect(status().is3xxRedirection())
|
|
1117
|
+
.andExpect(redirectedUrl("/items"))
|
|
1118
|
+
.andExpect(flash().attribute("successMessage", containsString("登録")));
|
|
1119
|
+
|
|
1120
|
+
// データベースに保存されていることを確認
|
|
1121
|
+
var saved = itemMapper.findByCode("NEW-001");
|
|
1122
|
+
Assertions.assertNotNull(saved);
|
|
1123
|
+
Assertions.assertEquals("新規品目", saved.getItemName());
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
@Test
|
|
1127
|
+
@DisplayName("バリデーションエラー時は入力画面に戻る")
|
|
1128
|
+
void shouldReturnFormOnValidationError() throws Exception {
|
|
1129
|
+
mockMvc.perform(post("/items")
|
|
1130
|
+
.param("itemCode", "") // 必須項目が空
|
|
1131
|
+
.param("itemName", "")
|
|
1132
|
+
.param("category", "PRODUCT"))
|
|
1133
|
+
.andExpect(status().isOk())
|
|
1134
|
+
.andExpect(view().name("items/new"))
|
|
1135
|
+
.andExpect(model().attributeHasFieldErrors("form", "itemCode", "itemName"));
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
private void insertTestItem(String code, String name, ItemCategory category) {
|
|
1140
|
+
itemMapper.insert(Item.builder()
|
|
1141
|
+
.itemCode(code)
|
|
1142
|
+
.effectiveDate(LocalDate.of(2025, 1, 1))
|
|
1143
|
+
.itemName(name)
|
|
1144
|
+
.category(category)
|
|
1145
|
+
.build());
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
```
|
|
1149
|
+
|
|
1150
|
+
</details>
|
|
1151
|
+
|
|
1152
|
+
<details>
|
|
1153
|
+
<summary>ItemController.java</summary>
|
|
1154
|
+
|
|
1155
|
+
```java
|
|
1156
|
+
package com.example.production.infrastructure.web.controller;
|
|
1157
|
+
|
|
1158
|
+
import com.example.production.application.port.in.*;
|
|
1159
|
+
import com.example.production.domain.exception.DuplicateItemException;
|
|
1160
|
+
import com.example.production.domain.exception.ItemNotFoundException;
|
|
1161
|
+
import com.example.production.domain.model.item.Item;
|
|
1162
|
+
import com.example.production.domain.model.item.ItemCategory;
|
|
1163
|
+
import com.example.production.infrastructure.web.form.ItemForm;
|
|
1164
|
+
import jakarta.validation.Valid;
|
|
1165
|
+
import org.springframework.stereotype.Controller;
|
|
1166
|
+
import org.springframework.ui.Model;
|
|
1167
|
+
import org.springframework.validation.BindingResult;
|
|
1168
|
+
import org.springframework.web.bind.annotation.*;
|
|
1169
|
+
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
|
1170
|
+
|
|
1171
|
+
import java.util.List;
|
|
1172
|
+
|
|
1173
|
+
/**
|
|
1174
|
+
* 品目マスタ Controller(モノリス版)
|
|
1175
|
+
*/
|
|
1176
|
+
@Controller
|
|
1177
|
+
@RequestMapping("/items")
|
|
1178
|
+
public class ItemController {
|
|
1179
|
+
|
|
1180
|
+
private final ItemUseCase itemUseCase;
|
|
1181
|
+
|
|
1182
|
+
public ItemController(ItemUseCase itemUseCase) {
|
|
1183
|
+
this.itemUseCase = itemUseCase;
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
/**
|
|
1187
|
+
* 品目一覧画面
|
|
1188
|
+
*/
|
|
1189
|
+
@GetMapping
|
|
1190
|
+
public String list(
|
|
1191
|
+
@RequestParam(required = false) ItemCategory category,
|
|
1192
|
+
@RequestParam(required = false) String keyword,
|
|
1193
|
+
Model model) {
|
|
1194
|
+
|
|
1195
|
+
List<Item> items;
|
|
1196
|
+
if (category != null) {
|
|
1197
|
+
items = itemUseCase.getItemsByCategory(category);
|
|
1198
|
+
} else if (keyword != null && !keyword.isBlank()) {
|
|
1199
|
+
items = itemUseCase.searchItems(keyword);
|
|
1200
|
+
} else {
|
|
1201
|
+
items = itemUseCase.getAllItems();
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
model.addAttribute("items", items);
|
|
1205
|
+
model.addAttribute("categories", ItemCategory.values());
|
|
1206
|
+
model.addAttribute("selectedCategory", category);
|
|
1207
|
+
model.addAttribute("keyword", keyword);
|
|
1208
|
+
|
|
1209
|
+
return "items/list";
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
/**
|
|
1213
|
+
* 品目詳細画面
|
|
1214
|
+
*/
|
|
1215
|
+
@GetMapping("/{itemCode}")
|
|
1216
|
+
public String detail(@PathVariable String itemCode, Model model) {
|
|
1217
|
+
Item item = itemUseCase.getItemByCode(itemCode);
|
|
1218
|
+
model.addAttribute("item", item);
|
|
1219
|
+
return "items/detail";
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
/**
|
|
1223
|
+
* 品目登録画面
|
|
1224
|
+
*/
|
|
1225
|
+
@GetMapping("/new")
|
|
1226
|
+
public String newForm(Model model) {
|
|
1227
|
+
model.addAttribute("form", new ItemForm());
|
|
1228
|
+
model.addAttribute("categories", ItemCategory.values());
|
|
1229
|
+
return "items/new";
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
/**
|
|
1233
|
+
* 品目登録処理
|
|
1234
|
+
*/
|
|
1235
|
+
@PostMapping
|
|
1236
|
+
public String create(
|
|
1237
|
+
@Valid @ModelAttribute("form") ItemForm form,
|
|
1238
|
+
BindingResult bindingResult,
|
|
1239
|
+
Model model,
|
|
1240
|
+
RedirectAttributes redirectAttributes) {
|
|
1241
|
+
|
|
1242
|
+
if (bindingResult.hasErrors()) {
|
|
1243
|
+
model.addAttribute("categories", ItemCategory.values());
|
|
1244
|
+
return "items/new";
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
try {
|
|
1248
|
+
itemUseCase.createItem(form.toCommand());
|
|
1249
|
+
redirectAttributes.addFlashAttribute("successMessage",
|
|
1250
|
+
"品目「" + form.getItemCode() + "」を登録しました");
|
|
1251
|
+
return "redirect:/items";
|
|
1252
|
+
} catch (DuplicateItemException e) {
|
|
1253
|
+
model.addAttribute("errorMessage",
|
|
1254
|
+
"品目コード「" + form.getItemCode() + "」は既に存在します");
|
|
1255
|
+
model.addAttribute("categories", ItemCategory.values());
|
|
1256
|
+
return "items/new";
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
/**
|
|
1261
|
+
* 品目削除処理
|
|
1262
|
+
*/
|
|
1263
|
+
@PostMapping("/{itemCode}/delete")
|
|
1264
|
+
public String delete(
|
|
1265
|
+
@PathVariable String itemCode,
|
|
1266
|
+
RedirectAttributes redirectAttributes) {
|
|
1267
|
+
|
|
1268
|
+
itemUseCase.deleteItem(itemCode);
|
|
1269
|
+
redirectAttributes.addFlashAttribute("successMessage",
|
|
1270
|
+
"品目「" + itemCode + "」を削除しました");
|
|
1271
|
+
return "redirect:/items";
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
/**
|
|
1275
|
+
* 例外ハンドリング(品目が見つからない場合)
|
|
1276
|
+
*/
|
|
1277
|
+
@ExceptionHandler(ItemNotFoundException.class)
|
|
1278
|
+
@ResponseStatus(org.springframework.http.HttpStatus.NOT_FOUND)
|
|
1279
|
+
public String handleItemNotFound(ItemNotFoundException e, Model model) {
|
|
1280
|
+
model.addAttribute("errorMessage", e.getMessage());
|
|
1281
|
+
return "error/404";
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
```
|
|
1285
|
+
|
|
1286
|
+
</details>
|
|
1287
|
+
|
|
1288
|
+
---
|
|
1289
|
+
|
|
1290
|
+
### 34.4 品目一覧テンプレート
|
|
1291
|
+
|
|
1292
|
+
<details>
|
|
1293
|
+
<summary>items/list.html</summary>
|
|
1294
|
+
|
|
1295
|
+
```html
|
|
1296
|
+
<!DOCTYPE html>
|
|
1297
|
+
<html xmlns:th="http://www.thymeleaf.org"
|
|
1298
|
+
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
|
1299
|
+
layout:decorate="~{layout/default}">
|
|
1300
|
+
<head>
|
|
1301
|
+
<title>品目マスタ</title>
|
|
1302
|
+
</head>
|
|
1303
|
+
<body>
|
|
1304
|
+
<div layout:fragment="content">
|
|
1305
|
+
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
1306
|
+
<h1>品目マスタ</h1>
|
|
1307
|
+
<a th:href="@{/items/new}" class="btn btn-primary">
|
|
1308
|
+
新規登録
|
|
1309
|
+
</a>
|
|
1310
|
+
</div>
|
|
1311
|
+
|
|
1312
|
+
<!-- 検索フォーム -->
|
|
1313
|
+
<div class="card mb-4">
|
|
1314
|
+
<div class="card-body">
|
|
1315
|
+
<form th:action="@{/items}" method="get" class="row g-3">
|
|
1316
|
+
<div class="col-md-4">
|
|
1317
|
+
<label class="form-label">キーワード検索</label>
|
|
1318
|
+
<input type="text" class="form-control" name="keyword"
|
|
1319
|
+
th:value="${keyword}" placeholder="品目コードまたは品名">
|
|
1320
|
+
</div>
|
|
1321
|
+
<div class="col-md-3">
|
|
1322
|
+
<label class="form-label">品目区分</label>
|
|
1323
|
+
<select class="form-select" name="category">
|
|
1324
|
+
<option value="">すべて</option>
|
|
1325
|
+
<option th:each="cat : ${categories}"
|
|
1326
|
+
th:value="${cat}"
|
|
1327
|
+
th:text="${cat.displayName}"
|
|
1328
|
+
th:selected="${cat == selectedCategory}"></option>
|
|
1329
|
+
</select>
|
|
1330
|
+
</div>
|
|
1331
|
+
<div class="col-md-2 d-flex align-items-end">
|
|
1332
|
+
<button type="submit" class="btn btn-secondary">検索</button>
|
|
1333
|
+
</div>
|
|
1334
|
+
</form>
|
|
1335
|
+
</div>
|
|
1336
|
+
</div>
|
|
1337
|
+
|
|
1338
|
+
<!-- 品目一覧テーブル -->
|
|
1339
|
+
<div class="card">
|
|
1340
|
+
<div class="card-body">
|
|
1341
|
+
<table class="table table-striped table-hover" th:if="${not #lists.isEmpty(items)}">
|
|
1342
|
+
<thead>
|
|
1343
|
+
<tr>
|
|
1344
|
+
<th>品目コード</th>
|
|
1345
|
+
<th>品名</th>
|
|
1346
|
+
<th>品目区分</th>
|
|
1347
|
+
<th>リードタイム</th>
|
|
1348
|
+
<th>安全在庫</th>
|
|
1349
|
+
<th>操作</th>
|
|
1350
|
+
</tr>
|
|
1351
|
+
</thead>
|
|
1352
|
+
<tbody>
|
|
1353
|
+
<tr th:each="item : ${items}">
|
|
1354
|
+
<td>
|
|
1355
|
+
<a th:href="@{/items/{code}(code=${item.itemCode})}"
|
|
1356
|
+
th:text="${item.itemCode}"></a>
|
|
1357
|
+
</td>
|
|
1358
|
+
<td th:text="${item.itemName}"></td>
|
|
1359
|
+
<td>
|
|
1360
|
+
<span class="badge"
|
|
1361
|
+
th:classappend="${item.category.name() == 'PRODUCT'} ? 'bg-primary' :
|
|
1362
|
+
(${item.category.name() == 'PART'} ? 'bg-success' : 'bg-secondary')"
|
|
1363
|
+
th:text="${item.category.displayName}"></span>
|
|
1364
|
+
</td>
|
|
1365
|
+
<td th:text="${item.leadTime} + ' 日'" th:if="${item.leadTime}"></td>
|
|
1366
|
+
<td th:unless="${item.leadTime}">-</td>
|
|
1367
|
+
<td th:text="${item.safetyStock}" th:if="${item.safetyStock}"></td>
|
|
1368
|
+
<td th:unless="${item.safetyStock}">-</td>
|
|
1369
|
+
<td>
|
|
1370
|
+
<a th:href="@{/items/{code}/edit(code=${item.itemCode})}"
|
|
1371
|
+
class="btn btn-sm btn-outline-primary">編集</a>
|
|
1372
|
+
<form th:action="@{/items/{code}/delete(code=${item.itemCode})}"
|
|
1373
|
+
method="post" class="d-inline"
|
|
1374
|
+
onsubmit="return confirm('本当に削除しますか?');">
|
|
1375
|
+
<button type="submit" class="btn btn-sm btn-outline-danger">削除</button>
|
|
1376
|
+
</form>
|
|
1377
|
+
</td>
|
|
1378
|
+
</tr>
|
|
1379
|
+
</tbody>
|
|
1380
|
+
</table>
|
|
1381
|
+
<div th:if="${#lists.isEmpty(items)}" class="alert alert-info">
|
|
1382
|
+
該当する品目がありません。
|
|
1383
|
+
</div>
|
|
1384
|
+
</div>
|
|
1385
|
+
</div>
|
|
1386
|
+
|
|
1387
|
+
<!-- 件数表示 -->
|
|
1388
|
+
<div class="mt-3 text-muted">
|
|
1389
|
+
全 <span th:text="${#lists.size(items)}">0</span> 件
|
|
1390
|
+
</div>
|
|
1391
|
+
</div>
|
|
1392
|
+
</body>
|
|
1393
|
+
</html>
|
|
1394
|
+
```
|
|
1395
|
+
|
|
1396
|
+
</details>
|
|
1397
|
+
|
|
1398
|
+
---
|
|
1399
|
+
|
|
1400
|
+
### 34.5 BOM マスタ画面の実装
|
|
1401
|
+
|
|
1402
|
+
<details>
|
|
1403
|
+
<summary>BomController.java</summary>
|
|
1404
|
+
|
|
1405
|
+
```java
|
|
1406
|
+
@Controller
|
|
1407
|
+
@RequestMapping("/bom")
|
|
1408
|
+
public class BomController {
|
|
1409
|
+
|
|
1410
|
+
private final BomService bomService;
|
|
1411
|
+
private final ItemUseCase itemUseCase;
|
|
1412
|
+
|
|
1413
|
+
public BomController(BomService bomService, ItemUseCase itemUseCase) {
|
|
1414
|
+
this.bomService = bomService;
|
|
1415
|
+
this.itemUseCase = itemUseCase;
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
/**
|
|
1419
|
+
* BOM 一覧画面(製品の一覧)
|
|
1420
|
+
*/
|
|
1421
|
+
@GetMapping
|
|
1422
|
+
public String list(Model model) {
|
|
1423
|
+
List<Item> products = itemUseCase.getItemsByCategory(ItemCategory.PRODUCT);
|
|
1424
|
+
model.addAttribute("products", products);
|
|
1425
|
+
return "bom/list";
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
/**
|
|
1429
|
+
* BOM 構成表示(ツリー展開)
|
|
1430
|
+
*/
|
|
1431
|
+
@GetMapping("/{itemCode}/explode")
|
|
1432
|
+
public String explode(@PathVariable String itemCode, Model model) {
|
|
1433
|
+
BomNode tree = bomService.explodeBom(itemCode);
|
|
1434
|
+
Item item = itemUseCase.getItemByCode(itemCode);
|
|
1435
|
+
|
|
1436
|
+
model.addAttribute("item", item);
|
|
1437
|
+
model.addAttribute("bomTree", tree);
|
|
1438
|
+
return "bom/explode";
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
/**
|
|
1442
|
+
* 逆展開(使用先照会)
|
|
1443
|
+
*/
|
|
1444
|
+
@GetMapping("/{itemCode}/where-used")
|
|
1445
|
+
public String whereUsed(@PathVariable String itemCode, Model model) {
|
|
1446
|
+
List<WhereUsedResult> parents = bomService.whereUsed(itemCode);
|
|
1447
|
+
Item item = itemUseCase.getItemByCode(itemCode);
|
|
1448
|
+
|
|
1449
|
+
model.addAttribute("item", item);
|
|
1450
|
+
model.addAttribute("parents", parents);
|
|
1451
|
+
return "bom/where-used";
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
```
|
|
1455
|
+
|
|
1456
|
+
</details>
|
|
1457
|
+
|
|
1458
|
+
<details>
|
|
1459
|
+
<summary>bom/explode.html(BOM 展開テンプレート)</summary>
|
|
1460
|
+
|
|
1461
|
+
```html
|
|
1462
|
+
<!DOCTYPE html>
|
|
1463
|
+
<html xmlns:th="http://www.thymeleaf.org"
|
|
1464
|
+
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
|
1465
|
+
layout:decorate="~{layout/default}">
|
|
1466
|
+
<head>
|
|
1467
|
+
<title>BOM 展開</title>
|
|
1468
|
+
<style>
|
|
1469
|
+
.bom-tree { list-style-type: none; padding-left: 0; }
|
|
1470
|
+
.bom-tree ul { list-style-type: none; padding-left: 20px; }
|
|
1471
|
+
.bom-item { padding: 5px; border-left: 2px solid #dee2e6; margin-bottom: 2px; }
|
|
1472
|
+
.bom-item:hover { background-color: #f8f9fa; }
|
|
1473
|
+
.level-0 { border-left-color: #0d6efd; }
|
|
1474
|
+
.level-1 { border-left-color: #198754; }
|
|
1475
|
+
.level-2 { border-left-color: #ffc107; }
|
|
1476
|
+
.level-3 { border-left-color: #dc3545; }
|
|
1477
|
+
</style>
|
|
1478
|
+
</head>
|
|
1479
|
+
<body>
|
|
1480
|
+
<div layout:fragment="content">
|
|
1481
|
+
<h1 class="mb-4">BOM 展開</h1>
|
|
1482
|
+
|
|
1483
|
+
<div class="card mb-4">
|
|
1484
|
+
<div class="card-header">
|
|
1485
|
+
<strong th:text="${item.itemCode}"></strong> -
|
|
1486
|
+
<span th:text="${item.itemName}"></span>
|
|
1487
|
+
</div>
|
|
1488
|
+
<div class="card-body">
|
|
1489
|
+
<!-- 再帰的なツリー表示 -->
|
|
1490
|
+
<ul class="bom-tree">
|
|
1491
|
+
<th:block th:replace="~{bom/fragments :: bomNode(${bomTree})}"></th:block>
|
|
1492
|
+
</ul>
|
|
1493
|
+
</div>
|
|
1494
|
+
</div>
|
|
1495
|
+
|
|
1496
|
+
<a th:href="@{/bom}" class="btn btn-secondary">戻る</a>
|
|
1497
|
+
</div>
|
|
1498
|
+
</body>
|
|
1499
|
+
</html>
|
|
1500
|
+
```
|
|
1501
|
+
|
|
1502
|
+
</details>
|
|
1503
|
+
|
|
1504
|
+
---
|
|
1505
|
+
|
|
1506
|
+
## 第35章:トランザクションの実装
|
|
1507
|
+
|
|
1508
|
+
### 35.1 発注業務画面の実装
|
|
1509
|
+
|
|
1510
|
+
<details>
|
|
1511
|
+
<summary>PurchaseOrderController.java</summary>
|
|
1512
|
+
|
|
1513
|
+
```java
|
|
1514
|
+
@Controller
|
|
1515
|
+
@RequestMapping("/purchase-orders")
|
|
1516
|
+
public class PurchaseOrderController {
|
|
1517
|
+
|
|
1518
|
+
private final PurchaseOrderUseCase purchaseOrderUseCase;
|
|
1519
|
+
private final ItemUseCase itemUseCase;
|
|
1520
|
+
private final SupplierUseCase supplierUseCase;
|
|
1521
|
+
|
|
1522
|
+
// ... コンストラクタ省略
|
|
1523
|
+
|
|
1524
|
+
/**
|
|
1525
|
+
* 発注一覧画面
|
|
1526
|
+
*/
|
|
1527
|
+
@GetMapping
|
|
1528
|
+
public String list(
|
|
1529
|
+
@RequestParam(required = false) String status,
|
|
1530
|
+
@RequestParam(required = false) String supplierCode,
|
|
1531
|
+
Model model) {
|
|
1532
|
+
|
|
1533
|
+
List<PurchaseOrder> orders = purchaseOrderUseCase.getOrders(status, supplierCode);
|
|
1534
|
+
model.addAttribute("orders", orders);
|
|
1535
|
+
model.addAttribute("suppliers", supplierUseCase.getAllSuppliers());
|
|
1536
|
+
return "purchase-orders/list";
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1539
|
+
/**
|
|
1540
|
+
* 発注登録画面
|
|
1541
|
+
*/
|
|
1542
|
+
@GetMapping("/new")
|
|
1543
|
+
public String newForm(Model model) {
|
|
1544
|
+
model.addAttribute("form", new PurchaseOrderForm());
|
|
1545
|
+
model.addAttribute("suppliers", supplierUseCase.getAllSuppliers());
|
|
1546
|
+
model.addAttribute("items", itemUseCase.getItemsByCategory(ItemCategory.MATERIAL));
|
|
1547
|
+
return "purchase-orders/new";
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1550
|
+
/**
|
|
1551
|
+
* 発注登録処理
|
|
1552
|
+
*/
|
|
1553
|
+
@PostMapping
|
|
1554
|
+
public String create(
|
|
1555
|
+
@Valid @ModelAttribute("form") PurchaseOrderForm form,
|
|
1556
|
+
BindingResult bindingResult,
|
|
1557
|
+
Model model,
|
|
1558
|
+
RedirectAttributes redirectAttributes) {
|
|
1559
|
+
|
|
1560
|
+
if (bindingResult.hasErrors()) {
|
|
1561
|
+
model.addAttribute("suppliers", supplierUseCase.getAllSuppliers());
|
|
1562
|
+
model.addAttribute("items", itemUseCase.getItemsByCategory(ItemCategory.MATERIAL));
|
|
1563
|
+
return "purchase-orders/new";
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
PurchaseOrder order = purchaseOrderUseCase.createOrder(form.toCommand());
|
|
1567
|
+
redirectAttributes.addFlashAttribute("successMessage",
|
|
1568
|
+
"発注「" + order.getOrderNumber() + "」を登録しました");
|
|
1569
|
+
return "redirect:/purchase-orders";
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
/**
|
|
1573
|
+
* 発注確定処理
|
|
1574
|
+
*/
|
|
1575
|
+
@PostMapping("/{orderNumber}/confirm")
|
|
1576
|
+
public String confirm(
|
|
1577
|
+
@PathVariable String orderNumber,
|
|
1578
|
+
RedirectAttributes redirectAttributes) {
|
|
1579
|
+
|
|
1580
|
+
purchaseOrderUseCase.confirmOrder(orderNumber);
|
|
1581
|
+
redirectAttributes.addFlashAttribute("successMessage",
|
|
1582
|
+
"発注「" + orderNumber + "」を確定しました");
|
|
1583
|
+
return "redirect:/purchase-orders/" + orderNumber;
|
|
1584
|
+
}
|
|
1585
|
+
}
|
|
1586
|
+
```
|
|
1587
|
+
|
|
1588
|
+
</details>
|
|
1589
|
+
|
|
1590
|
+
---
|
|
1591
|
+
|
|
1592
|
+
### 35.2 MRP 実行画面の実装
|
|
1593
|
+
|
|
1594
|
+
<details>
|
|
1595
|
+
<summary>MrpController.java</summary>
|
|
1596
|
+
|
|
1597
|
+
```java
|
|
1598
|
+
@Controller
|
|
1599
|
+
@RequestMapping("/mrp")
|
|
1600
|
+
public class MrpController {
|
|
1601
|
+
|
|
1602
|
+
private final MrpService mrpService;
|
|
1603
|
+
|
|
1604
|
+
public MrpController(MrpService mrpService) {
|
|
1605
|
+
this.mrpService = mrpService;
|
|
1606
|
+
}
|
|
1607
|
+
|
|
1608
|
+
/**
|
|
1609
|
+
* MRP 実行画面
|
|
1610
|
+
*/
|
|
1611
|
+
@GetMapping
|
|
1612
|
+
public String index(Model model) {
|
|
1613
|
+
model.addAttribute("form", new MrpExecuteForm());
|
|
1614
|
+
return "mrp/index";
|
|
1615
|
+
}
|
|
1616
|
+
|
|
1617
|
+
/**
|
|
1618
|
+
* MRP 実行処理
|
|
1619
|
+
*/
|
|
1620
|
+
@PostMapping("/execute")
|
|
1621
|
+
public String execute(
|
|
1622
|
+
@Valid @ModelAttribute("form") MrpExecuteForm form,
|
|
1623
|
+
BindingResult bindingResult,
|
|
1624
|
+
Model model,
|
|
1625
|
+
RedirectAttributes redirectAttributes) {
|
|
1626
|
+
|
|
1627
|
+
if (bindingResult.hasErrors()) {
|
|
1628
|
+
return "mrp/index";
|
|
1629
|
+
}
|
|
1630
|
+
|
|
1631
|
+
MrpResult result = mrpService.execute(form.getStartDate(), form.getEndDate());
|
|
1632
|
+
|
|
1633
|
+
// 結果をセッションに保存(大量データのため)
|
|
1634
|
+
model.addAttribute("result", result);
|
|
1635
|
+
return "mrp/result";
|
|
1636
|
+
}
|
|
1637
|
+
|
|
1638
|
+
/**
|
|
1639
|
+
* MRP 結果の帳票出力(Excel)
|
|
1640
|
+
*/
|
|
1641
|
+
@GetMapping("/export")
|
|
1642
|
+
public ResponseEntity<Resource> exportExcel(HttpSession session) {
|
|
1643
|
+
MrpResult result = (MrpResult) session.getAttribute("mrpResult");
|
|
1644
|
+
if (result == null) {
|
|
1645
|
+
throw new IllegalStateException("MRP 結果が見つかりません");
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
ByteArrayResource resource = mrpService.exportToExcel(result);
|
|
1649
|
+
|
|
1650
|
+
return ResponseEntity.ok()
|
|
1651
|
+
.header(HttpHeaders.CONTENT_DISPOSITION,
|
|
1652
|
+
"attachment; filename=mrp-result.xlsx")
|
|
1653
|
+
.contentType(MediaType.parseMediaType(
|
|
1654
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
|
|
1655
|
+
.body(resource);
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
```
|
|
1659
|
+
|
|
1660
|
+
</details>
|
|
1661
|
+
|
|
1662
|
+
---
|
|
1663
|
+
|
|
1664
|
+
## 第36章:エラーハンドリングと帳票出力
|
|
1665
|
+
|
|
1666
|
+
### 36.1 グローバル例外ハンドラ
|
|
1667
|
+
|
|
1668
|
+
<details>
|
|
1669
|
+
<summary>GlobalExceptionHandler.java</summary>
|
|
1670
|
+
|
|
1671
|
+
```java
|
|
1672
|
+
package com.example.production.infrastructure.web.exception;
|
|
1673
|
+
|
|
1674
|
+
import com.example.production.domain.exception.*;
|
|
1675
|
+
import org.springframework.ui.Model;
|
|
1676
|
+
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
1677
|
+
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
1678
|
+
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
|
1679
|
+
|
|
1680
|
+
/**
|
|
1681
|
+
* グローバル例外ハンドラ(モノリス版)
|
|
1682
|
+
*/
|
|
1683
|
+
@ControllerAdvice
|
|
1684
|
+
public class GlobalExceptionHandler {
|
|
1685
|
+
|
|
1686
|
+
/**
|
|
1687
|
+
* 品目が見つからない
|
|
1688
|
+
*/
|
|
1689
|
+
@ExceptionHandler(ItemNotFoundException.class)
|
|
1690
|
+
public String handleItemNotFound(ItemNotFoundException e, Model model) {
|
|
1691
|
+
model.addAttribute("errorTitle", "品目が見つかりません");
|
|
1692
|
+
model.addAttribute("errorMessage", e.getMessage());
|
|
1693
|
+
return "error/404";
|
|
1694
|
+
}
|
|
1695
|
+
|
|
1696
|
+
/**
|
|
1697
|
+
* 重複エラー
|
|
1698
|
+
*/
|
|
1699
|
+
@ExceptionHandler(DuplicateItemException.class)
|
|
1700
|
+
public String handleDuplicate(DuplicateItemException e, RedirectAttributes redirectAttributes) {
|
|
1701
|
+
redirectAttributes.addFlashAttribute("errorMessage", e.getMessage());
|
|
1702
|
+
return "redirect:/items/new";
|
|
1703
|
+
}
|
|
1704
|
+
|
|
1705
|
+
/**
|
|
1706
|
+
* 在庫不足
|
|
1707
|
+
*/
|
|
1708
|
+
@ExceptionHandler(InsufficientInventoryException.class)
|
|
1709
|
+
public String handleInsufficientInventory(InsufficientInventoryException e, Model model) {
|
|
1710
|
+
model.addAttribute("errorTitle", "在庫不足");
|
|
1711
|
+
model.addAttribute("errorMessage", e.getMessage());
|
|
1712
|
+
return "error/business-error";
|
|
1713
|
+
}
|
|
1714
|
+
|
|
1715
|
+
/**
|
|
1716
|
+
* その他の例外
|
|
1717
|
+
*/
|
|
1718
|
+
@ExceptionHandler(Exception.class)
|
|
1719
|
+
public String handleGenericException(Exception e, Model model) {
|
|
1720
|
+
model.addAttribute("errorTitle", "システムエラー");
|
|
1721
|
+
model.addAttribute("errorMessage", "予期しないエラーが発生しました。管理者に連絡してください。");
|
|
1722
|
+
model.addAttribute("errorDetail", e.getMessage());
|
|
1723
|
+
return "error/500";
|
|
1724
|
+
}
|
|
1725
|
+
}
|
|
1726
|
+
```
|
|
1727
|
+
|
|
1728
|
+
</details>
|
|
1729
|
+
|
|
1730
|
+
---
|
|
1731
|
+
|
|
1732
|
+
### 36.2 帳票出力(Excel)
|
|
1733
|
+
|
|
1734
|
+
<details>
|
|
1735
|
+
<summary>ReportService.java</summary>
|
|
1736
|
+
|
|
1737
|
+
```java
|
|
1738
|
+
@Service
|
|
1739
|
+
public class ReportService {
|
|
1740
|
+
|
|
1741
|
+
/**
|
|
1742
|
+
* 在庫一覧を Excel 出力
|
|
1743
|
+
*/
|
|
1744
|
+
public ByteArrayResource exportInventoryToExcel(List<Stock> stocks) {
|
|
1745
|
+
try (Workbook workbook = new XSSFWorkbook()) {
|
|
1746
|
+
Sheet sheet = workbook.createSheet("在庫一覧");
|
|
1747
|
+
|
|
1748
|
+
// ヘッダー行
|
|
1749
|
+
Row headerRow = sheet.createRow(0);
|
|
1750
|
+
String[] headers = {"場所コード", "品目コード", "品名", "在庫数量", "在庫状態"};
|
|
1751
|
+
for (int i = 0; i < headers.length; i++) {
|
|
1752
|
+
Cell cell = headerRow.createCell(i);
|
|
1753
|
+
cell.setCellValue(headers[i]);
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1756
|
+
// データ行
|
|
1757
|
+
int rowNum = 1;
|
|
1758
|
+
for (Stock stock : stocks) {
|
|
1759
|
+
Row row = sheet.createRow(rowNum++);
|
|
1760
|
+
row.createCell(0).setCellValue(stock.getLocationCode());
|
|
1761
|
+
row.createCell(1).setCellValue(stock.getItemCode());
|
|
1762
|
+
row.createCell(2).setCellValue(stock.getItemName());
|
|
1763
|
+
row.createCell(3).setCellValue(stock.getQuantity().doubleValue());
|
|
1764
|
+
row.createCell(4).setCellValue(stock.getStatus().getDisplayName());
|
|
1765
|
+
}
|
|
1766
|
+
|
|
1767
|
+
// 列幅自動調整
|
|
1768
|
+
for (int i = 0; i < headers.length; i++) {
|
|
1769
|
+
sheet.autoSizeColumn(i);
|
|
1770
|
+
}
|
|
1771
|
+
|
|
1772
|
+
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1773
|
+
workbook.write(out);
|
|
1774
|
+
return new ByteArrayResource(out.toByteArray());
|
|
1775
|
+
|
|
1776
|
+
} catch (IOException e) {
|
|
1777
|
+
throw new RuntimeException("Excel 出力に失敗しました", e);
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
```
|
|
1782
|
+
|
|
1783
|
+
</details>
|
|
1784
|
+
|
|
1785
|
+
<details>
|
|
1786
|
+
<summary>ReportController.java</summary>
|
|
1787
|
+
|
|
1788
|
+
```java
|
|
1789
|
+
@Controller
|
|
1790
|
+
@RequestMapping("/reports")
|
|
1791
|
+
public class ReportController {
|
|
1792
|
+
|
|
1793
|
+
private final ReportService reportService;
|
|
1794
|
+
private final InventoryUseCase inventoryUseCase;
|
|
1795
|
+
|
|
1796
|
+
// ... コンストラクタ省略
|
|
1797
|
+
|
|
1798
|
+
@GetMapping("/inventory/excel")
|
|
1799
|
+
public ResponseEntity<Resource> exportInventoryExcel(
|
|
1800
|
+
@RequestParam(required = false) String locationCode) {
|
|
1801
|
+
|
|
1802
|
+
List<Stock> stocks = inventoryUseCase.getInventory(
|
|
1803
|
+
InventoryQuery.builder().locationCode(locationCode).build());
|
|
1804
|
+
|
|
1805
|
+
ByteArrayResource resource = reportService.exportInventoryToExcel(stocks);
|
|
1806
|
+
|
|
1807
|
+
String filename = "inventory-" + LocalDate.now() + ".xlsx";
|
|
1808
|
+
|
|
1809
|
+
return ResponseEntity.ok()
|
|
1810
|
+
.header(HttpHeaders.CONTENT_DISPOSITION,
|
|
1811
|
+
"attachment; filename=" + filename)
|
|
1812
|
+
.contentType(MediaType.parseMediaType(
|
|
1813
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
|
|
1814
|
+
.body(resource);
|
|
1815
|
+
}
|
|
1816
|
+
}
|
|
1817
|
+
```
|
|
1818
|
+
|
|
1819
|
+
</details>
|
|
1820
|
+
|
|
1821
|
+
---
|
|
1822
|
+
|
|
1823
|
+
## 第37章:ページネーション
|
|
1824
|
+
|
|
1825
|
+
### 37.1 ページネーションの概要
|
|
1826
|
+
|
|
1827
|
+
大量のデータを扱う一覧画面では、すべてのデータを一度に表示すると、パフォーマンスの低下やユーザビリティの問題が発生します。ページネーション(ページ分割)を実装することで、これらの問題を解決します。
|
|
1828
|
+
|
|
1829
|
+
```plantuml
|
|
1830
|
+
@startuml pagination_flow
|
|
1831
|
+
!define RECTANGLE class
|
|
1832
|
+
|
|
1833
|
+
title ページネーションの仕組み
|
|
1834
|
+
|
|
1835
|
+
actor "ブラウザ" as Browser
|
|
1836
|
+
participant "Controller" as Controller
|
|
1837
|
+
participant "UseCase" as UseCase
|
|
1838
|
+
participant "Repository" as Repository
|
|
1839
|
+
database "Database" as DB
|
|
1840
|
+
|
|
1841
|
+
Browser -> Controller : GET /items?page=2&size=20
|
|
1842
|
+
Controller -> UseCase : getItems(page=2, size=20)
|
|
1843
|
+
UseCase -> Repository : findAll(Pageable)
|
|
1844
|
+
Repository -> DB : SELECT ... LIMIT 20 OFFSET 20
|
|
1845
|
+
DB --> Repository : 20件のデータ + 総件数
|
|
1846
|
+
Repository --> UseCase : Page<Item>
|
|
1847
|
+
UseCase --> Controller : Page<Item>
|
|
1848
|
+
Controller --> Browser : items + ページ情報
|
|
1849
|
+
|
|
1850
|
+
note right of DB
|
|
1851
|
+
LIMIT: 1ページあたりの件数
|
|
1852
|
+
OFFSET: スキップする件数
|
|
1853
|
+
例: 2ページ目 = OFFSET 20
|
|
1854
|
+
end note
|
|
1855
|
+
|
|
1856
|
+
@enduml
|
|
1857
|
+
```
|
|
1858
|
+
|
|
1859
|
+
### 37.2 ページネーション用のドメインオブジェクト
|
|
1860
|
+
|
|
1861
|
+
<details>
|
|
1862
|
+
<summary>PageResult.java(ページ結果)</summary>
|
|
1863
|
+
|
|
1864
|
+
```java
|
|
1865
|
+
package com.example.pms.domain.model.common;
|
|
1866
|
+
|
|
1867
|
+
import java.util.List;
|
|
1868
|
+
|
|
1869
|
+
/**
|
|
1870
|
+
* ページネーション結果を保持するクラス.
|
|
1871
|
+
*
|
|
1872
|
+
* @param <T> コンテンツの型
|
|
1873
|
+
*/
|
|
1874
|
+
public class PageResult<T> {
|
|
1875
|
+
|
|
1876
|
+
private final List<T> content;
|
|
1877
|
+
private final int page;
|
|
1878
|
+
private final int size;
|
|
1879
|
+
private final long totalElements;
|
|
1880
|
+
private final int totalPages;
|
|
1881
|
+
|
|
1882
|
+
public PageResult(List<T> content, int page, int size, long totalElements) {
|
|
1883
|
+
this.content = content;
|
|
1884
|
+
this.page = page;
|
|
1885
|
+
this.size = size;
|
|
1886
|
+
this.totalElements = totalElements;
|
|
1887
|
+
this.totalPages = size > 0 ? (int) Math.ceil((double) totalElements / size) : 0;
|
|
1888
|
+
}
|
|
1889
|
+
|
|
1890
|
+
public List<T> getContent() {
|
|
1891
|
+
return content;
|
|
1892
|
+
}
|
|
1893
|
+
|
|
1894
|
+
public int getPage() {
|
|
1895
|
+
return page;
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1898
|
+
public int getSize() {
|
|
1899
|
+
return size;
|
|
1900
|
+
}
|
|
1901
|
+
|
|
1902
|
+
public long getTotalElements() {
|
|
1903
|
+
return totalElements;
|
|
1904
|
+
}
|
|
1905
|
+
|
|
1906
|
+
public int getTotalPages() {
|
|
1907
|
+
return totalPages;
|
|
1908
|
+
}
|
|
1909
|
+
|
|
1910
|
+
public boolean hasNext() {
|
|
1911
|
+
return page < totalPages - 1;
|
|
1912
|
+
}
|
|
1913
|
+
|
|
1914
|
+
public boolean hasPrevious() {
|
|
1915
|
+
return page > 0;
|
|
1916
|
+
}
|
|
1917
|
+
|
|
1918
|
+
public boolean isFirst() {
|
|
1919
|
+
return page == 0;
|
|
1920
|
+
}
|
|
1921
|
+
|
|
1922
|
+
public boolean isLast() {
|
|
1923
|
+
return page >= totalPages - 1;
|
|
1924
|
+
}
|
|
1925
|
+
}
|
|
1926
|
+
```
|
|
1927
|
+
|
|
1928
|
+
</details>
|
|
1929
|
+
|
|
1930
|
+
---
|
|
1931
|
+
|
|
1932
|
+
### 37.3 Repository 層の実装
|
|
1933
|
+
|
|
1934
|
+
<details>
|
|
1935
|
+
<summary>ItemRepository.java(ページネーション対応)</summary>
|
|
1936
|
+
|
|
1937
|
+
```java
|
|
1938
|
+
package com.example.pms.application.port.out;
|
|
1939
|
+
|
|
1940
|
+
import com.example.pms.domain.model.item.Item;
|
|
1941
|
+
import com.example.pms.domain.model.item.ItemCategory;
|
|
1942
|
+
|
|
1943
|
+
import java.util.List;
|
|
1944
|
+
import java.util.Optional;
|
|
1945
|
+
|
|
1946
|
+
public interface ItemRepository {
|
|
1947
|
+
|
|
1948
|
+
/**
|
|
1949
|
+
* ページネーション付きで品目を取得
|
|
1950
|
+
*/
|
|
1951
|
+
List<Item> findWithPagination(ItemCategory category, String keyword, int limit, int offset);
|
|
1952
|
+
|
|
1953
|
+
/**
|
|
1954
|
+
* 条件に一致する品目の件数を取得
|
|
1955
|
+
*/
|
|
1956
|
+
long count(ItemCategory category, String keyword);
|
|
1957
|
+
|
|
1958
|
+
List<Item> findAll();
|
|
1959
|
+
|
|
1960
|
+
List<Item> findByCategory(ItemCategory category);
|
|
1961
|
+
|
|
1962
|
+
List<Item> searchByKeyword(String keyword);
|
|
1963
|
+
|
|
1964
|
+
Optional<Item> findByItemCode(String itemCode);
|
|
1965
|
+
|
|
1966
|
+
void save(Item item);
|
|
1967
|
+
|
|
1968
|
+
void update(Item item);
|
|
1969
|
+
|
|
1970
|
+
void deleteByItemCode(String itemCode);
|
|
1971
|
+
}
|
|
1972
|
+
```
|
|
1973
|
+
|
|
1974
|
+
</details>
|
|
1975
|
+
|
|
1976
|
+
<details>
|
|
1977
|
+
<summary>ItemMapper.xml(MyBatis XML マッピング - PostgreSQL/H2 対応)</summary>
|
|
1978
|
+
|
|
1979
|
+
```xml
|
|
1980
|
+
<!-- ページネーション対応クエリ -->
|
|
1981
|
+
<select id="findWithPagination" resultMap="itemResultMap" databaseId="postgresql">
|
|
1982
|
+
SELECT
|
|
1983
|
+
id, 品目コード, 品目名, 品目区分, 単位コード,
|
|
1984
|
+
適用開始日, 適用終了日, リードタイム, 安全リードタイム,
|
|
1985
|
+
安全在庫, 歩留率, 最小ロットサイズ, ロット増分,
|
|
1986
|
+
最大ロットサイズ, 賞味期限日数, 作成日時, 更新日時
|
|
1987
|
+
FROM 品目マスタ
|
|
1988
|
+
WHERE 1=1
|
|
1989
|
+
<if test="category != null">
|
|
1990
|
+
AND 品目区分 = #{category}::品目区分
|
|
1991
|
+
</if>
|
|
1992
|
+
<if test="keyword != null and keyword != ''">
|
|
1993
|
+
AND (品目コード LIKE '%' || #{keyword} || '%'
|
|
1994
|
+
OR 品目名 LIKE '%' || #{keyword} || '%')
|
|
1995
|
+
</if>
|
|
1996
|
+
ORDER BY 品目コード
|
|
1997
|
+
LIMIT #{limit} OFFSET #{offset}
|
|
1998
|
+
</select>
|
|
1999
|
+
|
|
2000
|
+
<select id="findWithPagination" resultMap="itemResultMap" databaseId="h2">
|
|
2001
|
+
SELECT
|
|
2002
|
+
id, 品目コード, 品目名, 品目区分, 単位コード,
|
|
2003
|
+
適用開始日, 適用終了日, リードタイム, 安全リードタイム,
|
|
2004
|
+
安全在庫, 歩留率, 最小ロットサイズ, ロット増分,
|
|
2005
|
+
最大ロットサイズ, 賞味期限日数, 作成日時, 更新日時
|
|
2006
|
+
FROM 品目マスタ
|
|
2007
|
+
WHERE 1=1
|
|
2008
|
+
<if test="category != null">
|
|
2009
|
+
AND 品目区分 = #{category}
|
|
2010
|
+
</if>
|
|
2011
|
+
<if test="keyword != null and keyword != ''">
|
|
2012
|
+
AND (品目コード LIKE '%' || #{keyword} || '%'
|
|
2013
|
+
OR 品目名 LIKE '%' || #{keyword} || '%')
|
|
2014
|
+
</if>
|
|
2015
|
+
ORDER BY 品目コード
|
|
2016
|
+
LIMIT #{limit} OFFSET #{offset}
|
|
2017
|
+
</select>
|
|
2018
|
+
|
|
2019
|
+
<select id="count" resultType="long">
|
|
2020
|
+
SELECT COUNT(*)
|
|
2021
|
+
FROM 品目マスタ
|
|
2022
|
+
WHERE 1=1
|
|
2023
|
+
<if test="category != null">
|
|
2024
|
+
<choose>
|
|
2025
|
+
<when test="_databaseId == 'postgresql'">
|
|
2026
|
+
AND 品目区分 = #{category}::品目区分
|
|
2027
|
+
</when>
|
|
2028
|
+
<otherwise>
|
|
2029
|
+
AND 品目区分 = #{category}
|
|
2030
|
+
</otherwise>
|
|
2031
|
+
</choose>
|
|
2032
|
+
</if>
|
|
2033
|
+
<if test="keyword != null and keyword != ''">
|
|
2034
|
+
AND (品目コード LIKE '%' || #{keyword} || '%'
|
|
2035
|
+
OR 品目名 LIKE '%' || #{keyword} || '%')
|
|
2036
|
+
</if>
|
|
2037
|
+
</select>
|
|
2038
|
+
```
|
|
2039
|
+
|
|
2040
|
+
</details>
|
|
2041
|
+
|
|
2042
|
+
<details>
|
|
2043
|
+
<summary>ItemRepositoryImpl.java(Repository 実装)</summary>
|
|
2044
|
+
|
|
2045
|
+
```java
|
|
2046
|
+
package com.example.pms.infrastructure.out.persistence;
|
|
2047
|
+
|
|
2048
|
+
import com.example.pms.application.port.out.ItemRepository;
|
|
2049
|
+
import com.example.pms.domain.model.item.Item;
|
|
2050
|
+
import com.example.pms.domain.model.item.ItemCategory;
|
|
2051
|
+
import org.springframework.stereotype.Repository;
|
|
2052
|
+
|
|
2053
|
+
import java.util.List;
|
|
2054
|
+
import java.util.Optional;
|
|
2055
|
+
|
|
2056
|
+
@Repository
|
|
2057
|
+
public class ItemRepositoryImpl implements ItemRepository {
|
|
2058
|
+
|
|
2059
|
+
private final ItemMapper itemMapper;
|
|
2060
|
+
|
|
2061
|
+
public ItemRepositoryImpl(ItemMapper itemMapper) {
|
|
2062
|
+
this.itemMapper = itemMapper;
|
|
2063
|
+
}
|
|
2064
|
+
|
|
2065
|
+
@Override
|
|
2066
|
+
public List<Item> findWithPagination(ItemCategory category, String keyword, int limit, int offset) {
|
|
2067
|
+
return itemMapper.findWithPagination(category, keyword, limit, offset);
|
|
2068
|
+
}
|
|
2069
|
+
|
|
2070
|
+
@Override
|
|
2071
|
+
public long count(ItemCategory category, String keyword) {
|
|
2072
|
+
return itemMapper.count(category, keyword);
|
|
2073
|
+
}
|
|
2074
|
+
|
|
2075
|
+
// ... その他のメソッド
|
|
2076
|
+
}
|
|
2077
|
+
```
|
|
2078
|
+
|
|
2079
|
+
</details>
|
|
2080
|
+
|
|
2081
|
+
---
|
|
2082
|
+
|
|
2083
|
+
### 37.4 Controller 層の実装
|
|
2084
|
+
|
|
2085
|
+
<details>
|
|
2086
|
+
<summary>ItemWebController.java(ページネーション対応)</summary>
|
|
2087
|
+
|
|
2088
|
+
```java
|
|
2089
|
+
package com.example.pms.infrastructure.in.web;
|
|
2090
|
+
|
|
2091
|
+
import com.example.pms.application.port.in.ItemUseCase;
|
|
2092
|
+
import com.example.pms.domain.model.common.PageResult;
|
|
2093
|
+
import com.example.pms.domain.model.item.Item;
|
|
2094
|
+
import com.example.pms.domain.model.item.ItemCategory;
|
|
2095
|
+
import org.springframework.stereotype.Controller;
|
|
2096
|
+
import org.springframework.ui.Model;
|
|
2097
|
+
import org.springframework.web.bind.annotation.GetMapping;
|
|
2098
|
+
import org.springframework.web.bind.annotation.RequestMapping;
|
|
2099
|
+
import org.springframework.web.bind.annotation.RequestParam;
|
|
2100
|
+
|
|
2101
|
+
/**
|
|
2102
|
+
* 品目マスタ画面 Controller(モノリス版).
|
|
2103
|
+
*/
|
|
2104
|
+
@Controller
|
|
2105
|
+
@RequestMapping("/items")
|
|
2106
|
+
public class ItemWebController {
|
|
2107
|
+
|
|
2108
|
+
private static final int DEFAULT_PAGE_SIZE = 20;
|
|
2109
|
+
private static final int MAX_PAGE_SIZE = 100;
|
|
2110
|
+
|
|
2111
|
+
private final ItemUseCase itemUseCase;
|
|
2112
|
+
|
|
2113
|
+
public ItemWebController(ItemUseCase itemUseCase) {
|
|
2114
|
+
this.itemUseCase = itemUseCase;
|
|
2115
|
+
}
|
|
2116
|
+
|
|
2117
|
+
/**
|
|
2118
|
+
* 品目一覧画面.
|
|
2119
|
+
*/
|
|
2120
|
+
@GetMapping
|
|
2121
|
+
public String list(
|
|
2122
|
+
@RequestParam(defaultValue = "0") int page,
|
|
2123
|
+
@RequestParam(defaultValue = "20") int size,
|
|
2124
|
+
@RequestParam(required = false) ItemCategory category,
|
|
2125
|
+
@RequestParam(required = false) String keyword,
|
|
2126
|
+
Model model) {
|
|
2127
|
+
|
|
2128
|
+
// ページサイズの上限を設定
|
|
2129
|
+
int pageSize = Math.min(size, MAX_PAGE_SIZE);
|
|
2130
|
+
if (pageSize <= 0) {
|
|
2131
|
+
pageSize = DEFAULT_PAGE_SIZE;
|
|
2132
|
+
}
|
|
2133
|
+
|
|
2134
|
+
PageResult<Item> pageResult = itemUseCase.getItems(page, pageSize, category, keyword);
|
|
2135
|
+
|
|
2136
|
+
model.addAttribute("items", pageResult.getContent());
|
|
2137
|
+
model.addAttribute("page", pageResult);
|
|
2138
|
+
model.addAttribute("currentSize", pageSize);
|
|
2139
|
+
model.addAttribute("categories", ItemCategory.values());
|
|
2140
|
+
model.addAttribute("selectedCategory", category);
|
|
2141
|
+
model.addAttribute("keyword", keyword);
|
|
2142
|
+
|
|
2143
|
+
return "items/list";
|
|
2144
|
+
}
|
|
2145
|
+
}
|
|
2146
|
+
```
|
|
2147
|
+
|
|
2148
|
+
</details>
|
|
2149
|
+
|
|
2150
|
+
<details>
|
|
2151
|
+
<summary>ItemService.java(ページネーション対応 UseCase 実装)</summary>
|
|
2152
|
+
|
|
2153
|
+
```java
|
|
2154
|
+
@Override
|
|
2155
|
+
@Transactional(readOnly = true)
|
|
2156
|
+
public PageResult<Item> getItems(int page, int size, ItemCategory category, String keyword) {
|
|
2157
|
+
int offset = page * size;
|
|
2158
|
+
List<Item> items = itemRepository.findWithPagination(category, keyword, size, offset);
|
|
2159
|
+
long totalElements = itemRepository.count(category, keyword);
|
|
2160
|
+
return new PageResult<>(items, page, size, totalElements);
|
|
2161
|
+
}
|
|
2162
|
+
```
|
|
2163
|
+
|
|
2164
|
+
</details>
|
|
2165
|
+
|
|
2166
|
+
---
|
|
2167
|
+
|
|
2168
|
+
### 37.5 ページネーション用テンプレート
|
|
2169
|
+
|
|
2170
|
+
<details>
|
|
2171
|
+
<summary>items/list.html(ページネーション対応)</summary>
|
|
2172
|
+
|
|
2173
|
+
```html
|
|
2174
|
+
<!DOCTYPE html>
|
|
2175
|
+
<html xmlns:th="http://www.thymeleaf.org"
|
|
2176
|
+
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
|
2177
|
+
layout:decorate="~{layout/default}"
|
|
2178
|
+
lang="ja">
|
|
2179
|
+
<head>
|
|
2180
|
+
<title>品目一覧</title>
|
|
2181
|
+
</head>
|
|
2182
|
+
<body>
|
|
2183
|
+
<div layout:fragment="content">
|
|
2184
|
+
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
2185
|
+
<h1>品目一覧</h1>
|
|
2186
|
+
<a th:href="@{/items/new}" class="btn btn-primary">
|
|
2187
|
+
<i class="bi bi-plus-lg"></i> 新規登録
|
|
2188
|
+
</a>
|
|
2189
|
+
</div>
|
|
2190
|
+
|
|
2191
|
+
<!-- 検索フォーム -->
|
|
2192
|
+
<div class="card mb-4">
|
|
2193
|
+
<div class="card-body">
|
|
2194
|
+
<form th:action="@{/items}" method="get" class="row g-3">
|
|
2195
|
+
<div class="col-md-3">
|
|
2196
|
+
<label for="category" class="form-label">品目区分</label>
|
|
2197
|
+
<select id="category" name="category" class="form-select">
|
|
2198
|
+
<option value="">すべて</option>
|
|
2199
|
+
<option th:each="cat : ${categories}"
|
|
2200
|
+
th:value="${cat}"
|
|
2201
|
+
th:text="${cat.displayName}"
|
|
2202
|
+
th:selected="${cat == selectedCategory}">
|
|
2203
|
+
</option>
|
|
2204
|
+
</select>
|
|
2205
|
+
</div>
|
|
2206
|
+
<div class="col-md-3">
|
|
2207
|
+
<label for="keyword" class="form-label">キーワード</label>
|
|
2208
|
+
<input type="text" id="keyword" name="keyword" class="form-control"
|
|
2209
|
+
th:value="${keyword}" placeholder="品目コードまたは品名">
|
|
2210
|
+
</div>
|
|
2211
|
+
<div class="col-md-2">
|
|
2212
|
+
<label for="size" class="form-label">表示件数</label>
|
|
2213
|
+
<select id="size" name="size" class="form-select">
|
|
2214
|
+
<option value="10" th:selected="${currentSize == 10}">10件</option>
|
|
2215
|
+
<option value="20" th:selected="${currentSize == 20}">20件</option>
|
|
2216
|
+
<option value="50" th:selected="${currentSize == 50}">50件</option>
|
|
2217
|
+
<option value="100" th:selected="${currentSize == 100}">100件</option>
|
|
2218
|
+
</select>
|
|
2219
|
+
</div>
|
|
2220
|
+
<div class="col-md-4 d-flex align-items-end">
|
|
2221
|
+
<button type="submit" class="btn btn-secondary me-2">
|
|
2222
|
+
<i class="bi bi-search"></i> 検索
|
|
2223
|
+
</button>
|
|
2224
|
+
<a th:href="@{/items}" class="btn btn-outline-secondary">クリア</a>
|
|
2225
|
+
</div>
|
|
2226
|
+
</form>
|
|
2227
|
+
</div>
|
|
2228
|
+
</div>
|
|
2229
|
+
|
|
2230
|
+
<!-- 品目一覧テーブル -->
|
|
2231
|
+
<div class="card">
|
|
2232
|
+
<div class="card-body">
|
|
2233
|
+
<div th:if="${#lists.isEmpty(items)}" class="alert alert-info">
|
|
2234
|
+
品目が登録されていません。
|
|
2235
|
+
</div>
|
|
2236
|
+
<table th:if="${not #lists.isEmpty(items)}" class="table table-striped table-hover">
|
|
2237
|
+
<thead>
|
|
2238
|
+
<tr>
|
|
2239
|
+
<th>品目コード</th>
|
|
2240
|
+
<th>品名</th>
|
|
2241
|
+
<th>品目区分</th>
|
|
2242
|
+
<th>単位</th>
|
|
2243
|
+
<th>リードタイム</th>
|
|
2244
|
+
<th>安全在庫</th>
|
|
2245
|
+
<th>操作</th>
|
|
2246
|
+
</tr>
|
|
2247
|
+
</thead>
|
|
2248
|
+
<tbody>
|
|
2249
|
+
<tr th:each="item : ${items}">
|
|
2250
|
+
<td>
|
|
2251
|
+
<a th:href="@{/items/{code}(code=${item.itemCode})}"
|
|
2252
|
+
th:text="${item.itemCode}"></a>
|
|
2253
|
+
</td>
|
|
2254
|
+
<td th:text="${item.itemName}"></td>
|
|
2255
|
+
<td th:text="${item.itemCategory?.displayName}"></td>
|
|
2256
|
+
<td th:text="${item.unitCode}"></td>
|
|
2257
|
+
<td th:text="${item.leadTime}"></td>
|
|
2258
|
+
<td th:text="${item.safetyStock}"></td>
|
|
2259
|
+
<td>
|
|
2260
|
+
<a th:href="@{/items/{code}/edit(code=${item.itemCode})}"
|
|
2261
|
+
class="btn btn-sm btn-outline-primary">
|
|
2262
|
+
<i class="bi bi-pencil"></i>
|
|
2263
|
+
</a>
|
|
2264
|
+
<form th:action="@{/items/{code}/delete(code=${item.itemCode})}"
|
|
2265
|
+
method="post" class="d-inline"
|
|
2266
|
+
onsubmit="return confirm('この品目を削除してもよろしいですか?');">
|
|
2267
|
+
<button type="submit" class="btn btn-sm btn-outline-danger">
|
|
2268
|
+
<i class="bi bi-trash"></i>
|
|
2269
|
+
</button>
|
|
2270
|
+
</form>
|
|
2271
|
+
</td>
|
|
2272
|
+
</tr>
|
|
2273
|
+
</tbody>
|
|
2274
|
+
</table>
|
|
2275
|
+
|
|
2276
|
+
<!-- ページネーション -->
|
|
2277
|
+
<div class="mt-3" th:if="${page != null}">
|
|
2278
|
+
<div class="text-muted text-center mb-2">
|
|
2279
|
+
<span th:if="${page.totalElements > 0}">
|
|
2280
|
+
<span th:text="${page.page * page.size + 1}">1</span> -
|
|
2281
|
+
<span th:text="${page.page * page.size + #lists.size(items)}">10</span> 件
|
|
2282
|
+
(全 <span th:text="${page.totalElements}">0</span> 件)
|
|
2283
|
+
</span>
|
|
2284
|
+
<span th:if="${page.totalElements == 0}">0 件</span>
|
|
2285
|
+
</div>
|
|
2286
|
+
|
|
2287
|
+
<nav th:if="${page.totalPages > 1}" aria-label="ページナビゲーション">
|
|
2288
|
+
<ul class="pagination justify-content-center mb-0">
|
|
2289
|
+
<!-- 最初のページへ -->
|
|
2290
|
+
<li class="page-item" th:classappend="${page.first} ? 'disabled'">
|
|
2291
|
+
<a class="page-link" th:href="@{/items(page=0, size=${currentSize}, category=${selectedCategory}, keyword=${keyword})}">«</a>
|
|
2292
|
+
</li>
|
|
2293
|
+
<!-- 前のページへ -->
|
|
2294
|
+
<li class="page-item" th:classappend="${!page.hasPrevious()} ? 'disabled'">
|
|
2295
|
+
<a class="page-link" th:href="@{/items(page=${page.page - 1}, size=${currentSize}, category=${selectedCategory}, keyword=${keyword})}">‹</a>
|
|
2296
|
+
</li>
|
|
2297
|
+
|
|
2298
|
+
<!-- ページ番号 -->
|
|
2299
|
+
<th:block th:with="startPage=${page.page > 2 ? page.page - 2 : 0},
|
|
2300
|
+
endPage=${page.page + 2 < page.totalPages - 1 ? page.page + 2 : page.totalPages - 1}">
|
|
2301
|
+
<li class="page-item" th:if="${startPage > 0}">
|
|
2302
|
+
<span class="page-link">...</span>
|
|
2303
|
+
</li>
|
|
2304
|
+
<li th:each="i : ${#numbers.sequence(startPage, endPage)}"
|
|
2305
|
+
class="page-item"
|
|
2306
|
+
th:classappend="${i == page.page} ? 'active'">
|
|
2307
|
+
<a class="page-link"
|
|
2308
|
+
th:href="@{/items(page=${i}, size=${currentSize}, category=${selectedCategory}, keyword=${keyword})}"
|
|
2309
|
+
th:text="${i + 1}">1</a>
|
|
2310
|
+
</li>
|
|
2311
|
+
<li class="page-item" th:if="${endPage < page.totalPages - 1}">
|
|
2312
|
+
<span class="page-link">...</span>
|
|
2313
|
+
</li>
|
|
2314
|
+
</th:block>
|
|
2315
|
+
|
|
2316
|
+
<!-- 次のページへ -->
|
|
2317
|
+
<li class="page-item" th:classappend="${!page.hasNext()} ? 'disabled'">
|
|
2318
|
+
<a class="page-link" th:href="@{/items(page=${page.page + 1}, size=${currentSize}, category=${selectedCategory}, keyword=${keyword})}">›</a>
|
|
2319
|
+
</li>
|
|
2320
|
+
<!-- 最後のページへ -->
|
|
2321
|
+
<li class="page-item" th:classappend="${page.last} ? 'disabled'">
|
|
2322
|
+
<a class="page-link" th:href="@{/items(page=${page.totalPages - 1}, size=${currentSize}, category=${selectedCategory}, keyword=${keyword})}">»</a>
|
|
2323
|
+
</li>
|
|
2324
|
+
</ul>
|
|
2325
|
+
</nav>
|
|
2326
|
+
</div>
|
|
2327
|
+
</div>
|
|
2328
|
+
</div>
|
|
2329
|
+
</div>
|
|
2330
|
+
</body>
|
|
2331
|
+
</html>
|
|
2332
|
+
```
|
|
2333
|
+
|
|
2334
|
+
</details>
|
|
2335
|
+
|
|
2336
|
+
---
|
|
2337
|
+
|
|
2338
|
+
### 37.6 ページネーションコンポーネントの共通化
|
|
2339
|
+
|
|
2340
|
+
複数の画面で同じページネーションを使用するため、フラグメントとして共通化します。
|
|
2341
|
+
|
|
2342
|
+
<details>
|
|
2343
|
+
<summary>fragments/pagination.html(共通ページネーションコンポーネント)</summary>
|
|
2344
|
+
|
|
2345
|
+
```html
|
|
2346
|
+
<!DOCTYPE html>
|
|
2347
|
+
<html xmlns:th="http://www.thymeleaf.org">
|
|
2348
|
+
|
|
2349
|
+
<!-- ページネーションコンポーネント -->
|
|
2350
|
+
<nav th:fragment="pagination(page, currentSize, baseUrl, params)" th:if="${page.totalPages > 1}" class="mt-4">
|
|
2351
|
+
<ul class="pagination justify-content-center">
|
|
2352
|
+
<!-- 最初へ -->
|
|
2353
|
+
<li class="page-item" th:classappend="${page.first} ? 'disabled'">
|
|
2354
|
+
<a class="page-link"
|
|
2355
|
+
th:href="${baseUrl + '?page=0&size=' + currentSize + (params != null ? '&' + params : '')}"
|
|
2356
|
+
th:if="${!page.first}">«</a>
|
|
2357
|
+
<span class="page-link" th:if="${page.first}">«</span>
|
|
2358
|
+
</li>
|
|
2359
|
+
|
|
2360
|
+
<!-- 前へ -->
|
|
2361
|
+
<li class="page-item" th:classappend="${!page.hasPrevious()} ? 'disabled'">
|
|
2362
|
+
<a class="page-link"
|
|
2363
|
+
th:href="${baseUrl + '?page=' + (page.page - 1) + '&size=' + currentSize + (params != null ? '&' + params : '')}"
|
|
2364
|
+
th:if="${page.hasPrevious()}">‹</a>
|
|
2365
|
+
<span class="page-link" th:if="${!page.hasPrevious()}">‹</span>
|
|
2366
|
+
</li>
|
|
2367
|
+
|
|
2368
|
+
<!-- ページ番号(前後2ページ表示) -->
|
|
2369
|
+
<th:block th:with="startPage=${page.page > 2 ? page.page - 2 : 0},
|
|
2370
|
+
endPage=${page.page + 2 < page.totalPages - 1 ? page.page + 2 : page.totalPages - 1}">
|
|
2371
|
+
<li class="page-item" th:if="${startPage > 0}">
|
|
2372
|
+
<span class="page-link">...</span>
|
|
2373
|
+
</li>
|
|
2374
|
+
<li th:each="i : ${#numbers.sequence(startPage, endPage)}"
|
|
2375
|
+
class="page-item"
|
|
2376
|
+
th:classappend="${i == page.page} ? 'active'">
|
|
2377
|
+
<a class="page-link"
|
|
2378
|
+
th:href="${baseUrl + '?page=' + i + '&size=' + currentSize + (params != null ? '&' + params : '')}"
|
|
2379
|
+
th:text="${i + 1}"></a>
|
|
2380
|
+
</li>
|
|
2381
|
+
<li class="page-item" th:if="${endPage < page.totalPages - 1}">
|
|
2382
|
+
<span class="page-link">...</span>
|
|
2383
|
+
</li>
|
|
2384
|
+
</th:block>
|
|
2385
|
+
|
|
2386
|
+
<!-- 次へ -->
|
|
2387
|
+
<li class="page-item" th:classappend="${!page.hasNext()} ? 'disabled'">
|
|
2388
|
+
<a class="page-link"
|
|
2389
|
+
th:href="${baseUrl + '?page=' + (page.page + 1) + '&size=' + currentSize + (params != null ? '&' + params : '')}"
|
|
2390
|
+
th:if="${page.hasNext()}">›</a>
|
|
2391
|
+
<span class="page-link" th:if="${!page.hasNext()}">›</span>
|
|
2392
|
+
</li>
|
|
2393
|
+
|
|
2394
|
+
<!-- 最後へ -->
|
|
2395
|
+
<li class="page-item" th:classappend="${page.last} ? 'disabled'">
|
|
2396
|
+
<a class="page-link"
|
|
2397
|
+
th:href="${baseUrl + '?page=' + (page.totalPages - 1) + '&size=' + currentSize + (params != null ? '&' + params : '')}"
|
|
2398
|
+
th:if="${!page.last}">»</a>
|
|
2399
|
+
<span class="page-link" th:if="${page.last}">»</span>
|
|
2400
|
+
</li>
|
|
2401
|
+
</ul>
|
|
2402
|
+
|
|
2403
|
+
<!-- ページ情報 -->
|
|
2404
|
+
<div class="text-center text-muted small">
|
|
2405
|
+
<span th:text="${page.page + 1}"></span> /
|
|
2406
|
+
<span th:text="${page.totalPages}"></span> ページ
|
|
2407
|
+
(全 <span th:text="${page.totalElements}"></span> 件)
|
|
2408
|
+
</div>
|
|
2409
|
+
</nav>
|
|
2410
|
+
|
|
2411
|
+
</html>
|
|
2412
|
+
```
|
|
2413
|
+
|
|
2414
|
+
</details>
|
|
2415
|
+
|
|
2416
|
+
<details>
|
|
2417
|
+
<summary>使用例</summary>
|
|
2418
|
+
|
|
2419
|
+
```html
|
|
2420
|
+
<!-- 品目一覧での使用例 -->
|
|
2421
|
+
<th:block th:replace="~{fragments/pagination :: pagination(
|
|
2422
|
+
${page},
|
|
2423
|
+
${currentSize},
|
|
2424
|
+
'/items',
|
|
2425
|
+
'category=' + ${selectedCategory} + '&keyword=' + ${keyword}
|
|
2426
|
+
)}"></th:block>
|
|
2427
|
+
|
|
2428
|
+
<!-- 発注一覧での使用例 -->
|
|
2429
|
+
<th:block th:replace="~{fragments/pagination :: pagination(
|
|
2430
|
+
${page},
|
|
2431
|
+
${currentSize},
|
|
2432
|
+
'/purchase-orders',
|
|
2433
|
+
'status=' + ${status}
|
|
2434
|
+
)}"></th:block>
|
|
2435
|
+
```
|
|
2436
|
+
|
|
2437
|
+
</details>
|
|
2438
|
+
|
|
2439
|
+
---
|
|
2440
|
+
|
|
2441
|
+
### 37.7 htmx によるページネーションの部分更新
|
|
2442
|
+
|
|
2443
|
+
ページ遷移時に画面全体をリロードせず、テーブル部分のみを更新します。
|
|
2444
|
+
|
|
2445
|
+
<details>
|
|
2446
|
+
<summary>htmx 対応ページネーション</summary>
|
|
2447
|
+
|
|
2448
|
+
```html
|
|
2449
|
+
<!-- ページネーションを htmx で部分更新 -->
|
|
2450
|
+
<div id="items-container">
|
|
2451
|
+
<!-- テーブルとページネーションを含むコンテナ -->
|
|
2452
|
+
<th:block th:replace="~{items/fragments :: itemsTable}"></th:block>
|
|
2453
|
+
</div>
|
|
2454
|
+
|
|
2455
|
+
<!-- items/fragments.html -->
|
|
2456
|
+
<!DOCTYPE html>
|
|
2457
|
+
<html xmlns:th="http://www.thymeleaf.org">
|
|
2458
|
+
|
|
2459
|
+
<div th:fragment="itemsTable">
|
|
2460
|
+
<!-- 件数表示 -->
|
|
2461
|
+
<div class="d-flex justify-content-between align-items-center mb-3">
|
|
2462
|
+
<div class="text-muted">
|
|
2463
|
+
全 <span th:text="${page.totalElements}">0</span> 件中
|
|
2464
|
+
<span th:text="${page.page * page.size + 1}">1</span> -
|
|
2465
|
+
<span th:text="${page.page * page.size + #lists.size(items)}">20</span> 件を表示
|
|
2466
|
+
</div>
|
|
2467
|
+
</div>
|
|
2468
|
+
|
|
2469
|
+
<!-- テーブル -->
|
|
2470
|
+
<table class="table table-striped table-hover">
|
|
2471
|
+
<!-- ... テーブル内容 ... -->
|
|
2472
|
+
</table>
|
|
2473
|
+
|
|
2474
|
+
<!-- ページネーション(htmx対応) -->
|
|
2475
|
+
<nav th:if="${page.totalPages > 1}" class="mt-4">
|
|
2476
|
+
<ul class="pagination justify-content-center">
|
|
2477
|
+
<th:block th:with="startPage=${page.page > 2 ? page.page - 2 : 0},
|
|
2478
|
+
endPage=${page.page + 2 < page.totalPages - 1 ? page.page + 2 : page.totalPages - 1}">
|
|
2479
|
+
<li th:each="i : ${#numbers.sequence(startPage, endPage)}"
|
|
2480
|
+
class="page-item"
|
|
2481
|
+
th:classappend="${i == page.page} ? 'active'">
|
|
2482
|
+
<a class="page-link"
|
|
2483
|
+
th:href="@{/items(page=${i}, size=${currentSize})}"
|
|
2484
|
+
hx-get="/items/table"
|
|
2485
|
+
th:attr="hx-get=@{/items/table(page=${i}, size=${currentSize})}"
|
|
2486
|
+
hx-target="#items-container"
|
|
2487
|
+
hx-swap="innerHTML"
|
|
2488
|
+
hx-push-url="true"
|
|
2489
|
+
th:text="${i + 1}"></a>
|
|
2490
|
+
</li>
|
|
2491
|
+
</th:block>
|
|
2492
|
+
</ul>
|
|
2493
|
+
</nav>
|
|
2494
|
+
</div>
|
|
2495
|
+
|
|
2496
|
+
</html>
|
|
2497
|
+
```
|
|
2498
|
+
|
|
2499
|
+
</details>
|
|
2500
|
+
|
|
2501
|
+
<details>
|
|
2502
|
+
<summary>Controller(htmx 対応)</summary>
|
|
2503
|
+
|
|
2504
|
+
```java
|
|
2505
|
+
@Controller
|
|
2506
|
+
@RequestMapping("/items")
|
|
2507
|
+
public class ItemWebController {
|
|
2508
|
+
|
|
2509
|
+
/**
|
|
2510
|
+
* テーブル部分のみを返す(htmx用)
|
|
2511
|
+
*/
|
|
2512
|
+
@GetMapping("/table")
|
|
2513
|
+
public String table(
|
|
2514
|
+
@RequestParam(defaultValue = "0") int page,
|
|
2515
|
+
@RequestParam(defaultValue = "20") int size,
|
|
2516
|
+
@RequestParam(required = false) ItemCategory category,
|
|
2517
|
+
@RequestParam(required = false) String keyword,
|
|
2518
|
+
Model model) {
|
|
2519
|
+
|
|
2520
|
+
int pageSize = Math.min(size, MAX_PAGE_SIZE);
|
|
2521
|
+
PageResult<Item> pageResult = itemUseCase.getItems(page, pageSize, category, keyword);
|
|
2522
|
+
|
|
2523
|
+
model.addAttribute("items", pageResult.getContent());
|
|
2524
|
+
model.addAttribute("page", pageResult);
|
|
2525
|
+
model.addAttribute("currentSize", pageSize);
|
|
2526
|
+
|
|
2527
|
+
// フラグメントのみを返す
|
|
2528
|
+
return "items/fragments :: itemsTable";
|
|
2529
|
+
}
|
|
2530
|
+
}
|
|
2531
|
+
```
|
|
2532
|
+
|
|
2533
|
+
</details>
|
|
2534
|
+
|
|
2535
|
+
---
|
|
2536
|
+
|
|
2537
|
+
## まとめ
|
|
2538
|
+
|
|
2539
|
+
本研究では、モノリスアーキテクチャによる生産管理システムを実装しました。
|
|
2540
|
+
|
|
2541
|
+
### API サーバー版との比較
|
|
2542
|
+
|
|
2543
|
+
| 観点 | API サーバー版(第32章) | モノリス版(本研究) |
|
|
2544
|
+
|------|------------------------|----------------------|
|
|
2545
|
+
| **アーキテクチャ** | フロントエンド分離(SPA) | 統合(Thymeleaf) |
|
|
2546
|
+
| **通信** | REST API(JSON) | サーバーサイドレンダリング(HTML) |
|
|
2547
|
+
| **状態管理** | クライアント側(localStorage等) | サーバー側(セッション) |
|
|
2548
|
+
| **部分更新** | JavaScript(Axios等) | htmx |
|
|
2549
|
+
| **帳票出力** | API経由でダウンロード | 直接ダウンロード |
|
|
2550
|
+
| **認証** | JWT/OAuth | セッションベース |
|
|
2551
|
+
| **複雑さ** | 高(API設計、CORS等) | 低(シンプル) |
|
|
2552
|
+
| **開発速度** | 初期は遅い | 初期は速い |
|
|
2553
|
+
|
|
2554
|
+
### 実装した機能
|
|
2555
|
+
|
|
2556
|
+
| カテゴリ | 機能 |
|
|
2557
|
+
|---------|------|
|
|
2558
|
+
| **品目マスタ** | 一覧、詳細、登録、編集、削除、検索 |
|
|
2559
|
+
| **BOM** | 階層表示(ツリー)、逆展開 |
|
|
2560
|
+
| **発注** | 一覧、登録(動的明細)、確定、取消 |
|
|
2561
|
+
| **在庫** | 照会、不足品目表示 |
|
|
2562
|
+
| **MRP** | 実行、結果表示 |
|
|
2563
|
+
| **帳票** | Excel 出力 |
|
|
2564
|
+
| **ページネーション** | ページ分割、ソート、件数表示、htmx 対応 |
|
|
2565
|
+
| **共通** | レイアウト、エラーハンドリング、フラッシュメッセージ |
|
|
2566
|
+
|
|
2567
|
+
### 技術スタック
|
|
2568
|
+
|
|
2569
|
+
- **Spring Boot 3.2**: Web フレームワーク
|
|
2570
|
+
- **Thymeleaf**: テンプレートエンジン
|
|
2571
|
+
- **htmx**: 部分更新(JavaScript 不要)
|
|
2572
|
+
- **Bootstrap 5**: CSS フレームワーク
|
|
2573
|
+
- **Apache POI**: Excel 出力
|
|
2574
|
+
- **MyBatis**: O/R マッピング
|
|
2575
|
+
- **Testcontainers**: 統合テスト
|
|
2576
|
+
|
|
2577
|
+
### モノリスを選択すべき場面
|
|
2578
|
+
|
|
2579
|
+
1. **社内システム**: 限られたユーザー、SEO 不要
|
|
2580
|
+
2. **業務アプリケーション**: 複雑な業務フロー
|
|
2581
|
+
3. **小規模チーム**: フルスタック開発
|
|
2582
|
+
4. **迅速な開発**: MVP、プロトタイプ
|
|
2583
|
+
5. **運用コスト重視**: シンプルなインフラ
|
|
2584
|
+
|
|
2585
|
+
---
|
|
2586
|
+
|
|
2587
|
+
[目次へ戻る](../index.md)
|