質問編集履歴
1
テキスト追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -56,6 +56,148 @@
|
|
56
56
|
|
57
57
|
|
58
58
|
|
59
|
+
**テキスト**
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
package com.example.demo;
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
import java.time.LocalDateTime;
|
68
|
+
|
69
|
+
import java.util.List;
|
70
|
+
|
71
|
+
import java.util.Optional;
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
import org.springframework.beans.factory.annotation.Autowired;
|
76
|
+
|
77
|
+
import org.springframework.stereotype.Controller;
|
78
|
+
|
79
|
+
import org.springframework.ui.Model;
|
80
|
+
|
81
|
+
import org.springframework.web.bind.annotation.GetMapping;
|
82
|
+
|
83
|
+
import org.springframework.web.bind.annotation.PathVariable;
|
84
|
+
|
85
|
+
import org.springframework.web.bind.annotation.PostMapping;
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
@Controller
|
90
|
+
|
91
|
+
public class Sample7Controller {
|
92
|
+
|
93
|
+
@Autowired
|
94
|
+
|
95
|
+
private BlogRepository blogRepository;
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
@GetMapping("/")
|
100
|
+
|
101
|
+
public String index(Model model) {
|
102
|
+
|
103
|
+
List<Blog> blogs = blogRepository.findAll();
|
104
|
+
|
105
|
+
model.addAttribute("blogs", blogs);
|
106
|
+
|
107
|
+
return "idnex";
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
@GetMapping("/form")
|
112
|
+
|
113
|
+
public String form(Blog blog) {
|
114
|
+
|
115
|
+
return "form";
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
@PostMapping("/create")
|
122
|
+
|
123
|
+
public String create(Blog blog) {
|
124
|
+
|
125
|
+
blog.setPostDateTime(LocalDateTime.now());
|
126
|
+
|
127
|
+
blogRepository.save(blog);
|
128
|
+
|
129
|
+
return "redirect:/blog/" + blog.getId();
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
@GetMapping("/blog/{id}")
|
136
|
+
|
137
|
+
public String blog(@PathVariable Integer id, Model model) {
|
138
|
+
|
139
|
+
Optional<Blog> blog = blogRepository.findById(id);
|
140
|
+
|
141
|
+
model.addAttribute("blog", blog.get());
|
142
|
+
|
143
|
+
return "blog";
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
**Blog.java
|
152
|
+
|
153
|
+
テキスト**
|
154
|
+
|
155
|
+
package com.example.demo;
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
import java.time.LocalDateTime;
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
import javax.persistence.Column;
|
164
|
+
|
165
|
+
import javax.persistence.Entity;
|
166
|
+
|
167
|
+
import javax.persistence.GeneratedValue;
|
168
|
+
|
169
|
+
import javax.persistence.GenerationType;
|
170
|
+
|
171
|
+
import javax.persistence.Id;
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
@Entity
|
176
|
+
|
177
|
+
public class Blog {
|
178
|
+
|
179
|
+
@Id
|
180
|
+
|
181
|
+
@GeneratedValue(strategy = GenerationType.AUTO)
|
182
|
+
|
183
|
+
private Integer id;
|
184
|
+
|
185
|
+
private String title;
|
186
|
+
|
187
|
+
private LocalDateTime postDateTime;
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
@Column(length = 1000)
|
192
|
+
|
193
|
+
private String contents;
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
|
200
|
+
|
59
201
|
lombokは導入しています。
|
60
202
|
|
61
203
|
|