質問編集履歴

5

修正

2020/09/28 13:08

投稿

box_908
box_908

スコア5

test CHANGED
File without changes
test CHANGED
@@ -10,8 +10,58 @@
10
10
 
11
11
 
12
12
 
13
+
14
+
13
15
  #ソース
14
16
 
17
+
18
+
19
+ ```schema
20
+
21
+ DROP TABLE IF EXISTS detail CASCADE;
22
+
23
+ DROP TABLE IF EXISTS player CASCADE;
24
+
25
+
26
+
27
+
28
+
29
+ CREATE TABLE player
30
+
31
+ (
32
+
33
+ id character NOT NULL ,
34
+
35
+ name character varying(256),
36
+
37
+ age character varying(256),
38
+
39
+ detail_id character varying(256),
40
+
41
+ CONSTRAINT pkey PRIMARY KEY (id)
42
+
43
+ );
44
+
45
+
46
+
47
+
48
+
49
+ CREATE TABLE detail
50
+
51
+ (
52
+
53
+ detail_id character NOT NULL ,
54
+
55
+ work character varying(256),
56
+
57
+ CONSTRAINT ppkey PRIMARY KEY (detail_id)
58
+
59
+ );
60
+
61
+ ```
62
+
63
+
64
+
15
65
  ```MyBatisMapper
16
66
 
17
67
  <select id="selectItem" resultMap="PlayerDetailMap">

4

修正

2020/09/28 13:08

投稿

box_908
box_908

スコア5

test CHANGED
File without changes
test CHANGED
@@ -21,8 +21,6 @@
21
21
  p.id
22
22
 
23
23
  ,p.name
24
-
25
- ,p.detail_id
26
24
 
27
25
  ,d.work
28
26
 
@@ -54,11 +52,7 @@
54
52
 
55
53
  <result property="name" column="name" />
56
54
 
57
- <result property="detail_id" column="detail_id" />
58
-
59
55
  <collection property="detailEntity" ofType="com.mybatis.test.domain.DetailEntity">
60
-
61
- <result property="detail_id" column="detail_id" />
62
56
 
63
57
  <result property="work" column="work" />
64
58
 
@@ -93,6 +87,8 @@
93
87
  private String age;
94
88
 
95
89
  private String detail_id;
90
+
91
+ @Transient
96
92
 
97
93
  private List<DetailEntity> detailEntity;
98
94
 
@@ -134,7 +130,29 @@
134
130
 
135
131
  ```
136
132
 
133
+ ```index
137
134
 
135
+ <div th:object="${playerDetailEntity}">
136
+
137
+ <div>
138
+
139
+ <label><b>名前</b></label>
140
+
141
+ <p th:text="*{name}">
142
+
143
+ </div>
144
+
145
+ <div>
146
+
147
+ <label><b>仕事</b></label>
148
+
149
+ <p th:text="*{detailEntity.work}">
150
+
151
+ </div>
152
+
153
+ </div>
154
+
155
+ ```
138
156
 
139
157
 
140
158
 

3

修正

2020/09/28 12:29

投稿

box_908
box_908

スコア5

test CHANGED
File without changes
test CHANGED
@@ -21,6 +21,8 @@
21
21
  p.id
22
22
 
23
23
  ,p.name
24
+
25
+ ,p.detail_id
24
26
 
25
27
  ,d.work
26
28
 

2

修正

2020/09/28 11:48

投稿

box_908
box_908

スコア5

test CHANGED
File without changes
test CHANGED
@@ -14,9 +14,9 @@
14
14
 
15
15
  ```MyBatisMapper
16
16
 
17
- <select id="selectItem" parameterType="string" resultMap="PlayerDetailMap">
17
+ <select id="selectItem" resultMap="PlayerDetailMap">
18
18
 
19
- select
19
+ SELECT
20
20
 
21
21
  p.id
22
22
 
@@ -24,19 +24,19 @@
24
24
 
25
25
  ,d.work
26
26
 
27
- from
27
+ FROM
28
28
 
29
29
  player p
30
30
 
31
- inner join
31
+ INNER JOIN
32
32
 
33
33
  detail d
34
34
 
35
- on
35
+ ON
36
36
 
37
37
  p.detail_id=d.detail_id
38
38
 
39
- where
39
+ WHERE
40
40
 
41
41
  p.id=#{id}
42
42
 
@@ -46,7 +46,7 @@
46
46
 
47
47
  <!-- テーブルのSELECT結果をどのようにマッピングするか指定 -->
48
48
 
49
- <resultMap id="PlayerDetailMap" type="com.mybatis.test.domain.PlayerDetailEntity">
49
+ <resultMap id="PlayerDetailMap" type="com.mybatis.test.domain.PlayerEntity">
50
50
 
51
51
  <id property="id" column="id" />
52
52
 
@@ -54,7 +54,7 @@
54
54
 
55
55
  <result property="detail_id" column="detail_id" />
56
56
 
57
- <collection property="detail" ofType="com.mybatis.test.domain.DetailEntity">
57
+ <collection property="detailEntity" ofType="com.mybatis.test.domain.DetailEntity">
58
58
 
59
59
  <result property="detail_id" column="detail_id" />
60
60
 
@@ -65,34 +65,6 @@
65
65
  </resultMap>
66
66
 
67
67
  ```
68
-
69
-
70
-
71
- 上記SQLでSELECTした結果を格納するためのエンティティクラスを作成。
72
-
73
-
74
-
75
- ```PlayerDetailEntity
76
-
77
- @Entity
78
-
79
- public class PlayerDetailEntity {
80
-
81
-
82
-
83
- @Id
84
-
85
- private String id;
86
-
87
- private String name;
88
-
89
- private String work;
90
-
91
- ゲッター、セッター割愛
92
-
93
- ```
94
-
95
-
96
68
 
97
69
 
98
70
 
@@ -119,6 +91,8 @@
119
91
  private String age;
120
92
 
121
93
  private String detail_id;
94
+
95
+ private List<DetailEntity> detailEntity;
122
96
 
123
97
  ```
124
98
 

1

修正

2020/09/28 11:34

投稿

box_908
box_908

スコア5

test CHANGED
File without changes
test CHANGED
@@ -142,6 +142,24 @@
142
142
 
143
143
 
144
144
 
145
+ ```Controller
146
+
147
+ @GetMapping()
148
+
149
+ public String index(Model model) {
150
+
151
+ PlayerDetailEntity playerDetailEntity = new PlayerDetailEntity();
152
+
153
+ playerDetailEntity = myBatisService.selectItem("001");
154
+
155
+ model.addAttribute("playerDetailEntity", playerDetailEntity);
156
+
157
+ return "index";
158
+
159
+ ```
160
+
161
+
162
+
145
163
 
146
164
 
147
165
  MyBatisMapper.xmlの記述が問題だと思われるのですが、