Skip to content

Commit 136595c

Browse files
committed
Add a test involving nested record
1 parent e4c56ad commit 136595c

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

src/test/java/org/apache/ibatis/submitted/record_type/CreateDB.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ create table prop (
2323
);
2424

2525
insert into prop (id, val, url) values (1, 'Val1', 'https://www.google.com');
26+
27+
create table item (
28+
id int,
29+
prop_id int
30+
);
31+
32+
insert into item (id, prop_id) values (100, 1);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright 2009-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.apache.ibatis.submitted.record_type;
18+
19+
public record Item(Integer id, Property property) {
20+
}

src/test/java/org/apache/ibatis/submitted/record_type/RecordTypeMapper.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
import org.apache.ibatis.annotations.Arg;
1919
import org.apache.ibatis.annotations.Insert;
20+
import org.apache.ibatis.annotations.Results;
2021
import org.apache.ibatis.annotations.Select;
2122

2223
public interface RecordTypeMapper {
2324

2425
@Select("select id, val, url from prop where id = #{id}")
2526
Property selectPropertyAutomapping(int id);
2627

28+
@Results(id = "propertyRM")
2729
@Arg(column = "id", javaType = int.class)
2830
@Arg(column = "val", javaType = String.class)
2931
@Arg(column = "url", javaType = String.class)
@@ -33,4 +35,12 @@ public interface RecordTypeMapper {
3335
@Insert("insert into prop (id, val, url) values (#{id}, #{value}, #{URL})")
3436
int insertProperty(Property property);
3537

38+
@Arg(id = true, column = "id", javaType = Integer.class)
39+
@Arg(javaType = Property.class, resultMap = "propertyRM", columnPrefix = "p_")
40+
@Select({
41+
"select i.id, p.id p_id, p.val p_val, p.url p_url",
42+
"from item i left join prop p on p.id = i.prop_id",
43+
"where i.id = #{id}" })
44+
Item selectItem(Integer id);
45+
3646
}

src/test/java/org/apache/ibatis/submitted/record_type/RecordTypeTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,13 @@ void testInsertRecord() {
7777
}
7878
}
7979

80+
@Test
81+
void testSelectNestedRecord() {
82+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
83+
RecordTypeMapper mapper = sqlSession.getMapper(RecordTypeMapper.class);
84+
Item item = mapper.selectItem(100);
85+
assertEquals(Integer.valueOf(100), item.id());
86+
assertEquals(new Property(1, "Val1", "https://www.google.com"), item.property());
87+
}
88+
}
8089
}

0 commit comments

Comments
 (0)