質問編集履歴
2
試してみたこと
title
CHANGED
File without changes
|
body
CHANGED
@@ -88,4 +88,131 @@
|
|
88
88
|
|
89
89
|
</project>
|
90
90
|
|
91
|
+
```
|
92
|
+
|
93
|
+
コンバーターと列挙型を作成してみましたがうまくいきませんでした。
|
94
|
+
|
95
|
+
```コンバータークラス
|
96
|
+
package com.funayama.springboot;
|
97
|
+
|
98
|
+
import javax.persistence.AttributeConverter;
|
99
|
+
|
100
|
+
//フォーム用クラス
|
101
|
+
public class Converter implements AttributeConverter<JenreEnum, String> {
|
102
|
+
//データーベースに登録する値(列挙型使用)
|
103
|
+
@Override
|
104
|
+
public String convertToDatabaseColumn(JenreEnum jenreCode) {
|
105
|
+
if (jenreCode == null) {
|
106
|
+
return null;
|
107
|
+
}
|
108
|
+
switch (jenreCode) {
|
109
|
+
case Clock:
|
110
|
+
return "1";
|
111
|
+
|
112
|
+
case Electron:
|
113
|
+
return "2";
|
114
|
+
|
115
|
+
case Phone:
|
116
|
+
return "3";
|
117
|
+
|
118
|
+
default:
|
119
|
+
return null;
|
120
|
+
|
121
|
+
}
|
122
|
+
}
|
123
|
+
|
124
|
+
//Entityに登録する値(列挙型使用)
|
125
|
+
@Override
|
126
|
+
public JenreEnum convertToEntityAttribute(String dbData) {
|
127
|
+
if (dbData == null) {
|
128
|
+
return null;
|
129
|
+
}
|
130
|
+
switch (dbData) {
|
131
|
+
case "1":
|
132
|
+
return JenreEnum.Clock;
|
133
|
+
case "2":
|
134
|
+
return JenreEnum.Electron;
|
135
|
+
case "3":
|
136
|
+
return JenreEnum.Phone;
|
137
|
+
default:
|
138
|
+
return null;
|
139
|
+
}
|
140
|
+
}
|
141
|
+
|
142
|
+
}
|
143
|
+
|
144
|
+
```
|
145
|
+
|
146
|
+
```列挙型クラス
|
147
|
+
ackage com.funayama.springboot;
|
148
|
+
|
149
|
+
import javax.persistence.Convert;
|
150
|
+
|
151
|
+
@Convert(converter = Converter.class)
|
152
|
+
public enum JenreEnum {
|
153
|
+
|
154
|
+
Unspecified("0", "指定なし"),
|
155
|
+
|
156
|
+
Clock("1", "時計"),
|
157
|
+
|
158
|
+
Electron("2", "電子"),
|
159
|
+
|
160
|
+
Phone("3", "携帯");
|
161
|
+
|
162
|
+
//ジャンルID
|
163
|
+
private String genreId;
|
164
|
+
|
165
|
+
//ジャンル名
|
166
|
+
private String genreName;
|
167
|
+
|
168
|
+
//コンストラクタ
|
169
|
+
private JenreEnum(String genreId, String genreName) {
|
170
|
+
this.genreId = genreId;
|
171
|
+
this.genreId = genreName;
|
172
|
+
}
|
173
|
+
|
174
|
+
//商品IDゲッター
|
175
|
+
public String getGenreId() {
|
176
|
+
return genreId;
|
177
|
+
}
|
178
|
+
|
179
|
+
//ジャンル名ゲッター
|
180
|
+
public String getGenreName() {
|
181
|
+
return genreName;
|
182
|
+
}
|
183
|
+
|
184
|
+
}
|
185
|
+
|
186
|
+
```
|
187
|
+
|
188
|
+
```Entityクラス
|
189
|
+
@Convert(converter = Converter.class)
|
190
|
+
@Column(name = "product_genre",nullable = false,length = 2)
|
191
|
+
private String genre;
|
192
|
+
```
|
193
|
+
|
194
|
+
EntityをEnumに設定できないとエラーが発生してしまいました。
|
195
|
+
データーベースの型がcharなのにStringでしてしまっているからなのか
|
196
|
+
HTMLからの取り出し方に問題があるのか
|
197
|
+
なにがおかしいのか見当もつきません。
|
198
|
+
どなたかお力添えをしていただけないでしょうか。。
|
199
|
+
ちなみにHTMLも載せておきます
|
200
|
+
|
201
|
+
```HTML
|
202
|
+
<form method="post" action="/ProductList" th:action="@{/ProductList}" th:object="${formList}">
|
203
|
+
<div class='form-id'>
|
204
|
+
<!--商品ID-->
|
205
|
+
<label>※必須※ 商品ID</label> <br> <input type="text"
|
206
|
+
name="id">
|
207
|
+
</div>
|
208
|
+
<!--ジャンル-->
|
209
|
+
<div class='form-jenre'>
|
210
|
+
<label>ジャンル</label><br> <select style="width: 160px;"
|
211
|
+
name="genre">
|
212
|
+
<option value='0'></option>
|
213
|
+
<option value='1'>時計</option>
|
214
|
+
<option value='2'>電子</option>
|
215
|
+
<option value='3'>携帯</option>
|
216
|
+
</select>
|
217
|
+
</div>
|
91
218
|
```
|
1
追加の情報
title
CHANGED
File without changes
|
body
CHANGED
@@ -7,4 +7,85 @@
|
|
7
7
|
初心者で知識不足なので申し訳ないですが
|
8
8
|
どなたかご教授いただけないでしょうか。
|
9
9
|
DBの型がchar型になっていて、enunではStringで作成してます。
|
10
|
-
こちらは変換なども必要なのでしょうか。
|
10
|
+
こちらは変換なども必要なのでしょうか。
|
11
|
+
|
12
|
+
追加の情報です。
|
13
|
+
A-pzさんのアドバイスに明確に答えられないかもしれません。
|
14
|
+
以下、私が使っているものです。
|
15
|
+
|
16
|
+
・エクリプスのSTSでスタータースプリングブート
|
17
|
+
・MYSQL
|
18
|
+
・Marven
|
19
|
+
・トムキャット
|
20
|
+
・mysql.jdbc.Driver
|
21
|
+
・ライブラリがよくわかっていないのですが
|
22
|
+
JREシステムライブラリというフォルダがプロジェクトにあります。
|
23
|
+
|
24
|
+
・
|
25
|
+
```pom.xml
|
26
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
27
|
+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
28
|
+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
29
|
+
<modelVersion>4.0.0</modelVersion>
|
30
|
+
|
31
|
+
<groupId>com.funayama.springboot</groupId>
|
32
|
+
<artifactId>pda_k_funayama</artifactId>
|
33
|
+
<version>0.0.1-SNAPSHOT</version>
|
34
|
+
<packaging>jar</packaging>
|
35
|
+
|
36
|
+
<name>pda_k_funayama</name>
|
37
|
+
<description>sample project for Spring Boot</description>
|
38
|
+
|
39
|
+
<parent>
|
40
|
+
<groupId>org.springframework.boot</groupId>
|
41
|
+
<artifactId>spring-boot-starter-parent</artifactId>
|
42
|
+
<version>2.0.5.RELEASE</version>
|
43
|
+
<relativePath/> <!-- lookup parent from repository -->
|
44
|
+
</parent>
|
45
|
+
|
46
|
+
<properties>
|
47
|
+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
48
|
+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
49
|
+
<java.version>1.8</java.version>
|
50
|
+
</properties>
|
51
|
+
|
52
|
+
<dependencies>
|
53
|
+
<dependency>
|
54
|
+
<groupId>org.springframework.boot</groupId>
|
55
|
+
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
56
|
+
</dependency>
|
57
|
+
|
58
|
+
<dependency>
|
59
|
+
<groupId>org.springframework.boot</groupId>
|
60
|
+
<artifactId>spring-boot-starter-web</artifactId>
|
61
|
+
</dependency>
|
62
|
+
|
63
|
+
<dependency>
|
64
|
+
<groupId>mysql</groupId>
|
65
|
+
<artifactId>mysql-connector-java</artifactId>
|
66
|
+
<scope>runtime</scope>
|
67
|
+
</dependency>
|
68
|
+
<dependency>
|
69
|
+
<groupId>org.springframework.boot</groupId>
|
70
|
+
<artifactId>spring-boot-starter-test</artifactId>
|
71
|
+
<scope>test</scope>
|
72
|
+
</dependency>
|
73
|
+
<dependency>
|
74
|
+
<groupId>org.springframework.boot</groupId>
|
75
|
+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
76
|
+
</dependency>
|
77
|
+
</dependencies>
|
78
|
+
|
79
|
+
<build>
|
80
|
+
<plugins>
|
81
|
+
<plugin>
|
82
|
+
<groupId>org.springframework.boot</groupId>
|
83
|
+
<artifactId>spring-boot-maven-plugin</artifactId>
|
84
|
+
</plugin>
|
85
|
+
</plugins>
|
86
|
+
</build>
|
87
|
+
|
88
|
+
|
89
|
+
</project>
|
90
|
+
|
91
|
+
```
|