teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

6

説明分の編集

2019/11/06 16:04

投稿

icezoom
icezoom

スコア36

title CHANGED
File without changes
body CHANGED
@@ -1,18 +1,13 @@
1
1
  自習用で作っているものです。
2
2
  リポジトリクラスで、結合したテーブルから取得した結果を最終的にTyhmeleafに渡して画面に表示したいです。
3
3
 
4
- telData,telgroupという二つのテーブルを用意し、リポジトリクラスでgroupIdで結合しております。
5
- その後のservice、controllerの書き方、
6
- 最終的なTyhmeleafの書き方のヒント等を頂ければ嬉しいです。
7
- 3日ほど試行錯誤しておりますが、うまく実装できない状態です。
8
-
9
4
  ```
10
5
  @Data
11
6
  @Entity
12
7
  public class User {
13
8
 
14
9
  @Id
15
- @GeneratedValue //主キーのIDを採番する役割
10
+ @GeneratedValue
16
11
  private Long id;
17
12
 
18
13
  private String userName;
@@ -37,12 +32,12 @@
37
32
  上記のテーブルをrepositoryで外部結合
38
33
  ```Java
39
34
  @Repository
40
- public interface TelDataRepository extends JpaRepository<User, Long> {
35
+ public interface UserRepository extends JpaRepository<User, Long> {
41
36
 
42
37
  @Query(value = "SELECT d.id,d.user_name,_no,d.mail_addr,g.group_name,null AS group_id "
43
38
  + "FROM user u "
44
39
  + "LEFT JOIN group g ON d.group_id = g.group_id",nativeQuery = true)
45
- public List<User> findUserGroup();
40
+ public List<User> findUser();
46
41
  }
47
42
 
48
43
  ```

5

説明分の編集

2019/11/06 16:04

投稿

icezoom
icezoom

スコア36

title CHANGED
File without changes
body CHANGED
@@ -9,7 +9,7 @@
9
9
  ```
10
10
  @Data
11
11
  @Entity
12
- public class TelData {
12
+ public class User {
13
13
 
14
14
  @Id
15
15
  @GeneratedValue //主キーのIDを採番する役割
@@ -17,8 +17,6 @@
17
17
 
18
18
  private String userName;
19
19
 
20
- private String telNo;
21
-
22
20
  private String mailAddr;
23
21
 
24
22
  private Long groupId;
@@ -28,7 +26,7 @@
28
26
 
29
27
  @Data
30
28
  @Entity
31
- public class TelGroup {
29
+ public class UserGroup {
32
30
 
33
31
  @Id
34
32
  private Long groupId;
@@ -39,18 +37,18 @@
39
37
  上記のテーブルをrepositoryで外部結合
40
38
  ```Java
41
39
  @Repository
42
- public interface TelDataRepository extends JpaRepository<TelData, Long> {
40
+ public interface TelDataRepository extends JpaRepository<User, Long> {
43
41
 
44
- @Query(value = "SELECT d.id,d.user_name,d.tel_no,d.mail_addr,g.group_name,null AS group_id "
42
+ @Query(value = "SELECT d.id,d.user_name,_no,d.mail_addr,g.group_name,null AS group_id "
45
- + "FROM tel_data d "
43
+ + "FROM user u "
46
- + "LEFT JOIN tel_group g ON d.group_id = g.group_id",nativeQuery = true)
44
+ + "LEFT JOIN group g ON d.group_id = g.group_id",nativeQuery = true)
47
- public List<TelData> findTelDataGroup();
45
+ public List<User> findUserGroup();
48
46
  }
49
47
 
50
48
  ```
51
49
  ```Java(service)
52
- public List<TelData> findTelDataGroup() {
50
+ public List<User> findUserGroup() {
53
- return telDataRepository.findTelDataGroup();
51
+ return UserRepository.findUserGroup();
54
52
  }
55
53
 
56
54
  ```
@@ -59,60 +57,10 @@
59
57
  @Controller
60
58
  @RequestMapping("/")
61
59
  public class IndexController {
62
-
63
- /** 一覧画面のサービスクラスのフィールド変数 */
64
60
  @Autowired
65
61
  private IndexService service;
66
-
67
- /**
68
- * 一覧画面表示メソッド
69
- *
70
- * @param model Model
71
- * @return 一覧画面
72
- */
73
62
  @RequestMapping(method = RequestMethod.GET)
74
63
  public String index(Model model) {
75
- // 一覧画面用サービス.電話帳データリスト検索処理メソッドを呼び出し、modelに「telDataList」という名前を持つ属性を追加し、
76
- // 値に電話帳データリスト検索処理メソッドの戻り値を設定する。
77
- // modelは、フレームワーク提供の画面とJava間でデータをやりとりするためのオブジェクト
78
- model.addAttribute("telDataList", service.findTelDataList());
79
- model.addAttribute("telGroupList",service.findTelGroupList());
64
+ model.addAttribute("UserGroup",service.findUserGroup());
80
- model.addAttribute("telDataGroupList",service.findTelDataGroup());
81
- // 一覧画面(index.html)を返却
82
65
  return "index";
83
- }
66
+ }
84
- ```
85
- 下記でhtmlに表示しようとしていますが、エラーがでます。。
86
- <td th:text="${telGroup.groupName}"></td> の部分です。
87
- エラー内容:EL1007E: Property or field 'groupName' cannot be found on null
88
- どのように指定すればいいのかお聞きしたいです。
89
-
90
- ```html
91
- <form th:action="@{delete}" method="post" th:object="${telDataForm}">
92
- <table class="table table-bordered table-hover" id="table">
93
- <thead>
94
- <tr>
95
- <th class="text-center">選択</th>
96
- <th class="text-center">氏名</th>
97
- <th class="text-center">電話番号</th>
98
- <th class="text-center">メールアドレス</th>
99
- <th class="text-center">グループ名</th>
100
- <th class="text-center">編集</th>
101
- </tr>
102
- </thead>
103
- <tbody>
104
- <tr th:each="telDataGroup : ${telDataGroupList}" >
105
- <td class="text-center"><input type="checkbox"
106
- name="selectRecords" th:value="${telDataGroup.id}"></td>
107
- <td th:text="${telDataGroup.userName}"></td>
108
- <td th:text="${telDataGroup.telNo}"></td>
109
- <td th:text="${telDataGroup.mailAddr}"></td>
110
- <td th:text="${telDataGroup.groupName}"></td>
111
- <td class="text-center"><a class="btn btn-secondary btn-sm"
112
-      th:href="@{'/edit?id=' + ${telDataGroup.id}}">編集</a></td>
113
- </tr>
114
- </tbody>
115
- </table>
116
- <input type="submit" class="btn btn-primary btn-sm" value="削除">
117
- </form>
118
- ```

4

説明文の修正

2019/11/06 16:02

投稿

icezoom
icezoom

スコア36

title CHANGED
File without changes
body CHANGED
@@ -77,13 +77,14 @@
77
77
  // modelは、フレームワーク提供の画面とJava間でデータをやりとりするためのオブジェクト
78
78
  model.addAttribute("telDataList", service.findTelDataList());
79
79
  model.addAttribute("telGroupList",service.findTelGroupList());
80
- **model.addAttribute("telDataList",service.findTelDataGroup());**
80
+ model.addAttribute("telDataGroupList",service.findTelDataGroup());
81
81
  // 一覧画面(index.html)を返却
82
82
  return "index";
83
83
  }
84
84
  ```
85
85
  下記でhtmlに表示しようとしていますが、エラーがでます。。
86
86
  <td th:text="${telGroup.groupName}"></td> の部分です。
87
+ エラー内容:EL1007E: Property or field 'groupName' cannot be found on null
87
88
  どのように指定すればいいのかお聞きしたいです。
88
89
 
89
90
  ```html
@@ -100,15 +101,15 @@
100
101
  </tr>
101
102
  </thead>
102
103
  <tbody>
103
- <tr th:each="telData : ${telDataList}" >
104
+ <tr th:each="telDataGroup : ${telDataGroupList}" >
104
105
  <td class="text-center"><input type="checkbox"
105
- name="selectRecords" th:value="${telData.id}"></td>
106
+ name="selectRecords" th:value="${telDataGroup.id}"></td>
106
- <td th:text="${telData.userName}"></td>
107
+ <td th:text="${telDataGroup.userName}"></td>
107
- <td th:text="${telData.telNo}"></td>
108
+ <td th:text="${telDataGroup.telNo}"></td>
108
- <td th:text="${telData.mailAddr}"></td>
109
+ <td th:text="${telDataGroup.mailAddr}"></td>
109
- <td th:text="${telGroup.groupName}"></td>
110
+ <td th:text="${telDataGroup.groupName}"></td>
110
111
  <td class="text-center"><a class="btn btn-secondary btn-sm"
111
- th:href="@{'/edit?id=' + ${telData.id}}">編集</a></td>
112
+      th:href="@{'/edit?id=' + ${telDataGroup.id}}">編集</a></td>
112
113
  </tr>
113
114
  </tbody>
114
115
  </table>

3

説明分の編集

2019/09/22 06:01

投稿

icezoom
icezoom

スコア36

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,11 @@
1
+ 自習用で作っているものです。
1
2
  リポジトリクラスで、結合したテーブルから取得した結果を最終的にTyhmeleafに渡して画面に表示したいです。
2
- Tyhmeleafでの<th:text="${telGroup.groupName}">のエラーをどのように対処すれば良いかわかりません。
3
3
 
4
+ telData,telgroupという二つのテーブルを用意し、リポジトリクラスでgroupIdで結合しております。
5
+ その後のservice、controllerの書き方、
6
+ 最終的なTyhmeleafの書き方のヒント等を頂ければ嬉しいです。
7
+ 3日ほど試行錯誤しておりますが、うまく実装できない状態です。
8
+
4
9
  ```
5
10
  @Data
6
11
  @Entity

2

説明分の編集

2019/09/22 05:16

投稿

icezoom
icezoom

スコア36

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,5 @@
1
- テーブルとして、二つのentityクラスを持っていてそのデータを外部結合し、htmlで表示させたいです。
1
+ リポジトリクラス、結合したテーブルから取得した結果を最終的にTyhmeleafに渡して画面に表示たいです。
2
- 外部結合後取得、表示で困っていま
2
+ Tyhmeleafで<th:text="${telGroup.groupName}">のエラをどように対処すれば良かわかりせん
3
3
 
4
4
  ```
5
5
  @Data

1

タグの編集

2019/09/22 05:07

投稿

icezoom
icezoom

スコア36

title CHANGED
File without changes
body CHANGED
File without changes