質問編集履歴
2
追記しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -215,3 +215,75 @@
|
|
215
215
|
|
216
216
|
|
217
217
|
```
|
218
|
+
|
219
|
+
### 該当のソースコード
|
220
|
+
|
221
|
+
```ここに言語名を入力
|
222
|
+
|
223
|
+
package com.example.demo.entity;
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
import lombok.Data;
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
@Data
|
232
|
+
|
233
|
+
public class MUser {
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
private String userId;
|
238
|
+
|
239
|
+
private String password;
|
240
|
+
|
241
|
+
private String role;
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
```
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
### 該当のソースコード
|
252
|
+
|
253
|
+
```ここに言語名を入力
|
254
|
+
|
255
|
+
package com.example.demo.entity;
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
import java.time.LocalDateTime;
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
import lombok.Getter;
|
264
|
+
|
265
|
+
import lombok.Setter;
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
@Getter
|
270
|
+
|
271
|
+
@Setter
|
272
|
+
|
273
|
+
public class Note {
|
274
|
+
|
275
|
+
private int id;
|
276
|
+
|
277
|
+
private LocalDateTime dateTime;
|
278
|
+
|
279
|
+
private String title;
|
280
|
+
|
281
|
+
private String name;
|
282
|
+
|
283
|
+
private String contents;
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
```
|
1
ソールコードを追記しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -13,3 +13,205 @@
|
|
13
13
|
スコープがsingletonだからだと思うのですが、sessionもしくはapplication
|
14
14
|
|
15
15
|
に変更すれば良いのでしょうか?
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
### 該当のソースコード
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
```ここに言語名を入力
|
26
|
+
|
27
|
+
CREATE TABLE note (
|
28
|
+
|
29
|
+
id int(5) NOT NULL AUTO_INCREMENT,
|
30
|
+
|
31
|
+
dateTime datetime,
|
32
|
+
|
33
|
+
name varchar(20),
|
34
|
+
|
35
|
+
title varchar(30),
|
36
|
+
|
37
|
+
contents varchar(500) NOT NULL,
|
38
|
+
|
39
|
+
PRIMARY KEY (id)
|
40
|
+
|
41
|
+
);
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
CREATE TABLE IF NOT EXISTS user (
|
46
|
+
|
47
|
+
user_id VARCHAR(50) PRIMARY KEY,
|
48
|
+
|
49
|
+
password VARCHAR(100),
|
50
|
+
|
51
|
+
role VARCHAR(50)
|
52
|
+
|
53
|
+
);
|
54
|
+
|
55
|
+
```### 該当のソースコード
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
```ここに言語名を入力
|
60
|
+
|
61
|
+
package com.example.demo.Dao;
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
import java.sql.Timestamp;
|
66
|
+
|
67
|
+
import java.util.ArrayList;
|
68
|
+
|
69
|
+
import java.util.List;
|
70
|
+
|
71
|
+
import java.util.Map;
|
72
|
+
|
73
|
+
import java.util.Optional;
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
import org.springframework.beans.factory.annotation.Autowired;
|
78
|
+
|
79
|
+
import org.springframework.jdbc.core.JdbcTemplate;
|
80
|
+
|
81
|
+
import org.springframework.stereotype.Repository;
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
import com.example.demo.entity.Note;
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
@Repository
|
90
|
+
|
91
|
+
public class NoteDaoImpl implements NoteDao {
|
92
|
+
|
93
|
+
// データベース操作用のクラス
|
94
|
+
|
95
|
+
private final JdbcTemplate jdbcTemplate;
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
@Autowired
|
100
|
+
|
101
|
+
public NoteDaoImpl(JdbcTemplate jdbcTemplate) {
|
102
|
+
|
103
|
+
this.jdbcTemplate = jdbcTemplate;
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
@Override
|
110
|
+
|
111
|
+
public void create(Note note) {
|
112
|
+
|
113
|
+
jdbcTemplate.update("INSERT INTO note(dateTime, title, name, contents) values(?,?,?,?)", note.getDateTime(),
|
114
|
+
|
115
|
+
note.getTitle(), note.getName(), note.getContents());
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
@Override
|
122
|
+
|
123
|
+
public int update(Note note) {
|
124
|
+
|
125
|
+
return jdbcTemplate.update("UPDATE note SET dateTime = ?, name = ?, title = ?, contents = ? WHERE id = ?",
|
126
|
+
|
127
|
+
note.getDateTime(), note.getName(), note.getTitle(), note.getContents(), note.getId());
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
@Override
|
134
|
+
|
135
|
+
public int deleteById(int id) {
|
136
|
+
|
137
|
+
return jdbcTemplate.update("DELETE FROM note WHERE id = ?", id);
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
@Override
|
144
|
+
|
145
|
+
public List<Note> getAll() {
|
146
|
+
|
147
|
+
String sql = "SELECT id, dateTime, name, title, contents FROM note ORDER BY id DESC";
|
148
|
+
|
149
|
+
List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql);
|
150
|
+
|
151
|
+
List<Note> list = new ArrayList<Note>();
|
152
|
+
|
153
|
+
for (Map<String, Object> result : resultList) {
|
154
|
+
|
155
|
+
Note note = new Note();
|
156
|
+
|
157
|
+
note.setId((int) result.get("id"));
|
158
|
+
|
159
|
+
note.setDateTime(((Timestamp) result.get("dateTime")).toLocalDateTime());
|
160
|
+
|
161
|
+
note.setName((String) result.get("name"));
|
162
|
+
|
163
|
+
note.setTitle((String) result.get("title"));
|
164
|
+
|
165
|
+
note.setContents((String) result.get("contents"));
|
166
|
+
|
167
|
+
list.add(note);
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
return list;
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
@Override
|
178
|
+
|
179
|
+
public Optional<Note> findById(int id) {
|
180
|
+
|
181
|
+
String sql = "SELECT id, dateTime, name, title, contents FROM note WHERE id = ?";
|
182
|
+
|
183
|
+
Map<String, Object> result = jdbcTemplate.queryForMap(sql, id);
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
Note note = new Note();
|
188
|
+
|
189
|
+
note.setId((int) result.get("id"));
|
190
|
+
|
191
|
+
note.setDateTime(((Timestamp) result.get("dateTime")).toLocalDateTime());
|
192
|
+
|
193
|
+
note.setName((String) result.get("name"));
|
194
|
+
|
195
|
+
note.setTitle((String) result.get("title"));
|
196
|
+
|
197
|
+
note.setContents((String) result.get("contents"));
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
// NoteをOptionalでラップする
|
202
|
+
|
203
|
+
Optional<Note> noteOpt = Optional.ofNullable(note);
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
return noteOpt;
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
```
|