質問編集履歴
4
失礼します。問題点及び進展があったので追記させて頂きました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -30,7 +30,29 @@
|
|
30
30
|
|
31
31
|
|
32
32
|
|
33
|
-
|
33
|
+
-------------------
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
【10/5更新】
|
38
|
+
|
39
|
+
進展がありましたのでソースコード更新させて頂きました。
|
40
|
+
|
41
|
+
更新ソースコード
|
42
|
+
|
43
|
+
・SearchRepositoryCustom.java
|
44
|
+
|
45
|
+
・DemoController.java
|
46
|
+
|
47
|
+
・SearchService.java
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
SearchRepositoryCustom.javaまでは思い通りの動きははいってそうです。
|
52
|
+
|
53
|
+
しかし、未入力の場合は一覧表示はできるようになったものの、検索する文字を入力した際の場合はリスト表示されず
|
54
|
+
|
55
|
+
(未入力で検索ボタンを押した場合はSELECT処理が入るが、文字入力してボタンを押した場合はSELECT処理が入ってない。SQLがそもそも通ってないせい?)
|
34
56
|
|
35
57
|
|
36
58
|
|
@@ -84,6 +106,8 @@
|
|
84
106
|
|
85
107
|
import org.springframework.beans.factory.annotation.Autowired;
|
86
108
|
|
109
|
+
import org.springframework.context.annotation.ComponentScan;
|
110
|
+
|
87
111
|
import org.springframework.stereotype.Controller;
|
88
112
|
|
89
113
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
@@ -118,15 +142,19 @@
|
|
118
142
|
|
119
143
|
|
120
144
|
|
145
|
+
@ComponentScan
|
146
|
+
|
121
147
|
@Controller
|
122
148
|
|
123
149
|
public class DemoController {
|
124
150
|
|
125
151
|
private final UserRepository repos;
|
126
152
|
|
153
|
+
|
154
|
+
|
127
155
|
@Autowired
|
128
156
|
|
129
|
-
|
157
|
+
SearchService service;
|
130
158
|
|
131
159
|
|
132
160
|
|
@@ -144,7 +172,13 @@
|
|
144
172
|
|
145
173
|
@GetMapping("/")
|
146
174
|
|
147
|
-
ModelAndView getAlllists(
|
175
|
+
ModelAndView getAlllists(@RequestParam(required = false) String name, @RequestParam(required = false) String address,
|
176
|
+
|
177
|
+
@RequestParam(required = false) String tel,
|
178
|
+
|
179
|
+
@RequestParam(required = false) String zip,
|
180
|
+
|
181
|
+
@RequestParam(required = false) String area) {
|
148
182
|
|
149
183
|
ModelAndView mav = new ModelAndView();
|
150
184
|
|
@@ -260,17 +294,35 @@
|
|
260
294
|
|
261
295
|
|
262
296
|
|
297
|
+
@RequestMapping(value = "/search", method = RequestMethod.GET)
|
298
|
+
|
299
|
+
public String index() {
|
300
|
+
|
301
|
+
return "users/list";
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
//検索機能
|
308
|
+
|
263
|
-
@RequestMapping(value = "search", method = RequestMethod.POST)
|
309
|
+
@RequestMapping(value = "/search", method = RequestMethod.POST)
|
264
310
|
|
265
311
|
public ModelAndView search(ModelAndView mav,
|
266
312
|
|
267
|
-
|
313
|
+
@RequestParam(required = false) String name, @RequestParam(required = false) String address,
|
314
|
+
|
268
|
-
|
315
|
+
@RequestParam(required = false) String tel,
|
316
|
+
|
317
|
+
@RequestParam(required = false) String zip,
|
318
|
+
|
319
|
+
@RequestParam(required = false) String area) {
|
320
|
+
|
269
|
-
mav.setViewName("se
|
321
|
+
mav.setViewName("users/list");
|
270
322
|
|
271
323
|
mav.addObject("name", name);
|
272
324
|
|
273
|
-
List<User> lists =
|
325
|
+
List<User> lists = service.search(name, address,tel, zip,area);
|
274
326
|
|
275
327
|
mav.addObject("lists", lists);
|
276
328
|
|
@@ -280,13 +332,11 @@
|
|
280
332
|
|
281
333
|
|
282
334
|
|
283
|
-
/* 初期データ作成 */
|
284
|
-
|
285
335
|
@PostConstruct
|
286
336
|
|
287
337
|
public void init() {
|
288
338
|
|
289
|
-
|
339
|
+
/* 初期データ作成部分 */
|
290
340
|
|
291
341
|
}
|
292
342
|
|
@@ -480,49 +530,195 @@
|
|
480
530
|
|
481
531
|
import org.springframework.beans.factory.annotation.Autowired;
|
482
532
|
|
533
|
+
import org.springframework.stereotype.Service;
|
534
|
+
|
535
|
+
|
536
|
+
|
537
|
+
@Service
|
538
|
+
|
539
|
+
public class SearchService {
|
540
|
+
|
541
|
+
|
542
|
+
|
543
|
+
@Autowired
|
544
|
+
|
545
|
+
UserRepository repos;
|
546
|
+
|
547
|
+
|
548
|
+
|
549
|
+
SearchRepositoryCustom repositoryCustom;
|
550
|
+
|
551
|
+
|
552
|
+
|
553
|
+
public List<User> search(String name, String address, String tel, String zip, String area){
|
554
|
+
|
555
|
+
List<User> lists;
|
556
|
+
|
557
|
+
if ("".equals(name) ) {
|
558
|
+
|
559
|
+
lists = repos.findAll();
|
560
|
+
|
561
|
+
} else {
|
562
|
+
|
563
|
+
lists = SearchRepositoryCustom.search(name,address,tel, zip, area);
|
564
|
+
|
565
|
+
}
|
566
|
+
|
567
|
+
|
568
|
+
|
569
|
+
return lists;
|
570
|
+
|
571
|
+
}
|
572
|
+
|
573
|
+
}
|
574
|
+
|
575
|
+
|
576
|
+
|
577
|
+
```
|
578
|
+
|
579
|
+
SearchRepositoryCustom.java
|
580
|
+
|
581
|
+
```ここに言語名を入力
|
582
|
+
|
583
|
+
package com.example.samplelist;
|
584
|
+
|
585
|
+
|
586
|
+
|
587
|
+
import java.io.Serializable;
|
588
|
+
|
589
|
+
import java.util.List;
|
590
|
+
|
591
|
+
//DAOclass
|
592
|
+
|
593
|
+
|
594
|
+
|
595
|
+
public interface SearchRepositoryCustom extends Serializable {
|
596
|
+
|
597
|
+
public static List<User> search(String name,String address,String tel, String zip, String area) {
|
598
|
+
|
599
|
+
return null;
|
600
|
+
|
601
|
+
}
|
602
|
+
|
603
|
+
}
|
604
|
+
|
605
|
+
|
606
|
+
|
607
|
+
|
608
|
+
|
609
|
+
```
|
610
|
+
|
611
|
+
SearchRepositoryCustomImp.java
|
612
|
+
|
613
|
+
```ここに言語名を入力
|
614
|
+
|
615
|
+
package com.example.samplelist;
|
616
|
+
|
617
|
+
|
618
|
+
|
619
|
+
import java.util.List;
|
620
|
+
|
621
|
+
|
622
|
+
|
623
|
+
import javax.persistence.EntityManager;
|
624
|
+
|
625
|
+
import javax.persistence.Query;
|
626
|
+
|
627
|
+
|
628
|
+
|
629
|
+
import org.springframework.beans.factory.annotation.Autowired;
|
630
|
+
|
631
|
+
|
632
|
+
|
633
|
+
@SuppressWarnings("serial")
|
634
|
+
|
635
|
+
public class SearchRepositoryCustomImp implements SearchRepositoryCustom{
|
636
|
+
|
637
|
+
|
638
|
+
|
639
|
+
@Autowired
|
640
|
+
|
641
|
+
EntityManager manager;
|
642
|
+
|
643
|
+
|
644
|
+
|
645
|
+
@SuppressWarnings("unchecked")
|
646
|
+
|
647
|
+
public List<User> search(String name, String address, String tel, String zip, String area) {
|
648
|
+
|
649
|
+
StringBuilder sql = new StringBuilder();
|
650
|
+
|
651
|
+
sql.append("SELECT g From User g WHERE ");
|
652
|
+
|
653
|
+
|
654
|
+
|
655
|
+
boolean andflg = false;
|
656
|
+
|
657
|
+
boolean nameflg = false;
|
658
|
+
|
659
|
+
boolean addressflg = false;
|
660
|
+
|
661
|
+
boolean telflg = false;
|
662
|
+
|
663
|
+
boolean zipflg = false;
|
664
|
+
|
665
|
+
|
666
|
+
|
667
|
+
if (!"".equals(name) && name != null) {
|
668
|
+
|
669
|
+
if (andflg) sql.append(" AND ");
|
670
|
+
|
671
|
+
sql.append(" g.name LIKE % :name % ");
|
672
|
+
|
673
|
+
nameflg = true;
|
674
|
+
|
675
|
+
andflg = true;
|
676
|
+
|
677
|
+
}
|
678
|
+
|
679
|
+
|
680
|
+
|
681
|
+
Query query = manager.createQuery(sql.toString());
|
682
|
+
|
683
|
+
if (nameflg) query.setParameter("name", "%" + name + "%");
|
684
|
+
|
685
|
+
|
686
|
+
|
687
|
+
|
688
|
+
|
689
|
+
return query.getResultList();
|
690
|
+
|
691
|
+
}
|
692
|
+
|
693
|
+
|
694
|
+
|
695
|
+
}
|
696
|
+
|
697
|
+
|
698
|
+
|
699
|
+
```
|
700
|
+
|
701
|
+
|
702
|
+
|
703
|
+
|
704
|
+
|
705
|
+
list.html
|
706
|
+
|
707
|
+
```ここに言語名を入力
|
708
|
+
|
709
|
+
package com.example.samplelist;
|
710
|
+
|
711
|
+
|
712
|
+
|
483
713
|
import org.springframework.data.jpa.repository.JpaRepository;
|
484
714
|
|
715
|
+
|
716
|
+
|
485
|
-
i
|
717
|
+
public interface UserRepository extends JpaRepository<User, Long>{
|
486
|
-
|
487
|
-
|
488
|
-
|
718
|
+
|
489
|
-
|
719
|
+
public User findById(int id);
|
490
|
-
|
720
|
+
|
491
|
-
public
|
721
|
+
public void deleteById(int id);
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
@Autowired
|
496
|
-
|
497
|
-
static
|
498
|
-
|
499
|
-
UserRepository repository;
|
500
|
-
|
501
|
-
@Autowired
|
502
|
-
|
503
|
-
static
|
504
|
-
|
505
|
-
SearchRepositoryCustom repositoryCustom;
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
public static List<User> search(String name, String address, String tel, String zip, String area){
|
510
|
-
|
511
|
-
List<User> lists;
|
512
|
-
|
513
|
-
if ("".equals(name) && "".equals(address) && tel == null && zip == null) {
|
514
|
-
|
515
|
-
lists = repository.findAll();
|
516
|
-
|
517
|
-
} else {
|
518
|
-
|
519
|
-
lists = repositoryCustom.search(name, address, tel, zip, area);
|
520
|
-
|
521
|
-
}
|
522
|
-
|
523
|
-
return lists;
|
524
|
-
|
525
|
-
}
|
526
722
|
|
527
723
|
}
|
528
724
|
|
@@ -530,152 +726,6 @@
|
|
530
726
|
|
531
727
|
```
|
532
728
|
|
533
|
-
SearchRepositoryCustom.java
|
534
|
-
|
535
|
-
```ここに言語名を入力
|
536
|
-
|
537
|
-
package com.example.samplelist;
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
import java.io.Serializable;
|
542
|
-
|
543
|
-
import java.util.List;
|
544
|
-
|
545
|
-
//DAOclass
|
546
|
-
|
547
|
-
public interface SearchRepositoryCustom extends Serializable {
|
548
|
-
|
549
|
-
public List<User> search(String name,String address,String tel, String zip, String area);
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
}
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
```
|
558
|
-
|
559
|
-
SearchRepositoryCustomImp.java
|
560
|
-
|
561
|
-
```ここに言語名を入力
|
562
|
-
|
563
|
-
package com.example.samplelist;
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
import java.util.List;
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
import javax.persistence.EntityManager;
|
572
|
-
|
573
|
-
import javax.persistence.Query;
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
import org.springframework.beans.factory.annotation.Autowired;
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
@SuppressWarnings("serial")
|
582
|
-
|
583
|
-
public class SearchRepositoryCustomImp implements SearchRepositoryCustom{
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
@Autowired
|
588
|
-
|
589
|
-
EntityManager manager;
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
@SuppressWarnings("unchecked")
|
594
|
-
|
595
|
-
@Override
|
596
|
-
|
597
|
-
public List<User> search(String name, String address, String tel, String zip, String area) {
|
598
|
-
|
599
|
-
StringBuilder sql = new StringBuilder();
|
600
|
-
|
601
|
-
sql.append("SELECT g From User g WHERE ");
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
boolean andflg = false;
|
606
|
-
|
607
|
-
boolean nameflg = false;
|
608
|
-
|
609
|
-
boolean addressflg = false;
|
610
|
-
|
611
|
-
boolean telflg = false;
|
612
|
-
|
613
|
-
boolean zipflg = false;
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
if (!"".equals(name) && name != null) {
|
618
|
-
|
619
|
-
if (andflg) sql.append(" AND ");
|
620
|
-
|
621
|
-
sql.append(" g.name LIKE % :name % ");
|
622
|
-
|
623
|
-
nameflg = true;
|
624
|
-
|
625
|
-
andflg = true;
|
626
|
-
|
627
|
-
}
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
Query query = manager.createQuery(sql.toString());
|
632
|
-
|
633
|
-
if (nameflg) query.setParameter("name", "%" + name + "%");
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
return query.getResultList();
|
640
|
-
|
641
|
-
}
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
}
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
```
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
list.html
|
656
|
-
|
657
|
-
```ここに言語名を入力
|
658
|
-
|
659
|
-
package com.example.samplelist;
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
import org.springframework.data.jpa.repository.JpaRepository;
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
public interface UserRepository extends JpaRepository<User, Long>{
|
668
|
-
|
669
|
-
public User findById(int id);
|
670
|
-
|
671
|
-
public void deleteById(int id);
|
672
|
-
|
673
|
-
}
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
```
|
678
|
-
|
679
729
|
|
680
730
|
|
681
731
|
```
|
3
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
|
11
11
|
|
12
12
|
|
13
|
-
### 問題点
|
13
|
+
### 問題点。
|
14
14
|
|
15
15
|
|
16
16
|
|
2
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,6 +8,20 @@
|
|
8
8
|
|
9
9
|
とりあえず簡易的でよいのでなんとか機能実装までに行きつきたいと考えております。
|
10
10
|
|
11
|
+
|
12
|
+
|
13
|
+
### 問題点
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
list.htmlの検索欄に名前を入力すると、リスト欄にその名前と一致するもののみを表示出来るようにしたいです。
|
18
|
+
|
19
|
+
controllerクラスか、SearchRepositoryCustomImp.javaの記載に問題があるのか…という予想なのですが、そもそも上手く通っているのか、
|
20
|
+
|
21
|
+
エラーコードも出てないため、どこが原因か分からない状態です。
|
22
|
+
|
23
|
+
|
24
|
+
|
11
25
|
ご助力いただけますと幸いです。よろしくお願いします。
|
12
26
|
|
13
27
|
|
1
失礼しました。CRUD機能のうちの検索機能以外は実装済み に修正致しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,7 +2,9 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
+
簡易的なCRUD機能のうちの検索機能以外は実装済み。
|
6
|
+
|
5
|
-
|
7
|
+
検索機能を追加したい(とりあえず名前での検索のみ)
|
6
8
|
|
7
9
|
とりあえず簡易的でよいのでなんとか機能実装までに行きつきたいと考えております。
|
8
10
|
|