@k2works/claude-code-booster 3.2.1 → 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/package.json +1 -1
|
@@ -0,0 +1,1693 @@
|
|
|
1
|
+
# 実践データベース設計:販売管理システム 研究 1 - モノリスサービスの実装
|
|
2
|
+
|
|
3
|
+
## はじめに
|
|
4
|
+
|
|
5
|
+
本研究では、API サーバー構成(第13章)とは異なるアプローチとして、**モノリスアーキテクチャ**による販売管理システムを実装します。UI(テンプレートエンジン)、ビジネスロジック、データベースアクセスがすべて同一サーバー内で動作する、伝統的かつ堅実なアーキテクチャです。
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 第14章:モノリスアーキテクチャの基礎
|
|
10
|
+
|
|
11
|
+
### 14.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
|
+
### 14.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
|
+
### 14.3 モノリスを選択すべき場面
|
|
136
|
+
|
|
137
|
+
**モノリスが適している状況:**
|
|
138
|
+
|
|
139
|
+
1. **小〜中規模のチーム**:専門のフロントエンドチームがいない場合
|
|
140
|
+
2. **社内システム**:SEO 不要、限られたユーザー数
|
|
141
|
+
3. **業務アプリケーション**:複雑な業務フローをサーバーサイドで処理
|
|
142
|
+
4. **迅速な開発**:MVP やプロトタイプの素早い構築
|
|
143
|
+
5. **運用コスト重視**:インフラ構成をシンプルに保ちたい場合
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
### 14.4 モノリスにおけるヘキサゴナルアーキテクチャ
|
|
148
|
+
|
|
149
|
+
モノリスであっても、ヘキサゴナルアーキテクチャ(Ports and Adapters)を採用することで、保守性の高い設計を実現できます。
|
|
150
|
+
|
|
151
|
+
```plantuml
|
|
152
|
+
@startuml hexagonal_monolith
|
|
153
|
+
!define RECTANGLE class
|
|
154
|
+
|
|
155
|
+
package "Hexagonal Architecture (モノリス版)" {
|
|
156
|
+
|
|
157
|
+
RECTANGLE "Application Core\n(Domain + Use Cases)" as core {
|
|
158
|
+
- Product (商品)
|
|
159
|
+
- Partner (取引先)
|
|
160
|
+
- SalesOrder (受注)
|
|
161
|
+
- Shipment (出荷)
|
|
162
|
+
- Invoice (請求)
|
|
163
|
+
- ProductUseCase
|
|
164
|
+
- SalesOrderUseCase
|
|
165
|
+
- InvoiceUseCase
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
RECTANGLE "Input Adapters\n(Driving Side)" as input {
|
|
169
|
+
- Thymeleaf Controller
|
|
170
|
+
- フォーム処理
|
|
171
|
+
- セッション管理
|
|
172
|
+
- リクエスト検証
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
RECTANGLE "Output Adapters\n(Driven Side)" as output {
|
|
176
|
+
- MyBatis Repository
|
|
177
|
+
- Database Access
|
|
178
|
+
- Entity Mapping
|
|
179
|
+
- 帳票出力
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
input --> core : "Input Ports\n(Use Cases)"
|
|
184
|
+
core --> output : "Output Ports\n(Repository Interfaces)"
|
|
185
|
+
|
|
186
|
+
note top of core
|
|
187
|
+
純粋なビジネスロジック
|
|
188
|
+
UI 技術(Thymeleaf)に依存しない
|
|
189
|
+
テスト可能な状態を維持
|
|
190
|
+
end note
|
|
191
|
+
|
|
192
|
+
note left of input
|
|
193
|
+
HTML フォームからの入力
|
|
194
|
+
サーバーサイドレンダリング
|
|
195
|
+
セッションベースの認証
|
|
196
|
+
end note
|
|
197
|
+
|
|
198
|
+
note right of output
|
|
199
|
+
PostgreSQL + MyBatis
|
|
200
|
+
帳票生成(PDF/Excel)
|
|
201
|
+
ファイル出力
|
|
202
|
+
end note
|
|
203
|
+
|
|
204
|
+
@enduml
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
### 14.5 ディレクトリ構成
|
|
210
|
+
|
|
211
|
+
```
|
|
212
|
+
apps/sms/backend/src/main/java/com/example/sms/
|
|
213
|
+
├── domain/ # ドメイン層(API版と共通)
|
|
214
|
+
│ ├── model/
|
|
215
|
+
│ │ ├── product/
|
|
216
|
+
│ │ ├── partner/
|
|
217
|
+
│ │ ├── sales/
|
|
218
|
+
│ │ ├── shipping/
|
|
219
|
+
│ │ ├── invoice/
|
|
220
|
+
│ │ └── receipt/
|
|
221
|
+
│ └── exception/
|
|
222
|
+
│
|
|
223
|
+
├── application/ # アプリケーション層(API版と共通)
|
|
224
|
+
│ ├── port/
|
|
225
|
+
│ │ ├── in/ # Input Port(ユースケース)
|
|
226
|
+
│ │ └── out/ # Output Port(リポジトリ)
|
|
227
|
+
│ └── service/
|
|
228
|
+
│
|
|
229
|
+
├── infrastructure/
|
|
230
|
+
│ ├── out/
|
|
231
|
+
│ │ └── persistence/ # Output Adapter(DB実装)
|
|
232
|
+
│ │ ├── mapper/
|
|
233
|
+
│ │ ├── repository/
|
|
234
|
+
│ │ └── typehandler/
|
|
235
|
+
│ └── in/
|
|
236
|
+
│ └── web/ # Input Adapter(Web実装)
|
|
237
|
+
│ ├── controller/ # Thymeleaf Controller
|
|
238
|
+
│ ├── form/ # フォームオブジェクト
|
|
239
|
+
│ └── helper/ # ビューヘルパー
|
|
240
|
+
│
|
|
241
|
+
└── config/
|
|
242
|
+
|
|
243
|
+
apps/sms/backend/src/main/resources/
|
|
244
|
+
├── templates/ # Thymeleaf テンプレート
|
|
245
|
+
│ ├── layout/ # 共通レイアウト
|
|
246
|
+
│ ├── products/ # 商品マスタ画面
|
|
247
|
+
│ ├── partners/ # 取引先マスタ画面
|
|
248
|
+
│ ├── orders/ # 受注画面
|
|
249
|
+
│ ├── shipments/ # 出荷画面
|
|
250
|
+
│ └── invoices/ # 請求画面
|
|
251
|
+
├── static/ # 静的リソース
|
|
252
|
+
│ ├── css/
|
|
253
|
+
│ ├── js/
|
|
254
|
+
│ └── images/
|
|
255
|
+
├── mapper/ # MyBatis Mapper XML
|
|
256
|
+
└── messages.properties # メッセージリソース
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
---
|
|
260
|
+
|
|
261
|
+
### 14.6 技術スタックの導入
|
|
262
|
+
|
|
263
|
+
#### build.gradle.kts
|
|
264
|
+
|
|
265
|
+
<details>
|
|
266
|
+
<summary>コード例: build.gradle.kts</summary>
|
|
267
|
+
|
|
268
|
+
```kotlin
|
|
269
|
+
plugins {
|
|
270
|
+
id("java")
|
|
271
|
+
id("org.springframework.boot") version "3.4.1"
|
|
272
|
+
id("io.spring.dependency-management") version "1.1.7"
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
java {
|
|
276
|
+
toolchain {
|
|
277
|
+
languageVersion = JavaLanguageVersion.of(21)
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
dependencies {
|
|
282
|
+
// Spring Boot Web(REST APIではなくMVCとして使用)
|
|
283
|
+
implementation("org.springframework.boot:spring-boot-starter-web")
|
|
284
|
+
implementation("org.springframework.boot:spring-boot-starter-validation")
|
|
285
|
+
|
|
286
|
+
// Thymeleaf(テンプレートエンジン)
|
|
287
|
+
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
|
|
288
|
+
implementation("nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:3.4.0")
|
|
289
|
+
|
|
290
|
+
// htmx(モダンなインタラクティブUI)
|
|
291
|
+
// CDN から読み込むため依存関係は不要
|
|
292
|
+
|
|
293
|
+
// MyBatis
|
|
294
|
+
implementation("org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.4")
|
|
295
|
+
|
|
296
|
+
// PostgreSQL
|
|
297
|
+
runtimeOnly("org.postgresql:postgresql")
|
|
298
|
+
|
|
299
|
+
// Flyway
|
|
300
|
+
implementation("org.flywaydb:flyway-core")
|
|
301
|
+
implementation("org.flywaydb:flyway-database-postgresql")
|
|
302
|
+
|
|
303
|
+
// Webjars(Bootstrap等のフロントエンドライブラリ)
|
|
304
|
+
implementation("org.webjars:bootstrap:5.3.3")
|
|
305
|
+
implementation("org.webjars:webjars-locator-core:0.59")
|
|
306
|
+
|
|
307
|
+
// 帳票出力
|
|
308
|
+
implementation("org.apache.poi:poi-ooxml:5.3.0") // Excel
|
|
309
|
+
implementation("io.github.openhtmltopdf:openhtmltopdf-pdfbox:1.1.22") // PDF(Thymeleaf + HTML/CSS)
|
|
310
|
+
implementation("io.github.openhtmltopdf:openhtmltopdf-slf4j:1.1.22")
|
|
311
|
+
|
|
312
|
+
// Test
|
|
313
|
+
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
|
314
|
+
testImplementation("org.testcontainers:postgresql:1.20.4")
|
|
315
|
+
testImplementation("org.testcontainers:junit-jupiter:1.20.4")
|
|
316
|
+
|
|
317
|
+
// Lombok
|
|
318
|
+
compileOnly("org.projectlombok:lombok")
|
|
319
|
+
annotationProcessor("org.projectlombok:lombok")
|
|
320
|
+
}
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
</details>
|
|
324
|
+
|
|
325
|
+
---
|
|
326
|
+
|
|
327
|
+
### 14.7 共通レイアウト(layout/default.html)
|
|
328
|
+
|
|
329
|
+
<details>
|
|
330
|
+
<summary>コード例: layout/default.html</summary>
|
|
331
|
+
|
|
332
|
+
```html
|
|
333
|
+
<!DOCTYPE html>
|
|
334
|
+
<html xmlns:th="http://www.thymeleaf.org"
|
|
335
|
+
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
|
336
|
+
lang="ja">
|
|
337
|
+
<head>
|
|
338
|
+
<meta charset="UTF-8">
|
|
339
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
340
|
+
<title layout:title-pattern="$CONTENT_TITLE - $LAYOUT_TITLE">販売管理システム</title>
|
|
341
|
+
|
|
342
|
+
<!-- Bootstrap CSS -->
|
|
343
|
+
<link rel="stylesheet" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}">
|
|
344
|
+
|
|
345
|
+
<!-- カスタム CSS -->
|
|
346
|
+
<link rel="stylesheet" th:href="@{/css/style.css}">
|
|
347
|
+
|
|
348
|
+
<!-- htmx(部分更新用) -->
|
|
349
|
+
<script src="https://unpkg.com/htmx.org@1.9.12"></script>
|
|
350
|
+
</head>
|
|
351
|
+
<body>
|
|
352
|
+
<!-- ナビゲーションバー -->
|
|
353
|
+
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
|
|
354
|
+
<div class="container-fluid">
|
|
355
|
+
<a class="navbar-brand" th:href="@{/}">販売管理システム</a>
|
|
356
|
+
<div class="collapse navbar-collapse" id="navbarNav">
|
|
357
|
+
<ul class="navbar-nav">
|
|
358
|
+
<li class="nav-item dropdown">
|
|
359
|
+
<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown">マスタ</a>
|
|
360
|
+
<ul class="dropdown-menu">
|
|
361
|
+
<li><a class="dropdown-item" th:href="@{/products}">商品マスタ</a></li>
|
|
362
|
+
<li><a class="dropdown-item" th:href="@{/partners}">取引先マスタ</a></li>
|
|
363
|
+
</ul>
|
|
364
|
+
</li>
|
|
365
|
+
<li class="nav-item dropdown">
|
|
366
|
+
<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown">販売</a>
|
|
367
|
+
<ul class="dropdown-menu">
|
|
368
|
+
<li><a class="dropdown-item" th:href="@{/orders}">受注</a></li>
|
|
369
|
+
<li><a class="dropdown-item" th:href="@{/shipments}">出荷</a></li>
|
|
370
|
+
</ul>
|
|
371
|
+
</li>
|
|
372
|
+
<li class="nav-item dropdown">
|
|
373
|
+
<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown">債権</a>
|
|
374
|
+
<ul class="dropdown-menu">
|
|
375
|
+
<li><a class="dropdown-item" th:href="@{/invoices}">請求</a></li>
|
|
376
|
+
<li><a class="dropdown-item" th:href="@{/receipts}">入金</a></li>
|
|
377
|
+
</ul>
|
|
378
|
+
</li>
|
|
379
|
+
</ul>
|
|
380
|
+
</div>
|
|
381
|
+
</div>
|
|
382
|
+
</nav>
|
|
383
|
+
|
|
384
|
+
<!-- フラッシュメッセージ -->
|
|
385
|
+
<div class="container mt-3">
|
|
386
|
+
<div th:if="${successMessage}" class="alert alert-success alert-dismissible fade show">
|
|
387
|
+
<span th:text="${successMessage}"></span>
|
|
388
|
+
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
|
389
|
+
</div>
|
|
390
|
+
<div th:if="${errorMessage}" class="alert alert-danger alert-dismissible fade show">
|
|
391
|
+
<span th:text="${errorMessage}"></span>
|
|
392
|
+
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
|
393
|
+
</div>
|
|
394
|
+
</div>
|
|
395
|
+
|
|
396
|
+
<!-- メインコンテンツ -->
|
|
397
|
+
<main class="container mt-4">
|
|
398
|
+
<div layout:fragment="content"></div>
|
|
399
|
+
</main>
|
|
400
|
+
|
|
401
|
+
<!-- Bootstrap JS -->
|
|
402
|
+
<script th:src="@{/webjars/bootstrap/js/bootstrap.bundle.min.js}"></script>
|
|
403
|
+
<th:block layout:fragment="scripts"></th:block>
|
|
404
|
+
</body>
|
|
405
|
+
</html>
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
</details>
|
|
409
|
+
|
|
410
|
+
---
|
|
411
|
+
|
|
412
|
+
### 14.8 htmx による部分更新
|
|
413
|
+
|
|
414
|
+
モノリスアプリケーションでも、**htmx** を使用することで SPA のような操作性を実現できます。
|
|
415
|
+
|
|
416
|
+
```plantuml
|
|
417
|
+
@startuml htmx_flow
|
|
418
|
+
title htmx による部分更新
|
|
419
|
+
|
|
420
|
+
actor "ブラウザ" as Browser
|
|
421
|
+
participant "htmx" as htmx
|
|
422
|
+
participant "Controller" as Controller
|
|
423
|
+
participant "Thymeleaf\nフラグメント" as Fragment
|
|
424
|
+
|
|
425
|
+
Browser -> htmx : ボタンクリック
|
|
426
|
+
htmx -> Controller : AJAX リクエスト\n(hx-get/hx-post)
|
|
427
|
+
Controller -> Fragment : フラグメントを\nレンダリング
|
|
428
|
+
Fragment --> Controller : HTML フラグメント
|
|
429
|
+
Controller --> htmx : HTML レスポンス
|
|
430
|
+
htmx -> Browser : DOM を部分更新\n(hx-target)
|
|
431
|
+
|
|
432
|
+
note right of htmx
|
|
433
|
+
htmx は特別な属性(hx-*)を
|
|
434
|
+
使って AJAX 通信を行う
|
|
435
|
+
JavaScript を書かずに
|
|
436
|
+
インタラクティブな UI を実現
|
|
437
|
+
end note
|
|
438
|
+
|
|
439
|
+
@enduml
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
#### htmx の基本的な使い方
|
|
443
|
+
|
|
444
|
+
<details>
|
|
445
|
+
<summary>コード例: htmx による商品検索</summary>
|
|
446
|
+
|
|
447
|
+
```html
|
|
448
|
+
<!-- 商品検索(部分更新) -->
|
|
449
|
+
<input type="text" class="form-control"
|
|
450
|
+
hx-get="/products/search"
|
|
451
|
+
hx-trigger="keyup changed delay:300ms"
|
|
452
|
+
hx-target="#search-results"
|
|
453
|
+
hx-indicator="#loading">
|
|
454
|
+
<span id="loading" class="htmx-indicator">検索中...</span>
|
|
455
|
+
|
|
456
|
+
<div id="search-results">
|
|
457
|
+
<!-- Controller から返される HTML フラグメント -->
|
|
458
|
+
</div>
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
</details>
|
|
462
|
+
|
|
463
|
+
---
|
|
464
|
+
|
|
465
|
+
## 第15章:マスタの実装
|
|
466
|
+
|
|
467
|
+
### 15.1 商品マスタ画面の設計
|
|
468
|
+
|
|
469
|
+
| 画面 | URL | メソッド | 説明 |
|
|
470
|
+
|------|-----|---------|------|
|
|
471
|
+
| 商品一覧 | /products | GET | 商品の検索・一覧表示 |
|
|
472
|
+
| 商品詳細 | /products/{productCode} | GET | 商品の詳細表示 |
|
|
473
|
+
| 商品登録 | /products/new | GET | 登録フォーム表示 |
|
|
474
|
+
| 商品登録処理 | /products | POST | 登録処理 |
|
|
475
|
+
| 商品編集 | /products/{productCode}/edit | GET | 編集フォーム表示 |
|
|
476
|
+
| 商品更新処理 | /products/{productCode} | POST | 更新処理(PUT 代替) |
|
|
477
|
+
| 商品削除処理 | /products/{productCode}/delete | POST | 削除処理(DELETE 代替) |
|
|
478
|
+
|
|
479
|
+
### 15.2 フォームオブジェクトの設計
|
|
480
|
+
|
|
481
|
+
<details>
|
|
482
|
+
<summary>コード例: ProductForm.java</summary>
|
|
483
|
+
|
|
484
|
+
```java
|
|
485
|
+
package com.example.sms.infrastructure.in.web.form;
|
|
486
|
+
|
|
487
|
+
import com.example.sms.application.port.in.CreateProductCommand;
|
|
488
|
+
import com.example.sms.domain.model.product.ProductCategory;
|
|
489
|
+
import jakarta.validation.constraints.NotBlank;
|
|
490
|
+
import jakarta.validation.constraints.NotNull;
|
|
491
|
+
import jakarta.validation.constraints.Positive;
|
|
492
|
+
import jakarta.validation.constraints.Size;
|
|
493
|
+
import lombok.Data;
|
|
494
|
+
|
|
495
|
+
import java.math.BigDecimal;
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* 商品登録・編集フォーム
|
|
499
|
+
*/
|
|
500
|
+
@Data
|
|
501
|
+
public class ProductForm {
|
|
502
|
+
|
|
503
|
+
@NotBlank(message = "商品コードは必須です")
|
|
504
|
+
@Size(max = 20, message = "商品コードは20文字以内で入力してください")
|
|
505
|
+
private String productCode;
|
|
506
|
+
|
|
507
|
+
@NotBlank(message = "商品名は必須です")
|
|
508
|
+
@Size(max = 100, message = "商品名は100文字以内で入力してください")
|
|
509
|
+
private String productName;
|
|
510
|
+
|
|
511
|
+
@NotNull(message = "商品区分は必須です")
|
|
512
|
+
private ProductCategory category;
|
|
513
|
+
|
|
514
|
+
@Positive(message = "単価は正の数で入力してください")
|
|
515
|
+
private BigDecimal unitPrice;
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* フォームをコマンドに変換
|
|
519
|
+
*/
|
|
520
|
+
public CreateProductCommand toCommand() {
|
|
521
|
+
return CreateProductCommand.builder()
|
|
522
|
+
.productCode(this.productCode)
|
|
523
|
+
.productName(this.productName)
|
|
524
|
+
.category(this.category)
|
|
525
|
+
.unitPrice(this.unitPrice)
|
|
526
|
+
.build();
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* エンティティからフォームを生成
|
|
531
|
+
*/
|
|
532
|
+
public static ProductForm from(Product product) {
|
|
533
|
+
ProductForm form = new ProductForm();
|
|
534
|
+
form.setProductCode(product.getProductCode());
|
|
535
|
+
form.setProductName(product.getProductName());
|
|
536
|
+
form.setCategory(product.getCategory());
|
|
537
|
+
form.setUnitPrice(product.getUnitPrice());
|
|
538
|
+
return form;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
```
|
|
542
|
+
|
|
543
|
+
</details>
|
|
544
|
+
|
|
545
|
+
### 15.3 商品 Controller の TDD 実装
|
|
546
|
+
|
|
547
|
+
<details>
|
|
548
|
+
<summary>コード例: ProductControllerTest.java</summary>
|
|
549
|
+
|
|
550
|
+
```java
|
|
551
|
+
@SpringBootTest
|
|
552
|
+
@AutoConfigureMockMvc
|
|
553
|
+
@Testcontainers
|
|
554
|
+
@DisplayName("商品マスタ画面")
|
|
555
|
+
class ProductControllerTest {
|
|
556
|
+
|
|
557
|
+
@Container
|
|
558
|
+
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16")
|
|
559
|
+
.withDatabaseName("sms_test");
|
|
560
|
+
|
|
561
|
+
@DynamicPropertySource
|
|
562
|
+
static void configureProperties(DynamicPropertyRegistry registry) {
|
|
563
|
+
registry.add("spring.datasource.url", postgres::getJdbcUrl);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
@Autowired
|
|
567
|
+
private MockMvc mockMvc;
|
|
568
|
+
|
|
569
|
+
@Test
|
|
570
|
+
@DisplayName("商品一覧画面を表示できる")
|
|
571
|
+
void shouldDisplayProductList() throws Exception {
|
|
572
|
+
mockMvc.perform(get("/products"))
|
|
573
|
+
.andExpect(status().isOk())
|
|
574
|
+
.andExpect(view().name("products/list"))
|
|
575
|
+
.andExpect(model().attributeExists("products"));
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
@Test
|
|
579
|
+
@DisplayName("商品を登録できる")
|
|
580
|
+
void shouldCreateProduct() throws Exception {
|
|
581
|
+
mockMvc.perform(post("/products")
|
|
582
|
+
.param("productCode", "NEW-001")
|
|
583
|
+
.param("productName", "新規商品")
|
|
584
|
+
.param("category", "FINISHED_GOODS")
|
|
585
|
+
.param("unitPrice", "1000"))
|
|
586
|
+
.andExpect(status().is3xxRedirection())
|
|
587
|
+
.andExpect(redirectedUrl("/products"))
|
|
588
|
+
.andExpect(flash().attribute("successMessage", containsString("登録")));
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
```
|
|
592
|
+
|
|
593
|
+
</details>
|
|
594
|
+
|
|
595
|
+
### 15.4 商品 Controller
|
|
596
|
+
|
|
597
|
+
<details>
|
|
598
|
+
<summary>コード例: ProductController.java</summary>
|
|
599
|
+
|
|
600
|
+
```java
|
|
601
|
+
@Controller
|
|
602
|
+
@RequestMapping("/products")
|
|
603
|
+
public class ProductController {
|
|
604
|
+
|
|
605
|
+
private final ProductUseCase productUseCase;
|
|
606
|
+
|
|
607
|
+
public ProductController(ProductUseCase productUseCase) {
|
|
608
|
+
this.productUseCase = productUseCase;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
@GetMapping
|
|
612
|
+
public String list(@RequestParam(required = false) ProductCategory category, Model model) {
|
|
613
|
+
List<Product> products = category != null
|
|
614
|
+
? productUseCase.findByCategory(category)
|
|
615
|
+
: productUseCase.findAll();
|
|
616
|
+
model.addAttribute("products", products);
|
|
617
|
+
model.addAttribute("categories", ProductCategory.values());
|
|
618
|
+
return "products/list";
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
@GetMapping("/new")
|
|
622
|
+
public String newForm(Model model) {
|
|
623
|
+
model.addAttribute("form", new ProductForm());
|
|
624
|
+
model.addAttribute("categories", ProductCategory.values());
|
|
625
|
+
return "products/new";
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
@PostMapping
|
|
629
|
+
public String create(
|
|
630
|
+
@Valid @ModelAttribute("form") ProductForm form,
|
|
631
|
+
BindingResult bindingResult,
|
|
632
|
+
RedirectAttributes redirectAttributes,
|
|
633
|
+
Model model) {
|
|
634
|
+
|
|
635
|
+
if (bindingResult.hasErrors()) {
|
|
636
|
+
model.addAttribute("categories", ProductCategory.values());
|
|
637
|
+
return "products/new";
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
productUseCase.createProduct(form.toCommand());
|
|
641
|
+
redirectAttributes.addFlashAttribute("successMessage", "商品を登録しました");
|
|
642
|
+
return "redirect:/products";
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
@GetMapping("/{productCode}/edit")
|
|
646
|
+
public String editForm(@PathVariable String productCode, Model model) {
|
|
647
|
+
Product product = productUseCase.findByCode(productCode)
|
|
648
|
+
.orElseThrow(() -> new ProductNotFoundException(productCode));
|
|
649
|
+
model.addAttribute("form", ProductForm.from(product));
|
|
650
|
+
model.addAttribute("categories", ProductCategory.values());
|
|
651
|
+
return "products/edit";
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
@PostMapping("/{productCode}")
|
|
655
|
+
public String update(
|
|
656
|
+
@PathVariable String productCode,
|
|
657
|
+
@Valid @ModelAttribute("form") ProductForm form,
|
|
658
|
+
BindingResult bindingResult,
|
|
659
|
+
RedirectAttributes redirectAttributes,
|
|
660
|
+
Model model) {
|
|
661
|
+
|
|
662
|
+
if (bindingResult.hasErrors()) {
|
|
663
|
+
model.addAttribute("categories", ProductCategory.values());
|
|
664
|
+
return "products/edit";
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
productUseCase.updateProduct(productCode, form.toCommand());
|
|
668
|
+
redirectAttributes.addFlashAttribute("successMessage", "商品を更新しました");
|
|
669
|
+
return "redirect:/products/" + productCode;
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
@PostMapping("/{productCode}/delete")
|
|
673
|
+
public String delete(@PathVariable String productCode, RedirectAttributes redirectAttributes) {
|
|
674
|
+
productUseCase.deleteProduct(productCode);
|
|
675
|
+
redirectAttributes.addFlashAttribute("successMessage", "商品を削除しました");
|
|
676
|
+
return "redirect:/products";
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
```
|
|
680
|
+
|
|
681
|
+
</details>
|
|
682
|
+
|
|
683
|
+
---
|
|
684
|
+
|
|
685
|
+
## 第16章:トランザクションの実装
|
|
686
|
+
|
|
687
|
+
### 16.1 受注業務画面の実装
|
|
688
|
+
|
|
689
|
+
#### 受注 Controller
|
|
690
|
+
|
|
691
|
+
<details>
|
|
692
|
+
<summary>コード例: OrderController.java</summary>
|
|
693
|
+
|
|
694
|
+
```java
|
|
695
|
+
@Controller
|
|
696
|
+
@RequestMapping("/orders")
|
|
697
|
+
public class OrderController {
|
|
698
|
+
|
|
699
|
+
private final OrderUseCase orderUseCase;
|
|
700
|
+
private final ProductUseCase productUseCase;
|
|
701
|
+
private final PartnerUseCase partnerUseCase;
|
|
702
|
+
|
|
703
|
+
@GetMapping
|
|
704
|
+
public String list(
|
|
705
|
+
@RequestParam(required = false) String status,
|
|
706
|
+
@RequestParam(required = false) String customerCode,
|
|
707
|
+
Model model) {
|
|
708
|
+
|
|
709
|
+
List<Order> orders = orderUseCase.getOrders(status, customerCode);
|
|
710
|
+
model.addAttribute("orders", orders);
|
|
711
|
+
model.addAttribute("customers", partnerUseCase.getCustomers());
|
|
712
|
+
return "orders/list";
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
@GetMapping("/new")
|
|
716
|
+
public String newForm(Model model) {
|
|
717
|
+
model.addAttribute("form", new OrderForm());
|
|
718
|
+
model.addAttribute("customers", partnerUseCase.getCustomers());
|
|
719
|
+
model.addAttribute("products", productUseCase.getAllProducts());
|
|
720
|
+
return "orders/new";
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
@PostMapping
|
|
724
|
+
public String create(
|
|
725
|
+
@Valid @ModelAttribute("form") OrderForm form,
|
|
726
|
+
BindingResult bindingResult,
|
|
727
|
+
Model model,
|
|
728
|
+
RedirectAttributes redirectAttributes) {
|
|
729
|
+
|
|
730
|
+
if (bindingResult.hasErrors()) {
|
|
731
|
+
model.addAttribute("customers", partnerUseCase.getCustomers());
|
|
732
|
+
model.addAttribute("products", productUseCase.getAllProducts());
|
|
733
|
+
return "orders/new";
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
Order order = orderUseCase.createOrder(form.toCommand());
|
|
737
|
+
redirectAttributes.addFlashAttribute("successMessage",
|
|
738
|
+
"受注「" + order.getOrderNumber() + "」を登録しました");
|
|
739
|
+
return "redirect:/orders";
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
@PostMapping("/{orderNumber}/confirm")
|
|
743
|
+
public String confirm(@PathVariable String orderNumber, RedirectAttributes redirectAttributes) {
|
|
744
|
+
orderUseCase.confirmOrder(orderNumber);
|
|
745
|
+
redirectAttributes.addFlashAttribute("successMessage",
|
|
746
|
+
"受注「" + orderNumber + "」を確定しました");
|
|
747
|
+
return "redirect:/orders/" + orderNumber;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
@PostMapping("/{orderNumber}/cancel")
|
|
751
|
+
public String cancel(@PathVariable String orderNumber, RedirectAttributes redirectAttributes) {
|
|
752
|
+
orderUseCase.cancelOrder(orderNumber);
|
|
753
|
+
redirectAttributes.addFlashAttribute("successMessage",
|
|
754
|
+
"受注「" + orderNumber + "」を取り消しました");
|
|
755
|
+
return "redirect:/orders";
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
```
|
|
759
|
+
|
|
760
|
+
</details>
|
|
761
|
+
|
|
762
|
+
### 16.2 動的フォーム(明細行の追加・削除)
|
|
763
|
+
|
|
764
|
+
htmx を使用して、ページをリロードせずに明細行を追加・削除できます。
|
|
765
|
+
|
|
766
|
+
<details>
|
|
767
|
+
<summary>コード例: 動的フォーム(明細行)</summary>
|
|
768
|
+
|
|
769
|
+
```html
|
|
770
|
+
<!-- 明細追加ボタン -->
|
|
771
|
+
<button type="button" class="btn btn-sm btn-outline-primary"
|
|
772
|
+
hx-get="/orders/add-detail-row"
|
|
773
|
+
hx-target="#detail-rows"
|
|
774
|
+
hx-swap="beforeend"
|
|
775
|
+
hx-vals='js:{"index": document.querySelectorAll("#detail-rows tr").length}'>
|
|
776
|
+
<i class="bi bi-plus"></i> 行追加
|
|
777
|
+
</button>
|
|
778
|
+
|
|
779
|
+
<!-- 明細行フラグメント -->
|
|
780
|
+
<tr th:fragment="detailRow" th:id="'row-' + ${index}">
|
|
781
|
+
<td>
|
|
782
|
+
<select class="form-select product-select"
|
|
783
|
+
th:name="'details[' + ${index} + '].productCode'" required
|
|
784
|
+
onchange="updatePrice(this)">
|
|
785
|
+
<option value="">選択</option>
|
|
786
|
+
<option th:each="product : ${products}"
|
|
787
|
+
th:value="${product.productCode}"
|
|
788
|
+
th:text="${product.productCode + ' - ' + product.productName}"
|
|
789
|
+
th:data-price="${product.unitPrice}"></option>
|
|
790
|
+
</select>
|
|
791
|
+
</td>
|
|
792
|
+
<td>
|
|
793
|
+
<input type="number" class="form-control quantity-input"
|
|
794
|
+
th:name="'details[' + ${index} + '].quantity'" min="1" value="1" required
|
|
795
|
+
onchange="calculateAmount(this)">
|
|
796
|
+
</td>
|
|
797
|
+
<td>
|
|
798
|
+
<input type="number" class="form-control price-input"
|
|
799
|
+
th:name="'details[' + ${index} + '].unitPrice'" step="1" min="0"
|
|
800
|
+
onchange="calculateAmount(this)">
|
|
801
|
+
</td>
|
|
802
|
+
<td>
|
|
803
|
+
<input type="text" class="form-control amount-display" readonly value="0">
|
|
804
|
+
</td>
|
|
805
|
+
<td>
|
|
806
|
+
<button type="button" class="btn btn-sm btn-outline-danger"
|
|
807
|
+
onclick="this.closest('tr').remove(); calculateTotal()">削除</button>
|
|
808
|
+
</td>
|
|
809
|
+
</tr>
|
|
810
|
+
```
|
|
811
|
+
|
|
812
|
+
</details>
|
|
813
|
+
|
|
814
|
+
### 16.3 出荷業務画面の実装
|
|
815
|
+
|
|
816
|
+
<details>
|
|
817
|
+
<summary>コード例: ShipmentController.java</summary>
|
|
818
|
+
|
|
819
|
+
```java
|
|
820
|
+
@Controller
|
|
821
|
+
@RequestMapping("/shipments")
|
|
822
|
+
public class ShipmentController {
|
|
823
|
+
|
|
824
|
+
private final ShipmentUseCase shipmentUseCase;
|
|
825
|
+
private final OrderUseCase orderUseCase;
|
|
826
|
+
|
|
827
|
+
@GetMapping
|
|
828
|
+
public String list(
|
|
829
|
+
@RequestParam(required = false) String status,
|
|
830
|
+
@RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate shipmentDate,
|
|
831
|
+
Model model) {
|
|
832
|
+
|
|
833
|
+
List<Shipment> shipments = shipmentDate != null
|
|
834
|
+
? shipmentUseCase.getShipmentsByDate(shipmentDate)
|
|
835
|
+
: shipmentUseCase.getShipments(status);
|
|
836
|
+
|
|
837
|
+
model.addAttribute("shipments", shipments);
|
|
838
|
+
model.addAttribute("today", LocalDate.now());
|
|
839
|
+
return "shipments/list";
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
@PostMapping("/{shipmentNumber}/execute")
|
|
843
|
+
public String execute(@PathVariable String shipmentNumber, RedirectAttributes redirectAttributes) {
|
|
844
|
+
try {
|
|
845
|
+
shipmentUseCase.executeShipment(shipmentNumber);
|
|
846
|
+
redirectAttributes.addFlashAttribute("successMessage",
|
|
847
|
+
"出荷「" + shipmentNumber + "」を実行しました");
|
|
848
|
+
} catch (InsufficientInventoryException e) {
|
|
849
|
+
redirectAttributes.addFlashAttribute("errorMessage",
|
|
850
|
+
"在庫不足のため出荷できません: " + e.getMessage());
|
|
851
|
+
}
|
|
852
|
+
return "redirect:/shipments/" + shipmentNumber;
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
```
|
|
856
|
+
|
|
857
|
+
</details>
|
|
858
|
+
|
|
859
|
+
---
|
|
860
|
+
|
|
861
|
+
## 第17章:エラーハンドリングと帳票出力
|
|
862
|
+
|
|
863
|
+
### 17.1 グローバル例外ハンドラ
|
|
864
|
+
|
|
865
|
+
<details>
|
|
866
|
+
<summary>コード例: GlobalExceptionHandler.java</summary>
|
|
867
|
+
|
|
868
|
+
```java
|
|
869
|
+
@ControllerAdvice
|
|
870
|
+
public class GlobalExceptionHandler {
|
|
871
|
+
|
|
872
|
+
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
|
873
|
+
|
|
874
|
+
/**
|
|
875
|
+
* 業務エラー(リソース未検出)
|
|
876
|
+
*/
|
|
877
|
+
@ExceptionHandler(ResourceNotFoundException.class)
|
|
878
|
+
public String handleResourceNotFound(ResourceNotFoundException ex, Model model) {
|
|
879
|
+
logger.warn("リソース未検出: {}", ex.getMessage());
|
|
880
|
+
model.addAttribute("errorMessage", ex.getMessage());
|
|
881
|
+
return "error/404";
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
/**
|
|
885
|
+
* 業務エラー(バリデーション)
|
|
886
|
+
*/
|
|
887
|
+
@ExceptionHandler(BusinessValidationException.class)
|
|
888
|
+
public String handleBusinessValidation(BusinessValidationException ex, Model model) {
|
|
889
|
+
logger.warn("業務バリデーションエラー: {}", ex.getMessage());
|
|
890
|
+
model.addAttribute("errorMessage", ex.getMessage());
|
|
891
|
+
model.addAttribute("errorCode", ex.getErrorCode());
|
|
892
|
+
return "error/business";
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
/**
|
|
896
|
+
* システムエラー
|
|
897
|
+
*/
|
|
898
|
+
@ExceptionHandler(Exception.class)
|
|
899
|
+
public String handleGeneralException(Exception ex, Model model) {
|
|
900
|
+
logger.error("システムエラー", ex);
|
|
901
|
+
model.addAttribute("errorMessage", "システムエラーが発生しました。管理者にお問い合わせください。");
|
|
902
|
+
return "error/500";
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
```
|
|
906
|
+
|
|
907
|
+
</details>
|
|
908
|
+
|
|
909
|
+
### 17.2 Excel 帳票出力
|
|
910
|
+
|
|
911
|
+
<details>
|
|
912
|
+
<summary>コード例: ReportController.java(Excel 出力)</summary>
|
|
913
|
+
|
|
914
|
+
```java
|
|
915
|
+
@Controller
|
|
916
|
+
@RequestMapping("/reports")
|
|
917
|
+
public class ReportController {
|
|
918
|
+
|
|
919
|
+
private final ReportUseCase reportUseCase;
|
|
920
|
+
|
|
921
|
+
@GetMapping("/inventory/excel")
|
|
922
|
+
public void exportInventoryExcel(HttpServletResponse response) throws IOException {
|
|
923
|
+
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
924
|
+
response.setHeader("Content-Disposition",
|
|
925
|
+
"attachment; filename=inventory_" + LocalDate.now() + ".xlsx");
|
|
926
|
+
|
|
927
|
+
try (Workbook workbook = new XSSFWorkbook()) {
|
|
928
|
+
Sheet sheet = workbook.createSheet("在庫一覧");
|
|
929
|
+
|
|
930
|
+
// ヘッダー行
|
|
931
|
+
Row headerRow = sheet.createRow(0);
|
|
932
|
+
CellStyle headerStyle = createHeaderStyle(workbook);
|
|
933
|
+
String[] headers = {"商品コード", "商品名", "現在在庫", "安全在庫", "過不足"};
|
|
934
|
+
for (int i = 0; i < headers.length; i++) {
|
|
935
|
+
Cell cell = headerRow.createCell(i);
|
|
936
|
+
cell.setCellValue(headers[i]);
|
|
937
|
+
cell.setCellStyle(headerStyle);
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
// データ行
|
|
941
|
+
List<InventoryData> inventoryList = reportUseCase.getInventoryReport();
|
|
942
|
+
int rowNum = 1;
|
|
943
|
+
for (InventoryData data : inventoryList) {
|
|
944
|
+
Row row = sheet.createRow(rowNum++);
|
|
945
|
+
row.createCell(0).setCellValue(data.getProductCode());
|
|
946
|
+
row.createCell(1).setCellValue(data.getProductName());
|
|
947
|
+
row.createCell(2).setCellValue(data.getCurrentStock());
|
|
948
|
+
row.createCell(3).setCellValue(data.getSafetyStock());
|
|
949
|
+
row.createCell(4).setCellValue(data.getDifference());
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
// 列幅自動調整
|
|
953
|
+
for (int i = 0; i < headers.length; i++) {
|
|
954
|
+
sheet.autoSizeColumn(i);
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
workbook.write(response.getOutputStream());
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
```
|
|
962
|
+
|
|
963
|
+
</details>
|
|
964
|
+
|
|
965
|
+
### 17.3 PDF 帳票出力(Thymeleaf + OpenHTMLtoPDF)
|
|
966
|
+
|
|
967
|
+
PDF 帳票は **Thymeleaf テンプレート + OpenHTMLtoPDF** を使用して実装します。この方式により、HTML/CSS で帳票のレイアウトを定義でき、デザインの自由度が高く保守性も向上します。
|
|
968
|
+
|
|
969
|
+
```plantuml
|
|
970
|
+
@startuml pdf_generation_flow
|
|
971
|
+
title PDF 生成フロー
|
|
972
|
+
|
|
973
|
+
participant "Controller" as C
|
|
974
|
+
participant "PdfGeneratorService" as PDF
|
|
975
|
+
participant "TemplateEngine" as T
|
|
976
|
+
participant "PdfRendererBuilder" as R
|
|
977
|
+
|
|
978
|
+
C -> PDF : generatePdf(templateName, variables)
|
|
979
|
+
PDF -> T : process(templateName, context)
|
|
980
|
+
T --> PDF : HTML 文字列
|
|
981
|
+
PDF -> R : withHtmlContent(html, null)
|
|
982
|
+
R -> R : run()
|
|
983
|
+
R --> PDF : PDF バイナリ
|
|
984
|
+
PDF --> C : byte[]
|
|
985
|
+
|
|
986
|
+
@enduml
|
|
987
|
+
```
|
|
988
|
+
|
|
989
|
+
#### PDF 生成サービス
|
|
990
|
+
|
|
991
|
+
<details>
|
|
992
|
+
<summary>コード例: PdfGeneratorService.java</summary>
|
|
993
|
+
|
|
994
|
+
```java
|
|
995
|
+
package com.example.sms.infrastructure.in.web.service;
|
|
996
|
+
|
|
997
|
+
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
|
|
998
|
+
import org.springframework.core.io.ClassPathResource;
|
|
999
|
+
import org.springframework.stereotype.Service;
|
|
1000
|
+
import org.thymeleaf.TemplateEngine;
|
|
1001
|
+
import org.thymeleaf.context.Context;
|
|
1002
|
+
|
|
1003
|
+
import java.io.ByteArrayOutputStream;
|
|
1004
|
+
import java.io.File;
|
|
1005
|
+
import java.io.IOException;
|
|
1006
|
+
import java.util.Map;
|
|
1007
|
+
|
|
1008
|
+
/**
|
|
1009
|
+
* PDF生成サービス.
|
|
1010
|
+
* ThymeleafテンプレートからPDFを生成する.
|
|
1011
|
+
*/
|
|
1012
|
+
@Service
|
|
1013
|
+
public class PdfGeneratorService {
|
|
1014
|
+
|
|
1015
|
+
private static final String FONT_FAMILY = "Japanese";
|
|
1016
|
+
|
|
1017
|
+
/** Windowsのシステムフォントパス. */
|
|
1018
|
+
private static final String[] WINDOWS_FONTS = {
|
|
1019
|
+
"C:/Windows/Fonts/YuGothM.ttc", // Yu Gothic Medium
|
|
1020
|
+
"C:/Windows/Fonts/YuGothR.ttc", // Yu Gothic Regular
|
|
1021
|
+
"C:/Windows/Fonts/msgothic.ttc", // MS Gothic
|
|
1022
|
+
"C:/Windows/Fonts/meiryo.ttc" // Meiryo
|
|
1023
|
+
};
|
|
1024
|
+
|
|
1025
|
+
private final TemplateEngine templateEngine;
|
|
1026
|
+
private final File japaneseFontFile;
|
|
1027
|
+
|
|
1028
|
+
public PdfGeneratorService(TemplateEngine templateEngine) {
|
|
1029
|
+
this.templateEngine = templateEngine;
|
|
1030
|
+
this.japaneseFontFile = findJapaneseFont();
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
/**
|
|
1034
|
+
* ThymeleafテンプレートからPDFを生成する.
|
|
1035
|
+
*/
|
|
1036
|
+
public byte[] generatePdf(String templateName, Map<String, Object> variables) {
|
|
1037
|
+
Context context = new Context();
|
|
1038
|
+
context.setVariables(variables);
|
|
1039
|
+
String html = templateEngine.process(templateName, context);
|
|
1040
|
+
|
|
1041
|
+
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
|
1042
|
+
PdfRendererBuilder builder = new PdfRendererBuilder()
|
|
1043
|
+
.useFastMode();
|
|
1044
|
+
|
|
1045
|
+
// 日本語フォントを登録
|
|
1046
|
+
if (japaneseFontFile != null) {
|
|
1047
|
+
builder = builder.useFont(japaneseFontFile, FONT_FAMILY);
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
builder.withHtmlContent(html, null)
|
|
1051
|
+
.toStream(outputStream)
|
|
1052
|
+
.run();
|
|
1053
|
+
|
|
1054
|
+
return outputStream.toByteArray();
|
|
1055
|
+
} catch (IOException e) {
|
|
1056
|
+
throw new PdfGenerationException("PDF生成に失敗しました", e);
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
/** 日本語フォントを検索する. */
|
|
1061
|
+
private File findJapaneseFont() {
|
|
1062
|
+
// クラスパスからフォントを検索
|
|
1063
|
+
ClassPathResource fontResource = new ClassPathResource("fonts/NotoSansJP-Regular.ttf");
|
|
1064
|
+
if (fontResource.exists()) {
|
|
1065
|
+
try {
|
|
1066
|
+
return fontResource.getFile();
|
|
1067
|
+
} catch (IOException e) {
|
|
1068
|
+
// クラスパスからの読み込み失敗時はシステムフォントを試す
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
// Windowsのシステムフォントを検索
|
|
1073
|
+
for (String fontPath : WINDOWS_FONTS) {
|
|
1074
|
+
File fontFile = new File(fontPath);
|
|
1075
|
+
if (fontFile.exists()) {
|
|
1076
|
+
return fontFile;
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
return null;
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
```
|
|
1083
|
+
|
|
1084
|
+
</details>
|
|
1085
|
+
|
|
1086
|
+
**日本語フォント対応のポイント:**
|
|
1087
|
+
|
|
1088
|
+
| ポイント | 説明 |
|
|
1089
|
+
|---------|------|
|
|
1090
|
+
| **フォント登録** | `builder.useFont(fontFile, fontFamily)` でフォントを登録 |
|
|
1091
|
+
| **CSS でフォント指定** | `font-family: Japanese, sans-serif;` で登録したフォントを参照 |
|
|
1092
|
+
| **システムフォント** | Windows は Yu Gothic、macOS はヒラギノなどを自動検出 |
|
|
1093
|
+
| **カスタムフォント** | `resources/fonts/` に TTF ファイルを配置して使用可能 |
|
|
1094
|
+
|
|
1095
|
+
#### PDF 用 HTML テンプレート
|
|
1096
|
+
|
|
1097
|
+
<details>
|
|
1098
|
+
<summary>コード例: reports/invoice-pdf.html</summary>
|
|
1099
|
+
|
|
1100
|
+
```html
|
|
1101
|
+
<!DOCTYPE html>
|
|
1102
|
+
<html xmlns:th="http://www.thymeleaf.org" lang="ja">
|
|
1103
|
+
<head>
|
|
1104
|
+
<meta charset="UTF-8"/>
|
|
1105
|
+
<title>請求書</title>
|
|
1106
|
+
<style>
|
|
1107
|
+
@page {
|
|
1108
|
+
size: A4;
|
|
1109
|
+
margin: 20mm;
|
|
1110
|
+
}
|
|
1111
|
+
body {
|
|
1112
|
+
/* "Japanese" は PdfGeneratorService で登録したフォントファミリー名 */
|
|
1113
|
+
font-family: Japanese, "Noto Sans JP", "Hiragino Sans", sans-serif;
|
|
1114
|
+
font-size: 10pt;
|
|
1115
|
+
line-height: 1.5;
|
|
1116
|
+
}
|
|
1117
|
+
.header {
|
|
1118
|
+
text-align: center;
|
|
1119
|
+
margin-bottom: 30px;
|
|
1120
|
+
}
|
|
1121
|
+
.header h1 {
|
|
1122
|
+
font-size: 24pt;
|
|
1123
|
+
margin: 0;
|
|
1124
|
+
}
|
|
1125
|
+
.amount-box {
|
|
1126
|
+
border: 2px solid #333;
|
|
1127
|
+
padding: 15px;
|
|
1128
|
+
margin-bottom: 30px;
|
|
1129
|
+
text-align: center;
|
|
1130
|
+
}
|
|
1131
|
+
.amount-box .value {
|
|
1132
|
+
font-size: 20pt;
|
|
1133
|
+
font-weight: bold;
|
|
1134
|
+
}
|
|
1135
|
+
table {
|
|
1136
|
+
width: 100%;
|
|
1137
|
+
border-collapse: collapse;
|
|
1138
|
+
margin-bottom: 20px;
|
|
1139
|
+
}
|
|
1140
|
+
th, td {
|
|
1141
|
+
border: 1px solid #333;
|
|
1142
|
+
padding: 8px;
|
|
1143
|
+
}
|
|
1144
|
+
th {
|
|
1145
|
+
background-color: #f0f0f0;
|
|
1146
|
+
text-align: center;
|
|
1147
|
+
}
|
|
1148
|
+
td.number {
|
|
1149
|
+
text-align: right;
|
|
1150
|
+
}
|
|
1151
|
+
</style>
|
|
1152
|
+
</head>
|
|
1153
|
+
<body>
|
|
1154
|
+
<div class="header">
|
|
1155
|
+
<h1>請求書</h1>
|
|
1156
|
+
</div>
|
|
1157
|
+
|
|
1158
|
+
<div class="meta">
|
|
1159
|
+
<p>請求番号: <span th:text="${invoice.invoiceNumber}"></span></p>
|
|
1160
|
+
<p>請求日: <span th:text="${#temporals.format(invoice.invoiceDate, 'yyyy年MM月dd日')}"></span></p>
|
|
1161
|
+
</div>
|
|
1162
|
+
|
|
1163
|
+
<div class="customer">
|
|
1164
|
+
<p><span th:text="${customerName}"></span> 御中</p>
|
|
1165
|
+
</div>
|
|
1166
|
+
|
|
1167
|
+
<div class="amount-box">
|
|
1168
|
+
<p class="label">ご請求金額</p>
|
|
1169
|
+
<p class="value">¥<span th:text="${#numbers.formatDecimal(invoice.currentInvoiceAmount, 1, 'COMMA', 0, 'POINT')}"></span>-</p>
|
|
1170
|
+
</div>
|
|
1171
|
+
|
|
1172
|
+
<!-- 請求明細テーブル -->
|
|
1173
|
+
<table th:if="${invoice.details != null and !invoice.details.isEmpty()}">
|
|
1174
|
+
<thead>
|
|
1175
|
+
<tr>
|
|
1176
|
+
<th>売上番号</th>
|
|
1177
|
+
<th>売上日</th>
|
|
1178
|
+
<th>売上金額</th>
|
|
1179
|
+
<th>消費税</th>
|
|
1180
|
+
<th>合計</th>
|
|
1181
|
+
</tr>
|
|
1182
|
+
</thead>
|
|
1183
|
+
<tbody>
|
|
1184
|
+
<tr th:each="detail : ${invoice.details}">
|
|
1185
|
+
<td th:text="${detail.salesNumber}"></td>
|
|
1186
|
+
<td th:text="${#temporals.format(detail.salesDate, 'yyyy/MM/dd')}"></td>
|
|
1187
|
+
<td class="number" th:text="${#numbers.formatDecimal(detail.salesAmount, 1, 'COMMA', 0, 'POINT')}"></td>
|
|
1188
|
+
<td class="number" th:text="${#numbers.formatDecimal(detail.taxAmount, 1, 'COMMA', 0, 'POINT')}"></td>
|
|
1189
|
+
<td class="number" th:text="${#numbers.formatDecimal(detail.totalAmount, 1, 'COMMA', 0, 'POINT')}"></td>
|
|
1190
|
+
</tr>
|
|
1191
|
+
</tbody>
|
|
1192
|
+
</table>
|
|
1193
|
+
</body>
|
|
1194
|
+
</html>
|
|
1195
|
+
```
|
|
1196
|
+
|
|
1197
|
+
</details>
|
|
1198
|
+
|
|
1199
|
+
#### Controller での PDF 出力
|
|
1200
|
+
|
|
1201
|
+
<details>
|
|
1202
|
+
<summary>コード例: ReportWebController.java(PDF 出力)</summary>
|
|
1203
|
+
|
|
1204
|
+
```java
|
|
1205
|
+
@Controller
|
|
1206
|
+
@RequestMapping("/reports")
|
|
1207
|
+
public class ReportWebController {
|
|
1208
|
+
|
|
1209
|
+
private final ReportService reportService;
|
|
1210
|
+
private final PdfGeneratorService pdfGeneratorService;
|
|
1211
|
+
|
|
1212
|
+
/**
|
|
1213
|
+
* 請求書を PDF 形式でエクスポート.
|
|
1214
|
+
*/
|
|
1215
|
+
@GetMapping("/invoice/{invoiceNumber}/pdf")
|
|
1216
|
+
public void exportInvoicePdf(@PathVariable String invoiceNumber, HttpServletResponse response)
|
|
1217
|
+
throws IOException {
|
|
1218
|
+
|
|
1219
|
+
Invoice invoice = reportService.getInvoiceForReport(invoiceNumber);
|
|
1220
|
+
String customerName = reportService.getCustomerName(invoice.getCustomerCode());
|
|
1221
|
+
|
|
1222
|
+
// テンプレート変数を設定
|
|
1223
|
+
Map<String, Object> variables = Map.of(
|
|
1224
|
+
"invoice", invoice,
|
|
1225
|
+
"customerName", customerName
|
|
1226
|
+
);
|
|
1227
|
+
|
|
1228
|
+
// PDFを生成
|
|
1229
|
+
byte[] pdfBytes = pdfGeneratorService.generatePdf("reports/invoice-pdf", variables);
|
|
1230
|
+
|
|
1231
|
+
// レスポンスを設定
|
|
1232
|
+
response.setContentType("application/pdf");
|
|
1233
|
+
response.setHeader("Content-Disposition",
|
|
1234
|
+
"attachment; filename=invoice_" + invoiceNumber + ".pdf");
|
|
1235
|
+
response.setContentLength(pdfBytes.length);
|
|
1236
|
+
response.getOutputStream().write(pdfBytes);
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
```
|
|
1240
|
+
|
|
1241
|
+
</details>
|
|
1242
|
+
|
|
1243
|
+
#### Thymeleaf + OpenHTMLtoPDF のメリット
|
|
1244
|
+
|
|
1245
|
+
| 観点 | 説明 |
|
|
1246
|
+
|------|------|
|
|
1247
|
+
| **デザインの自由度** | HTML/CSS でレイアウトを定義できるため、複雑なデザインも容易 |
|
|
1248
|
+
| **保守性** | HTML テンプレートなので、プログラマー以外でも修正しやすい |
|
|
1249
|
+
| **再利用** | 同じテンプレートを Web 表示とPDF 出力で共有可能 |
|
|
1250
|
+
| **プレビュー** | ブラウザで HTML として確認後、PDF 出力できる |
|
|
1251
|
+
| **日本語対応** | CSS で日本語フォントを指定するだけで対応可能 |
|
|
1252
|
+
|
|
1253
|
+
---
|
|
1254
|
+
|
|
1255
|
+
## 第18章:ページネーション
|
|
1256
|
+
|
|
1257
|
+
### 18.1 ページネーションの概要
|
|
1258
|
+
|
|
1259
|
+
大量のデータを扱う一覧画面では、パフォーマンスとユーザビリティの観点から**ページネーション**(ページ分割)が必要です。本章では、商品マスタ画面を例に、ヘキサゴナルアーキテクチャに沿ったページネーション機能の実装方法を解説します。
|
|
1260
|
+
|
|
1261
|
+
```plantuml
|
|
1262
|
+
@startuml pagination_flow
|
|
1263
|
+
title ページネーション処理フロー
|
|
1264
|
+
|
|
1265
|
+
actor "ブラウザ" as Browser
|
|
1266
|
+
participant "ProductWebController" as Controller
|
|
1267
|
+
participant "ProductService" as Service
|
|
1268
|
+
participant "ProductRepositoryImpl" as Repository
|
|
1269
|
+
participant "ProductMapper" as Mapper
|
|
1270
|
+
database "PostgreSQL" as DB
|
|
1271
|
+
|
|
1272
|
+
Browser -> Controller : GET /products?page=0&size=10
|
|
1273
|
+
Controller -> Service : getProducts(page, size, category, keyword)
|
|
1274
|
+
Service -> Repository : findWithPagination(page, size, category, keyword)
|
|
1275
|
+
Repository -> Mapper : findWithPagination(offset, limit, category, keyword)
|
|
1276
|
+
Mapper -> DB : SELECT ... LIMIT #{limit} OFFSET #{offset}
|
|
1277
|
+
DB --> Mapper : 商品データ(10件)
|
|
1278
|
+
Repository -> Mapper : count(category, keyword)
|
|
1279
|
+
Mapper -> DB : SELECT COUNT(*) ...
|
|
1280
|
+
DB --> Mapper : 総件数
|
|
1281
|
+
Repository --> Service : PageResult<Product>
|
|
1282
|
+
Service --> Controller : PageResult<Product>
|
|
1283
|
+
Controller -> Browser : products/list.html\n(ページネーション UI 付き)
|
|
1284
|
+
|
|
1285
|
+
@enduml
|
|
1286
|
+
```
|
|
1287
|
+
|
|
1288
|
+
### 18.2 ページネーション結果クラス
|
|
1289
|
+
|
|
1290
|
+
ページネーション結果を表す汎用クラスを定義します。
|
|
1291
|
+
|
|
1292
|
+
<details>
|
|
1293
|
+
<summary>コード例: PageResult.java</summary>
|
|
1294
|
+
|
|
1295
|
+
```java
|
|
1296
|
+
package com.example.sms.domain.model.common;
|
|
1297
|
+
|
|
1298
|
+
import java.util.List;
|
|
1299
|
+
|
|
1300
|
+
/**
|
|
1301
|
+
* ページネーション結果を表すクラス.
|
|
1302
|
+
*
|
|
1303
|
+
* @param <T> 要素の型
|
|
1304
|
+
*/
|
|
1305
|
+
public class PageResult<T> {
|
|
1306
|
+
|
|
1307
|
+
private final List<T> content;
|
|
1308
|
+
private final int pageNumber;
|
|
1309
|
+
private final int pageSize;
|
|
1310
|
+
private final long totalElements;
|
|
1311
|
+
private final int totalPages;
|
|
1312
|
+
|
|
1313
|
+
public PageResult(List<T> content, int pageNumber, int pageSize, long totalElements) {
|
|
1314
|
+
this.content = content;
|
|
1315
|
+
this.pageNumber = pageNumber;
|
|
1316
|
+
this.pageSize = pageSize;
|
|
1317
|
+
this.totalElements = totalElements;
|
|
1318
|
+
this.totalPages = pageSize > 0 ? (int) Math.ceil((double) totalElements / pageSize) : 0;
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
public List<T> getContent() { return content; }
|
|
1322
|
+
public int getPage() { return pageNumber; }
|
|
1323
|
+
public int getSize() { return pageSize; }
|
|
1324
|
+
public long getTotalElements() { return totalElements; }
|
|
1325
|
+
public int getTotalPages() { return totalPages; }
|
|
1326
|
+
public boolean hasNext() { return pageNumber < totalPages - 1; }
|
|
1327
|
+
public boolean hasPrevious() { return pageNumber > 0; }
|
|
1328
|
+
public boolean isFirst() { return pageNumber == 0; }
|
|
1329
|
+
public boolean isLast() { return pageNumber >= totalPages - 1; }
|
|
1330
|
+
public int getNumber() { return pageNumber + 1; }
|
|
1331
|
+
|
|
1332
|
+
public static <T> PageResult<T> empty() {
|
|
1333
|
+
return new PageResult<>(List.of(), 0, 10, 0);
|
|
1334
|
+
}
|
|
1335
|
+
}
|
|
1336
|
+
```
|
|
1337
|
+
|
|
1338
|
+
</details>
|
|
1339
|
+
|
|
1340
|
+
### 18.3 MyBatis マッパーの実装
|
|
1341
|
+
|
|
1342
|
+
ページネーション用の SQL クエリを追加します。`LIMIT` と `OFFSET` を使用してデータを取得し、`COUNT` で総件数を取得します。
|
|
1343
|
+
|
|
1344
|
+
<details>
|
|
1345
|
+
<summary>コード例: ProductMapper.xml(抜粋)</summary>
|
|
1346
|
+
|
|
1347
|
+
```xml
|
|
1348
|
+
<select id="findWithPagination" resultMap="ProductResultMap">
|
|
1349
|
+
SELECT * FROM "商品マスタ"
|
|
1350
|
+
<where>
|
|
1351
|
+
<if test="category != null and category != ''">
|
|
1352
|
+
"商品区分" = #{category}::商品区分
|
|
1353
|
+
</if>
|
|
1354
|
+
<if test="keyword != null and keyword != ''">
|
|
1355
|
+
AND (LOWER("商品コード") LIKE LOWER('%' || #{keyword} || '%')
|
|
1356
|
+
OR LOWER("商品名") LIKE LOWER('%' || #{keyword} || '%'))
|
|
1357
|
+
</if>
|
|
1358
|
+
</where>
|
|
1359
|
+
ORDER BY "商品コード"
|
|
1360
|
+
LIMIT #{limit} OFFSET #{offset}
|
|
1361
|
+
</select>
|
|
1362
|
+
|
|
1363
|
+
<select id="count" resultType="long">
|
|
1364
|
+
SELECT COUNT(*) FROM "商品マスタ"
|
|
1365
|
+
<where>
|
|
1366
|
+
<if test="category != null and category != ''">
|
|
1367
|
+
"商品区分" = #{category}::商品区分
|
|
1368
|
+
</if>
|
|
1369
|
+
<if test="keyword != null and keyword != ''">
|
|
1370
|
+
AND (LOWER("商品コード") LIKE LOWER('%' || #{keyword} || '%')
|
|
1371
|
+
OR LOWER("商品名") LIKE LOWER('%' || #{keyword} || '%'))
|
|
1372
|
+
</if>
|
|
1373
|
+
</where>
|
|
1374
|
+
</select>
|
|
1375
|
+
```
|
|
1376
|
+
|
|
1377
|
+
</details>
|
|
1378
|
+
|
|
1379
|
+
<details>
|
|
1380
|
+
<summary>コード例: ProductMapper.java</summary>
|
|
1381
|
+
|
|
1382
|
+
```java
|
|
1383
|
+
@Mapper
|
|
1384
|
+
public interface ProductMapper {
|
|
1385
|
+
// 既存のメソッド...
|
|
1386
|
+
|
|
1387
|
+
List<Product> findWithPagination(
|
|
1388
|
+
@Param("offset") int offset,
|
|
1389
|
+
@Param("limit") int limit,
|
|
1390
|
+
@Param("category") String category,
|
|
1391
|
+
@Param("keyword") String keyword);
|
|
1392
|
+
|
|
1393
|
+
long count(@Param("category") String category, @Param("keyword") String keyword);
|
|
1394
|
+
}
|
|
1395
|
+
```
|
|
1396
|
+
|
|
1397
|
+
</details>
|
|
1398
|
+
|
|
1399
|
+
### 18.4 リポジトリの実装
|
|
1400
|
+
|
|
1401
|
+
Output Port にページネーション用のメソッドを追加します。
|
|
1402
|
+
|
|
1403
|
+
<details>
|
|
1404
|
+
<summary>コード例: ProductRepository.java(インターフェース)</summary>
|
|
1405
|
+
|
|
1406
|
+
```java
|
|
1407
|
+
public interface ProductRepository {
|
|
1408
|
+
// 既存のメソッド...
|
|
1409
|
+
|
|
1410
|
+
/**
|
|
1411
|
+
* ページネーション付きで商品を取得.
|
|
1412
|
+
*/
|
|
1413
|
+
PageResult<Product> findWithPagination(int page, int size, ProductCategory category, String keyword);
|
|
1414
|
+
}
|
|
1415
|
+
```
|
|
1416
|
+
|
|
1417
|
+
</details>
|
|
1418
|
+
|
|
1419
|
+
<details>
|
|
1420
|
+
<summary>コード例: ProductRepositoryImpl.java(実装)</summary>
|
|
1421
|
+
|
|
1422
|
+
```java
|
|
1423
|
+
@Override
|
|
1424
|
+
public PageResult<Product> findWithPagination(int page, int size, ProductCategory category, String keyword) {
|
|
1425
|
+
int offset = page * size;
|
|
1426
|
+
String categoryName = category != null ? category.getDisplayName() : null;
|
|
1427
|
+
|
|
1428
|
+
List<Product> products = productMapper.findWithPagination(offset, size, categoryName, keyword);
|
|
1429
|
+
long totalElements = productMapper.count(categoryName, keyword);
|
|
1430
|
+
|
|
1431
|
+
return new PageResult<>(products, page, size, totalElements);
|
|
1432
|
+
}
|
|
1433
|
+
```
|
|
1434
|
+
|
|
1435
|
+
</details>
|
|
1436
|
+
|
|
1437
|
+
### 18.5 ユースケースとサービスの実装
|
|
1438
|
+
|
|
1439
|
+
Input Port にページネーション用のメソッドを追加します。
|
|
1440
|
+
|
|
1441
|
+
<details>
|
|
1442
|
+
<summary>コード例: ProductUseCase.java</summary>
|
|
1443
|
+
|
|
1444
|
+
```java
|
|
1445
|
+
public interface ProductUseCase {
|
|
1446
|
+
// 既存のメソッド...
|
|
1447
|
+
|
|
1448
|
+
/**
|
|
1449
|
+
* ページネーション付きで商品を取得する.
|
|
1450
|
+
*/
|
|
1451
|
+
PageResult<Product> getProducts(int page, int size, ProductCategory category, String keyword);
|
|
1452
|
+
}
|
|
1453
|
+
```
|
|
1454
|
+
|
|
1455
|
+
</details>
|
|
1456
|
+
|
|
1457
|
+
<details>
|
|
1458
|
+
<summary>コード例: ProductService.java</summary>
|
|
1459
|
+
|
|
1460
|
+
```java
|
|
1461
|
+
@Override
|
|
1462
|
+
@Transactional(readOnly = true)
|
|
1463
|
+
public PageResult<Product> getProducts(int page, int size, ProductCategory category, String keyword) {
|
|
1464
|
+
return productRepository.findWithPagination(page, size, category, keyword);
|
|
1465
|
+
}
|
|
1466
|
+
```
|
|
1467
|
+
|
|
1468
|
+
</details>
|
|
1469
|
+
|
|
1470
|
+
### 18.6 Controller の実装
|
|
1471
|
+
|
|
1472
|
+
ページネーションパラメータを受け取り、ビューにページ情報を渡します。
|
|
1473
|
+
|
|
1474
|
+
<details>
|
|
1475
|
+
<summary>コード例: ProductWebController.java</summary>
|
|
1476
|
+
|
|
1477
|
+
```java
|
|
1478
|
+
@Controller
|
|
1479
|
+
@RequestMapping("/products")
|
|
1480
|
+
public class ProductWebController {
|
|
1481
|
+
|
|
1482
|
+
private static final int DEFAULT_PAGE_SIZE = 10;
|
|
1483
|
+
|
|
1484
|
+
private final ProductUseCase productUseCase;
|
|
1485
|
+
// ...
|
|
1486
|
+
|
|
1487
|
+
@GetMapping
|
|
1488
|
+
public String list(
|
|
1489
|
+
@RequestParam(defaultValue = "0") int page,
|
|
1490
|
+
@RequestParam(defaultValue = "10") int size,
|
|
1491
|
+
@RequestParam(required = false) ProductCategory category,
|
|
1492
|
+
@RequestParam(required = false) String keyword,
|
|
1493
|
+
Model model) {
|
|
1494
|
+
|
|
1495
|
+
PageResult<Product> productPage = productUseCase.getProducts(page, size, category, keyword);
|
|
1496
|
+
|
|
1497
|
+
model.addAttribute("products", productPage.getContent());
|
|
1498
|
+
model.addAttribute("page", productPage);
|
|
1499
|
+
model.addAttribute("categories", ProductCategory.values());
|
|
1500
|
+
model.addAttribute("selectedCategory", category);
|
|
1501
|
+
model.addAttribute("keyword", keyword);
|
|
1502
|
+
model.addAttribute("currentSize", size);
|
|
1503
|
+
return "products/list";
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
```
|
|
1507
|
+
|
|
1508
|
+
</details>
|
|
1509
|
+
|
|
1510
|
+
### 18.7 Thymeleaf テンプレートの実装
|
|
1511
|
+
|
|
1512
|
+
Bootstrap のページネーションコンポーネントを使用して UI を実装します。
|
|
1513
|
+
|
|
1514
|
+
<details>
|
|
1515
|
+
<summary>コード例: products/list.html(ページネーション部分)</summary>
|
|
1516
|
+
|
|
1517
|
+
```html
|
|
1518
|
+
<!-- 検索フォームに表示件数選択を追加 -->
|
|
1519
|
+
<div class="col-md-2">
|
|
1520
|
+
<label class="form-label">表示件数</label>
|
|
1521
|
+
<select name="size" class="form-select">
|
|
1522
|
+
<option value="10" th:selected="${currentSize == 10}">10件</option>
|
|
1523
|
+
<option value="25" th:selected="${currentSize == 25}">25件</option>
|
|
1524
|
+
<option value="50" th:selected="${currentSize == 50}">50件</option>
|
|
1525
|
+
<option value="100" th:selected="${currentSize == 100}">100件</option>
|
|
1526
|
+
</select>
|
|
1527
|
+
</div>
|
|
1528
|
+
|
|
1529
|
+
<!-- ページネーション -->
|
|
1530
|
+
<div class="mt-3">
|
|
1531
|
+
<div class="text-muted text-center mb-2">
|
|
1532
|
+
<span th:if="${page.totalElements > 0}">
|
|
1533
|
+
<span th:text="${page.page * page.size + 1}">1</span> -
|
|
1534
|
+
<span th:text="${page.page * page.size + #lists.size(products)}">10</span> 件
|
|
1535
|
+
(全 <span th:text="${page.totalElements}">0</span> 件)
|
|
1536
|
+
</span>
|
|
1537
|
+
<span th:if="${page.totalElements == 0}">0 件</span>
|
|
1538
|
+
</div>
|
|
1539
|
+
|
|
1540
|
+
<nav th:if="${page.totalPages > 1}" aria-label="ページナビゲーション">
|
|
1541
|
+
<ul class="pagination justify-content-center mb-0">
|
|
1542
|
+
<!-- 最初のページへ -->
|
|
1543
|
+
<li class="page-item" th:classappend="${page.first} ? 'disabled'">
|
|
1544
|
+
<a class="page-link" th:href="@{/products(page=0, size=${currentSize},
|
|
1545
|
+
category=${selectedCategory?.name()}, keyword=${keyword})}">«</a>
|
|
1546
|
+
</li>
|
|
1547
|
+
<!-- 前のページへ -->
|
|
1548
|
+
<li class="page-item" th:classappend="${!page.hasPrevious()} ? 'disabled'">
|
|
1549
|
+
<a class="page-link" th:href="@{/products(page=${page.page - 1},
|
|
1550
|
+
size=${currentSize}, category=${selectedCategory?.name()},
|
|
1551
|
+
keyword=${keyword})}">‹</a>
|
|
1552
|
+
</li>
|
|
1553
|
+
|
|
1554
|
+
<!-- ページ番号 -->
|
|
1555
|
+
<th:block th:with="startPage=${page.page > 2 ? page.page - 2 : 0},
|
|
1556
|
+
endPage=${page.page + 2 < page.totalPages - 1 ? page.page + 2 : page.totalPages - 1}">
|
|
1557
|
+
<li class="page-item" th:if="${startPage > 0}">
|
|
1558
|
+
<span class="page-link">...</span>
|
|
1559
|
+
</li>
|
|
1560
|
+
<li th:each="i : ${#numbers.sequence(startPage, endPage)}"
|
|
1561
|
+
class="page-item"
|
|
1562
|
+
th:classappend="${i == page.page} ? 'active'">
|
|
1563
|
+
<a class="page-link"
|
|
1564
|
+
th:href="@{/products(page=${i}, size=${currentSize},
|
|
1565
|
+
category=${selectedCategory?.name()}, keyword=${keyword})}"
|
|
1566
|
+
th:text="${i + 1}">1</a>
|
|
1567
|
+
</li>
|
|
1568
|
+
<li class="page-item" th:if="${endPage < page.totalPages - 1}">
|
|
1569
|
+
<span class="page-link">...</span>
|
|
1570
|
+
</li>
|
|
1571
|
+
</th:block>
|
|
1572
|
+
|
|
1573
|
+
<!-- 次のページへ -->
|
|
1574
|
+
<li class="page-item" th:classappend="${!page.hasNext()} ? 'disabled'">
|
|
1575
|
+
<a class="page-link" th:href="@{/products(page=${page.page + 1},
|
|
1576
|
+
size=${currentSize}, category=${selectedCategory?.name()},
|
|
1577
|
+
keyword=${keyword})}">›</a>
|
|
1578
|
+
</li>
|
|
1579
|
+
<!-- 最後のページへ -->
|
|
1580
|
+
<li class="page-item" th:classappend="${page.last} ? 'disabled'">
|
|
1581
|
+
<a class="page-link" th:href="@{/products(page=${page.totalPages - 1},
|
|
1582
|
+
size=${currentSize}, category=${selectedCategory?.name()},
|
|
1583
|
+
keyword=${keyword})}">»</a>
|
|
1584
|
+
</li>
|
|
1585
|
+
</ul>
|
|
1586
|
+
</nav>
|
|
1587
|
+
</div>
|
|
1588
|
+
```
|
|
1589
|
+
|
|
1590
|
+
</details>
|
|
1591
|
+
|
|
1592
|
+
### 18.8 ページネーションの設計ポイント
|
|
1593
|
+
|
|
1594
|
+
| ポイント | 説明 |
|
|
1595
|
+
|---------|------|
|
|
1596
|
+
| **0始まり vs 1始まり** | 内部では0始まり、表示では1始まりを使用 |
|
|
1597
|
+
| **デフォルト値** | ページサイズは 10 件程度が一般的 |
|
|
1598
|
+
| **検索条件の保持** | ページ遷移時も検索条件(カテゴリ、キーワード)を保持 |
|
|
1599
|
+
| **ページ番号の省略** | 大量ページの場合は「...」で省略 |
|
|
1600
|
+
| **SQL 最適化** | OFFSET が大きい場合のパフォーマンスに注意 |
|
|
1601
|
+
|
|
1602
|
+
### 18.9 パフォーマンス考慮事項
|
|
1603
|
+
|
|
1604
|
+
```plantuml
|
|
1605
|
+
@startuml offset_performance
|
|
1606
|
+
title OFFSET のパフォーマンス問題
|
|
1607
|
+
|
|
1608
|
+
note as N1
|
|
1609
|
+
**OFFSET が大きい場合の問題**
|
|
1610
|
+
|
|
1611
|
+
SELECT * FROM products
|
|
1612
|
+
ORDER BY product_code
|
|
1613
|
+
LIMIT 10 OFFSET 100000
|
|
1614
|
+
|
|
1615
|
+
→ 先頭から100,000件スキャンしてから
|
|
1616
|
+
10件を返す(非効率)
|
|
1617
|
+
|
|
1618
|
+
**対策**
|
|
1619
|
+
1. Keyset Pagination(カーソル方式)
|
|
1620
|
+
2. インデックス付きカラムでの絞り込み
|
|
1621
|
+
3. キャッシュの活用
|
|
1622
|
+
end note
|
|
1623
|
+
|
|
1624
|
+
@enduml
|
|
1625
|
+
```
|
|
1626
|
+
|
|
1627
|
+
大量データの場合は、OFFSET ベースではなく **Keyset Pagination**(カーソル方式)を検討します:
|
|
1628
|
+
|
|
1629
|
+
```sql
|
|
1630
|
+
-- OFFSET 方式(非効率)
|
|
1631
|
+
SELECT * FROM products ORDER BY product_code LIMIT 10 OFFSET 100000;
|
|
1632
|
+
|
|
1633
|
+
-- Keyset Pagination(効率的)
|
|
1634
|
+
SELECT * FROM products
|
|
1635
|
+
WHERE product_code > 'LAST_SEEN_CODE'
|
|
1636
|
+
ORDER BY product_code
|
|
1637
|
+
LIMIT 10;
|
|
1638
|
+
```
|
|
1639
|
+
|
|
1640
|
+
---
|
|
1641
|
+
|
|
1642
|
+
## 第10部-B のまとめ
|
|
1643
|
+
|
|
1644
|
+
### API サーバー版との比較
|
|
1645
|
+
|
|
1646
|
+
| 観点 | API サーバー版 | モノリス版 |
|
|
1647
|
+
|------|--------------|-----------|
|
|
1648
|
+
| **アーキテクチャ** | フロントエンド分離(SPA) | 統合(Thymeleaf) |
|
|
1649
|
+
| **通信** | REST API(JSON) | サーバーサイドレンダリング(HTML) |
|
|
1650
|
+
| **状態管理** | クライアント側(localStorage 等) | サーバー側(セッション) |
|
|
1651
|
+
| **部分更新** | JavaScript(Axios 等) | htmx |
|
|
1652
|
+
| **帳票出力** | API 経由でダウンロード | 直接ダウンロード |
|
|
1653
|
+
| **認証** | JWT/OAuth | セッションベース |
|
|
1654
|
+
| **複雑さ** | 高(API 設計、CORS 等) | 低(シンプル) |
|
|
1655
|
+
| **開発速度** | 初期は遅い | 初期は速い |
|
|
1656
|
+
|
|
1657
|
+
### 実装した機能
|
|
1658
|
+
|
|
1659
|
+
| カテゴリ | 機能 |
|
|
1660
|
+
|---------|------|
|
|
1661
|
+
| **商品マスタ** | 一覧、詳細、登録、編集、削除、検索、ページネーション |
|
|
1662
|
+
| **取引先マスタ** | 一覧、詳細(取引履歴)、登録、編集、削除、検索 |
|
|
1663
|
+
| **受注** | 一覧、登録(動的明細)、確定、取消 |
|
|
1664
|
+
| **出荷** | 一覧、出荷指示、出荷実行、取消 |
|
|
1665
|
+
| **請求** | 一覧、締処理、請求書発行 |
|
|
1666
|
+
| **入金** | 一覧、入金登録、消込処理 |
|
|
1667
|
+
| **帳票** | Excel/PDF 出力 |
|
|
1668
|
+
| **共通** | レイアウト、エラーハンドリング、フラッシュメッセージ、ページネーション |
|
|
1669
|
+
|
|
1670
|
+
### 技術スタック
|
|
1671
|
+
|
|
1672
|
+
| カテゴリ | 技術 |
|
|
1673
|
+
|---------|------|
|
|
1674
|
+
| **言語** | Java 21 |
|
|
1675
|
+
| **フレームワーク** | Spring Boot 3.4 |
|
|
1676
|
+
| **テンプレート** | Thymeleaf |
|
|
1677
|
+
| **部分更新** | htmx |
|
|
1678
|
+
| **CSS** | Bootstrap 5 |
|
|
1679
|
+
| **ORM** | MyBatis |
|
|
1680
|
+
| **データベース** | PostgreSQL 16 |
|
|
1681
|
+
| **Excel** | Apache POI |
|
|
1682
|
+
| **PDF** | OpenHTMLtoPDF(Thymeleaf + HTML/CSS) |
|
|
1683
|
+
| **テスト** | JUnit 5, TestContainers |
|
|
1684
|
+
|
|
1685
|
+
### モノリスを選択すべき場面
|
|
1686
|
+
|
|
1687
|
+
1. **社内システム**: 限られたユーザー、SEO 不要
|
|
1688
|
+
2. **業務アプリケーション**: 複雑な業務フロー
|
|
1689
|
+
3. **小規模チーム**: フルスタック開発
|
|
1690
|
+
4. **迅速な開発**: MVP、プロトタイプ
|
|
1691
|
+
5. **運用コスト重視**: シンプルなインフラ
|
|
1692
|
+
|
|
1693
|
+
モノリスアーキテクチャは決して「古い」アーキテクチャではありません。適切な場面で選択することで、シンプルかつ効率的なシステム開発が可能です。
|