質問編集履歴

2

初心者マーク追加

2020/10/06 05:14

投稿

OG.
OG.

スコア7

test CHANGED
File without changes
test CHANGED
@@ -18,8 +18,6 @@
18
18
 
19
19
 
20
20
 
21
-
22
-
23
21
  ### 発生している問題・エラーメッセージ
24
22
 
25
23
 

1

list.htmlがうまく反映されてなかったため修正

2020/10/06 05:14

投稿

OG.
OG.

スコア7

test CHANGED
File without changes
test CHANGED
@@ -38,45 +38,257 @@
38
38
 
39
39
  ### 該当のソースコード
40
40
 
41
- AddressListApplication.java
41
+ DemoController.java
42
-
42
+
43
- ```java
43
+ ```ここに言語名を入力
44
44
 
45
45
  package com.example.samplelist;
46
46
 
47
47
 
48
48
 
49
- import org.springframework.boot.SpringApplication;
50
-
51
- import org.springframework.boot.autoconfigure.SpringBootApplication;
52
-
53
-
54
-
55
- @SpringBootApplication
56
-
57
- public class AddressListApplication {
58
-
59
-
60
-
61
- public static void main(String[] args) {
62
-
63
- SpringApplication.run(AddressListApplication.class, args);
64
-
65
- }
66
-
67
-
49
+ import org.springframework.beans.factory.annotation.Autowired;
50
+
51
+ import org.springframework.context.annotation.ComponentScan;
52
+
53
+ import org.springframework.stereotype.Controller;
54
+
55
+ import org.springframework.web.bind.annotation.DeleteMapping;
56
+
57
+ import org.springframework.web.bind.annotation.GetMapping;
58
+
59
+ import org.springframework.web.bind.annotation.PostMapping;
60
+
61
+ import org.springframework.web.bind.annotation.RequestMapping;
62
+
63
+ import org.springframework.web.bind.annotation.RequestMethod;
64
+
65
+ import org.springframework.ui.Model;
66
+
67
+ import org.springframework.validation.BindingResult;
68
+
69
+ import org.springframework.validation.annotation.Validated;
70
+
71
+ import org.springframework.web.bind.annotation.RequestParam;
72
+
73
+ import org.springframework.web.bind.annotation.ModelAttribute;
74
+
75
+ import org.springframework.web.servlet.ModelAndView;
76
+
77
+
78
+
79
+ import java.util.List;
80
+
81
+
82
+
83
+ import javax.annotation.PostConstruct;
84
+
85
+
86
+
87
+ @ComponentScan
88
+
89
+ @Controller
90
+
91
+ public class DemoController {
92
+
93
+ private final UserRepository repos;
94
+
95
+
96
+
97
+ @Autowired
98
+
99
+ SearchService service;
100
+
101
+
102
+
103
+ @Autowired
104
+
105
+ public DemoController(UserRepository repos) {
106
+
107
+ this.repos = repos;
108
+
109
+ }
110
+
111
+
112
+
113
+ /* 一覧画面(初期画面)への遷移 */
114
+
115
+ @GetMapping("/")
116
+
117
+ ModelAndView getAlllists(@RequestParam(required = false) String name, @RequestParam(required = false) String address,
118
+
119
+ @RequestParam(required = false) String tel,
120
+
121
+ @RequestParam(required = false) String zip,
122
+
123
+ @RequestParam(required = false) String area) {
124
+
125
+ ModelAndView mav = new ModelAndView();
126
+
127
+ User data = new User();
128
+
129
+ mav.addObject("formModel", data);
130
+
131
+ mav.addObject("name", name);
132
+
133
+ mav.addObject("addrass", address);
134
+
135
+ mav.addObject("tel",tel);
136
+
137
+ mav.addObject("zip", zip);
138
+
139
+ mav.addObject("area", area);
140
+
141
+ // COMMENTテーブル:レコード全件取得
142
+
143
+ mav.addObject("lists", repos.findAll());
144
+
145
+ mav.setViewName("users/list");
146
+
147
+ return mav;
148
+
149
+ }
150
+
151
+
152
+
153
+ /* 新規画面への遷移 */
154
+
155
+ @GetMapping("/add")
156
+
157
+ ModelAndView add() {
158
+
159
+ ModelAndView mav = new ModelAndView();
160
+
161
+ User data = new User();
162
+
163
+ mav.addObject("formModel", data);
164
+
165
+ mav.setViewName("users/new");
166
+
167
+ return mav;
168
+
169
+ }
170
+
171
+
172
+
173
+ /* 編集画面への遷移 */
174
+
175
+ @GetMapping("/edit")
176
+
177
+ ModelAndView edit(@RequestParam int id) {
178
+
179
+ ModelAndView mav = new ModelAndView();
180
+
181
+ User data = repos.findById(id);
182
+
183
+ mav.addObject("formModel", data);
184
+
185
+ mav.setViewName("users/new");
186
+
187
+ return mav;
188
+
189
+ }
190
+
191
+
192
+
193
+ /* 更新処理 */
194
+
195
+ @PostMapping("")//ここ修正する
196
+
197
+ public String addComment(User user, BindingResult result, Model model) {
198
+
199
+ model.addAttribute("views", repos.findAll());
200
+
201
+ if (result.hasErrors()) {
202
+
203
+ return "users/list";
204
+
205
+ }
206
+
207
+ // COMMENTテーブル:コメント登録
208
+
209
+ repos.save(user);
210
+
211
+ // ルートパス("/") にリダイレクトします
212
+
213
+ return "redirect:/";
214
+
215
+ }
216
+
217
+
218
+
219
+ /* 削除処理 */
220
+
221
+ @DeleteMapping("/delete")//ここも修正する
222
+
223
+ public String deleteComment(@Validated @ModelAttribute User user, BindingResult result, Model model ) {
224
+
225
+ model.addAttribute("views", repos.findAll());
226
+
227
+ // COMMENTテーブル:レコード削除
228
+
229
+ repos.deleteById(user.getId());
230
+
231
+ // ルートパス("/") にリダイレクトします
232
+
233
+ return "redirect:/deletes";
234
+
235
+ }
236
+
237
+
238
+
239
+ @RequestMapping(value = "/search", method = RequestMethod.GET)
240
+
241
+ public String index() {
242
+
243
+ return "users/list";
244
+
245
+ }
246
+
247
+
248
+
249
+ //検索機能
250
+
251
+ @RequestMapping(value = "/search", method = RequestMethod.POST)
252
+
253
+ public ModelAndView search(ModelAndView mav,
254
+
255
+ @RequestParam(required = false) String name, @RequestParam(required = false) String address,
256
+
257
+ @RequestParam(required = false) String tel,
258
+
259
+ @RequestParam(required = false) String zip,
260
+
261
+ @RequestParam(required = false) String area) {
262
+
263
+ mav.setViewName("users/list");
264
+
265
+ mav.addObject("name", name);
266
+
267
+ List<User> lists = service.search(name, address,tel, zip,area);
268
+
269
+ mav.addObject("lists", lists);
270
+
271
+ return mav;
272
+
273
+ }
274
+
275
+
276
+
277
+ @PostConstruct
278
+
279
+ public void init() {
280
+
281
+ /* 初期データ作成部分 */
282
+
283
+ }
68
284
 
69
285
  }
70
286
 
71
-
72
-
73
287
  ```
74
288
 
75
289
 
76
290
 
77
-
78
-
79
- DemoController.java
291
+ User.java
80
292
 
81
293
  ```ここに言語名を入力
82
294
 
@@ -84,632 +296,390 @@
84
296
 
85
297
 
86
298
 
299
+ import javax.persistence.Column;
300
+
301
+ import javax.persistence.Entity;
302
+
303
+ import javax.persistence.GeneratedValue;
304
+
305
+ import javax.persistence.GenerationType;
306
+
307
+ import javax.persistence.Id;
308
+
309
+ import javax.persistence.Table;
310
+
311
+
312
+
313
+ @Entity
314
+
315
+ @Table(name="addresslist")
316
+
317
+ public class User {
318
+
319
+ @Id
320
+
321
+ @GeneratedValue(strategy = GenerationType.AUTO)
322
+
323
+ @Column
324
+
325
+ private int id;
326
+
327
+ @Column(name="name")
328
+
329
+ private String name;
330
+
331
+ @Column
332
+
333
+ private String address;
334
+
335
+ @Column
336
+
337
+ private String tel;
338
+
339
+ @Column
340
+
341
+ private String zip;
342
+
343
+ @Column
344
+
345
+ private String area;
346
+
347
+
348
+
349
+ //getter setter 部分省略
350
+
351
+ }
352
+
353
+
354
+
355
+ ```
356
+
357
+ UserRepository.java
358
+
359
+ ```ここに言語名を入力
360
+
361
+ package com.example.samplelist;
362
+
363
+
364
+
365
+ import org.springframework.data.jpa.repository.JpaRepository;
366
+
367
+
368
+
369
+ public interface UserRepository extends JpaRepository<User, Long>{
370
+
371
+ public User findById(int id);
372
+
373
+ public void deleteById(int id);
374
+
375
+ }
376
+
377
+
378
+
379
+ ```
380
+
381
+ SearchService.java
382
+
383
+ ```ここに言語名を入力
384
+
385
+ package com.example.samplelist;
386
+
387
+
388
+
389
+ import java.util.List;
390
+
87
391
  import org.springframework.beans.factory.annotation.Autowired;
88
392
 
89
- import org.springframework.context.annotation.ComponentScan;
90
-
91
- import org.springframework.stereotype.Controller;
393
+ import org.springframework.stereotype.Service;
394
+
92
-
395
+ @Service
396
+
397
+ public class SearchService {
398
+
399
+
400
+
401
+ @Autowired
402
+
403
+ UserRepository repos;
404
+
405
+
406
+
407
+ SearchRepositoryCustom repositoryCustom;
408
+
409
+
410
+
93
- import org.springframework.web.bind.annotation.DeleteMapping;
411
+ public List<User> search(String name, String address, String tel, String zip, String area){
412
+
94
-
413
+ List<User> lists;
414
+
95
- import org.springframework.web.bind.annotation.GetMapping;
415
+ if ("".equals(name) ) {
416
+
96
-
417
+ lists = repos.findAll();
418
+
419
+ } else {
420
+
97
- import org.springframework.web.bind.annotation.PostMapping;
421
+ lists = SearchRepositoryCustom.search(name,address,tel, zip, area);
422
+
98
-
423
+ }
424
+
425
+
426
+
427
+ return lists;
428
+
429
+ }
430
+
431
+ }
432
+
433
+
434
+
435
+ ```
436
+
99
- import org.springframework.web.bind.annotation.RequestMapping;
437
+ SearchRepositoryCustom.java
438
+
100
-
439
+ ```ここに言語名を入力
440
+
101
- import org.springframework.web.bind.annotation.RequestMethod;
441
+ package com.example.samplelist;
102
-
442
+
443
+
444
+
103
- import org.springframework.ui.Model;
445
+ import java.io.Serializable;
104
-
105
- import org.springframework.validation.BindingResult;
106
-
107
- import org.springframework.validation.annotation.Validated;
108
-
109
- import org.springframework.web.bind.annotation.RequestParam;
110
-
111
- import org.springframework.web.bind.annotation.ModelAttribute;
112
-
113
- import org.springframework.web.servlet.ModelAndView;
114
-
115
-
116
446
 
117
447
  import java.util.List;
118
448
 
119
-
449
+ //DAOclass
450
+
451
+
452
+
120
-
453
+ public interface SearchRepositoryCustom extends Serializable {
454
+
455
+ public static List<User> search(String name,String address,String tel, String zip, String area) {
456
+
457
+ return null;
458
+
459
+ }
460
+
461
+ }
462
+
463
+
464
+
465
+
466
+
467
+ ```
468
+
469
+ SearchRepositoryCustomImp.java
470
+
471
+ ```ここに言語名を入力
472
+
473
+ package com.example.samplelist;
474
+
475
+
476
+
477
+ import java.util.List;
478
+
479
+
480
+
121
- import javax.annotation.PostConstruct;
481
+ import javax.persistence.EntityManager;
122
-
123
-
124
-
125
- @ComponentScan
482
+
126
-
127
- @Controller
128
-
129
- public class DemoController {
130
-
131
- private final UserRepository repos;
483
+ import javax.persistence.Query;
484
+
485
+
486
+
487
+ import org.springframework.beans.factory.annotation.Autowired;
488
+
489
+
490
+
491
+ @SuppressWarnings("serial")
492
+
493
+ public class SearchRepositoryCustomImp implements SearchRepositoryCustom{
132
494
 
133
495
 
134
496
 
135
497
  @Autowired
136
498
 
137
- SearchService service;
138
-
139
-
140
-
141
- @Autowired
142
-
143
- public DemoController(UserRepository repos) {
144
-
145
- this.repos = repos;
146
-
147
- }
148
-
149
-
150
-
151
- /* 一覧画面(初期画面)への遷移 */
152
-
153
- @GetMapping("/")
154
-
155
- ModelAndView getAlllists(@RequestParam(required = false) String name, @RequestParam(required = false) String address,
156
-
157
- @RequestParam(required = false) String tel,
158
-
159
- @RequestParam(required = false) String zip,
160
-
161
- @RequestParam(required = false) String area) {
162
-
163
- ModelAndView mav = new ModelAndView();
164
-
165
- User data = new User();
166
-
167
- mav.addObject("formModel", data);
168
-
169
- mav.addObject("name", name);
170
-
171
- mav.addObject("addrass", address);
172
-
173
- mav.addObject("tel",tel);
174
-
175
- mav.addObject("zip", zip);
176
-
177
- mav.addObject("area", area);
178
-
179
- // COMMENTテーブル:レコード全件取得
180
-
181
- mav.addObject("lists", repos.findAll());
182
-
183
- mav.setViewName("users/list");
184
-
185
- return mav;
186
-
187
- }
188
-
189
-
190
-
191
- /* 新規画面への遷移 */
192
-
193
- @GetMapping("/add")
194
-
195
- ModelAndView add() {
196
-
197
- ModelAndView mav = new ModelAndView();
198
-
199
- User data = new User();
200
-
201
- mav.addObject("formModel", data);
202
-
203
- mav.setViewName("users/new");
204
-
205
- return mav;
206
-
207
- }
208
-
209
-
210
-
211
- /* 編集画面への遷移 */
212
-
213
- @GetMapping("/edit")
214
-
215
- ModelAndView edit(@RequestParam int id) {
216
-
217
- ModelAndView mav = new ModelAndView();
218
-
219
- User data = repos.findById(id);
220
-
221
- mav.addObject("formModel", data);
222
-
223
- mav.setViewName("users/new");
224
-
225
- return mav;
226
-
227
- }
228
-
229
-
230
-
231
- /* 更新処理 */
232
-
233
- @PostMapping("")//ここ修正する
234
-
235
- public String addComment(User user, BindingResult result, Model model) {
236
-
237
- model.addAttribute("views", repos.findAll());
238
-
239
- if (result.hasErrors()) {
240
-
241
- return "users/list";
242
-
243
- }
244
-
245
- // COMMENTテーブル:コメント登録
246
-
247
- repos.save(user);
248
-
249
- // ルートパス("/") にリダイレクトします
250
-
251
- return "redirect:/";
252
-
253
- }
254
-
255
-
256
-
257
- /* 削除処理 */
258
-
259
- @DeleteMapping("/delete")//ここも修正する
260
-
261
- public String deleteComment(@Validated @ModelAttribute User user, BindingResult result, Model model ) {
262
-
263
- model.addAttribute("views", repos.findAll());
264
-
265
- // COMMENTテーブル:レコード削除
266
-
267
- repos.deleteById(user.getId());
268
-
269
- // ルートパス("/") にリダイレクトします
270
-
271
- return "redirect:/deletes";
272
-
273
- }
274
-
275
-
276
-
277
- @RequestMapping(value = "/search", method = RequestMethod.GET)
278
-
279
- public String index() {
280
-
281
- return "users/list";
282
-
283
- }
284
-
285
-
286
-
287
- //検索機能
288
-
289
- @RequestMapping(value = "/search", method = RequestMethod.POST)
290
-
291
- public ModelAndView search(ModelAndView mav,
292
-
293
- @RequestParam(required = false) String name, @RequestParam(required = false) String address,
294
-
295
- @RequestParam(required = false) String tel,
296
-
297
- @RequestParam(required = false) String zip,
298
-
299
- @RequestParam(required = false) String area) {
300
-
301
- mav.setViewName("users/list");
302
-
303
- mav.addObject("name", name);
304
-
305
- List<User> lists = service.search(name, address,tel, zip,area);
306
-
307
- mav.addObject("lists", lists);
308
-
309
- return mav;
310
-
311
- }
312
-
313
-
314
-
315
- @PostConstruct
316
-
317
- public void init() {
318
-
319
- /* 初期データ作成部分 */
320
-
321
- }
499
+ EntityManager manager;
500
+
501
+
502
+
503
+ @SuppressWarnings("unchecked")
504
+
505
+ public List<User> search(String name, String address, String tel, String zip, String area) {
506
+
507
+ StringBuilder sql = new StringBuilder();
508
+
509
+ sql.append("SELECT g From User g WHERE ");
510
+
511
+
512
+
513
+ boolean andflg = false;
514
+
515
+ boolean nameflg = false;
516
+
517
+ boolean addressflg = false;
518
+
519
+ boolean telflg = false;
520
+
521
+ boolean zipflg = false;
522
+
523
+
524
+
525
+ if (!"".equals(name) && name != null) {
526
+
527
+ if (andflg) sql.append(" AND ");
528
+
529
+ sql.append(" g.name LIKE % :name % ");
530
+
531
+ nameflg = true;
532
+
533
+ andflg = true;
534
+
535
+ }
536
+
537
+
538
+
539
+ Query query = manager.createQuery(sql.toString());
540
+
541
+ if (nameflg) query.setParameter("name", "%" + name + "%");
542
+
543
+
544
+
545
+
546
+
547
+ return query.getResultList();
548
+
549
+ }
550
+
551
+
322
552
 
323
553
  }
324
554
 
555
+
556
+
325
557
  ```
326
558
 
327
559
 
328
560
 
561
+
562
+
329
- User.java
563
+ list.html
330
564
 
331
565
  ```ここに言語名を入力
332
566
 
333
- package com.example.samplelist;
334
-
335
-
336
-
337
- import javax.persistence.Column;
338
-
339
- import javax.persistence.Entity;
340
-
341
- import javax.persistence.GeneratedValue;
342
-
343
- import javax.persistence.GenerationType;
344
-
345
- import javax.persistence.Id;
346
-
347
- import javax.persistence.Table;
348
-
349
-
350
-
351
- @Entity
352
-
353
- @Table(name="addresslist")
354
-
355
- public class User {
356
-
357
- @Id
358
-
359
- @GeneratedValue(strategy = GenerationType.AUTO)
360
-
361
- @Column
362
-
363
- private int id;
364
-
365
- @Column(name="name")
366
-
367
- private String name;
368
-
369
- @Column
370
-
371
- private String address;
372
-
373
- @Column
374
-
375
- private String tel;
376
-
377
- @Column
378
-
379
- private String zip;
380
-
381
- @Column
382
-
383
- private String area;
384
-
385
-
386
-
387
- public int getId() {
388
-
389
- return id;
390
-
391
- }
392
-
393
- public void setId(int id) {
394
-
395
- this.id = id;
396
-
397
- }
398
-
399
-
400
-
401
- public String getName() {
402
-
403
- return name;
404
-
405
- }
406
-
407
- public void setName(String name) {
408
-
409
- this.name = name;
410
-
411
- }
412
-
413
-
414
-
415
- public String getAddress() {
416
-
417
- return address;
418
-
419
- }
420
-
421
- public void setAddress(String address) {
422
-
423
- this.address = address;
424
-
425
- }
426
-
427
-
428
-
429
- public String getTel() {
430
-
431
- return tel;
432
-
433
- }
434
-
435
- public void setTel(String tel) {
436
-
437
- this.tel = tel;
438
-
439
- }
440
-
441
-
442
-
443
- public String getZip() {
444
-
445
- return zip;
446
-
447
- }
448
-
449
- public void setZip(String zip) {
450
-
451
- this.zip = zip;
452
-
453
- }
454
-
455
-
456
-
457
- public String getArea() {
458
-
459
- return area;
460
-
461
- }
462
-
463
- public void setArea(String area) {
464
-
465
- this.area = area;
466
-
467
- }
468
-
469
- }
567
+ <!DOCTYPE html>
568
+
569
+ <html xmlns:th="http://www.thymeleaf.org">
570
+
571
+ <head>
572
+
573
+ <style>
574
+
575
+ //省略
576
+
577
+
578
+
579
+ </style>
580
+
581
+ </head>
582
+
583
+ <body>
584
+
585
+ <h1>住所録</h1>
586
+
587
+ <form class="form-group" action="/search" method="post" id="search">
588
+
589
+ <input class="form-control" type="text" name="name" id="name" th:value="${name}" size="30"
590
+
591
+ maxlength= "15" placeholder="search..." />
592
+
593
+ <input type="submit" class="btn btn-outline-secondary" value="検索"/>
594
+
595
+ </form>
596
+
597
+ <div id="tablearea">
598
+
599
+ <table border="1" class="table">
600
+
601
+ <thead class="thead-dark">
602
+
603
+ <tr>
604
+
605
+ <th scope="col" class="col_name">名前</th>
606
+
607
+ <th scope="col" class="col_zip">郵便番号</th>
608
+
609
+ <th scope="col" class="col_address">住所</th>
610
+
611
+ <th scope="col" class="col_tel">電話番号</th>
612
+
613
+ <th scope="col" class="col_area">地方</th>
614
+
615
+ <th class="col_edit"></th>
616
+
617
+ <th class="col_delete"></th>
618
+
619
+ </tr>
620
+
621
+ </thead>
622
+
623
+ <tr th:each=" obj:${lists}" th:object="${obj}">
624
+
625
+ <td th:text="*{name}"></td>
626
+
627
+ <td th:text="*{zip}"></td>
628
+
629
+ <td th:text="*{address}"></td>
630
+
631
+ <td th:text="*{tel}"></td>
632
+
633
+ <td th:text="*{area}"></td>
634
+
635
+ <td>
636
+
637
+ <form action="/edit" method="get">
638
+
639
+ <input type="submit" class="btn btn-outline-secondary" value="編集">
640
+
641
+ <input type="hidden" name="id" th:value="${obj.id}">
642
+
643
+ </form>
644
+
645
+ </td>
646
+
647
+ <td>
648
+
649
+ <form action="/" method="post">
650
+
651
+ <input type="submit" class="btn btn-outline-danger" value="削除" onclick="return confirm('削除してもよろしいですか?')">
652
+
653
+ <input type="hidden" name="id" th:value="${obj.id}">
654
+
655
+ </form>
656
+
657
+ </td>
658
+
659
+ </tr>
660
+
661
+ </table>
662
+
663
+ </div>
664
+
665
+ <hr>
666
+
667
+ <form action="/add">
668
+
669
+ <input type="submit" id="add" class="btn btn-outline-secondary" value="新規追加" />
670
+
671
+ </form>
672
+
673
+ </body>
674
+
675
+ </html>
676
+
677
+
470
678
 
471
679
 
472
680
 
473
681
  ```
474
682
 
475
- UserRepository.java
476
-
477
- ```ここに言語名を入力
478
-
479
- package com.example.samplelist;
480
-
481
-
482
-
483
- import org.springframework.data.jpa.repository.JpaRepository;
484
-
485
-
486
-
487
- public interface UserRepository extends JpaRepository<User, Long>{
488
-
489
- public User findById(int id);
490
-
491
- public void deleteById(int id);
492
-
493
- }
494
-
495
-
496
-
497
- ```
498
-
499
- SearchService.java
500
-
501
- ```ここに言語名を入力
502
-
503
- package com.example.samplelist;
504
-
505
-
506
-
507
- import java.util.List;
508
-
509
-
510
-
511
- import org.springframework.beans.factory.annotation.Autowired;
512
-
513
- import org.springframework.stereotype.Service;
514
-
515
-
516
-
517
- @Service
518
-
519
- public class SearchService {
520
-
521
-
522
-
523
- @Autowired
524
-
525
- UserRepository repos;
526
-
527
-
528
-
529
- SearchRepositoryCustom repositoryCustom;
530
-
531
-
532
-
533
- public List<User> search(String name, String address, String tel, String zip, String area){
534
-
535
- List<User> lists;
536
-
537
- if ("".equals(name) ) {
538
-
539
- lists = repos.findAll();
540
-
541
- } else {
542
-
543
- lists = SearchRepositoryCustom.search(name,address,tel, zip, area);
544
-
545
- }
546
-
547
-
548
-
549
- return lists;
550
-
551
- }
552
-
553
- }
554
-
555
-
556
-
557
- ```
558
-
559
- SearchRepositoryCustom.java
560
-
561
- ```ここに言語名を入力
562
-
563
- package com.example.samplelist;
564
-
565
-
566
-
567
- import java.io.Serializable;
568
-
569
- import java.util.List;
570
-
571
- //DAOclass
572
-
573
-
574
-
575
- public interface SearchRepositoryCustom extends Serializable {
576
-
577
- public static List<User> search(String name,String address,String tel, String zip, String area) {
578
-
579
- return null;
580
-
581
- }
582
-
583
- }
584
-
585
-
586
-
587
-
588
-
589
- ```
590
-
591
- SearchRepositoryCustomImp.java
592
-
593
- ```ここに言語名を入力
594
-
595
- package com.example.samplelist;
596
-
597
-
598
-
599
- import java.util.List;
600
-
601
-
602
-
603
- import javax.persistence.EntityManager;
604
-
605
- import javax.persistence.Query;
606
-
607
-
608
-
609
- import org.springframework.beans.factory.annotation.Autowired;
610
-
611
-
612
-
613
- @SuppressWarnings("serial")
614
-
615
- public class SearchRepositoryCustomImp implements SearchRepositoryCustom{
616
-
617
-
618
-
619
- @Autowired
620
-
621
- EntityManager manager;
622
-
623
-
624
-
625
- @SuppressWarnings("unchecked")
626
-
627
- public List<User> search(String name, String address, String tel, String zip, String area) {
628
-
629
- StringBuilder sql = new StringBuilder();
630
-
631
- sql.append("SELECT g From User g WHERE ");
632
-
633
-
634
-
635
- boolean andflg = false;
636
-
637
- boolean nameflg = false;
638
-
639
- boolean addressflg = false;
640
-
641
- boolean telflg = false;
642
-
643
- boolean zipflg = false;
644
-
645
-
646
-
647
- if (!"".equals(name) && name != null) {
648
-
649
- if (andflg) sql.append(" AND ");
650
-
651
- sql.append(" g.name LIKE % :name % ");
652
-
653
- nameflg = true;
654
-
655
- andflg = true;
656
-
657
- }
658
-
659
-
660
-
661
- Query query = manager.createQuery(sql.toString());
662
-
663
- if (nameflg) query.setParameter("name", "%" + name + "%");
664
-
665
-
666
-
667
-
668
-
669
- return query.getResultList();
670
-
671
- }
672
-
673
-
674
-
675
- }
676
-
677
-
678
-
679
- ```
680
-
681
-
682
-
683
-
684
-
685
- list.html
686
-
687
- ```ここに言語名を入力
688
-
689
- package com.example.samplelist;
690
-
691
-
692
-
693
- import org.springframework.data.jpa.repository.JpaRepository;
694
-
695
-
696
-
697
- public interface UserRepository extends JpaRepository<User, Long>{
698
-
699
- public User findById(int id);
700
-
701
- public void deleteById(int id);
702
-
703
- }
704
-
705
-
706
-
707
- ```
708
-
709
-
710
-
711
- ```
712
-
713
683
 
714
684
 
715
685
  ### 試したこと