Skip to content

Commit c7c810a

Browse files
authored
Merge pull request #1771 from moonService/dev/add_resultmapId_to_annotion_many_one
Allow referencing result map from @one and @many
2 parents c915abc + dd8d87e commit c7c810a

File tree

11 files changed

+511
-1
lines changed

11 files changed

+511
-1
lines changed

src/main/java/org/apache/ibatis/annotations/Many.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
@Retention(RetentionPolicy.RUNTIME)
3434
@Target({})
3535
public @interface Many {
36+
/**
37+
* Returns the result map id used to map collection.
38+
*
39+
* @return the result map id
40+
*/
41+
String resultMap() default "";
42+
3643
/**
3744
* Returns the statement id that retrieves collection.
3845
*

src/main/java/org/apache/ibatis/annotations/One.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
@Retention(RetentionPolicy.RUNTIME)
3434
@Target({})
3535
public @interface One {
36+
/**
37+
* Returns the result map id used to map single object.
38+
*
39+
* @return the result map id
40+
*/
41+
String resultMap() default "";
42+
3643
/**
3744
* Returns the statement id that retrieves single object.
3845
*

src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ private void applyResults(Result[] results, Class<?> resultType, List<ResultMapp
552552
result.javaType() == void.class ? null : result.javaType(),
553553
result.jdbcType() == JdbcType.UNDEFINED ? null : result.jdbcType(),
554554
hasNestedSelect(result) ? nestedSelectId(result) : null,
555-
null,
555+
hasNestedResultMap(result) ? nestedResultMapId(result) : null,
556556
null,
557557
null,
558558
typeHandler,
@@ -564,6 +564,24 @@ private void applyResults(Result[] results, Class<?> resultType, List<ResultMapp
564564
}
565565
}
566566

567+
private String nestedResultMapId(Result result) {
568+
String resultMapId = result.one().resultMap();
569+
if (resultMapId.length() < 1) {
570+
resultMapId = result.many().resultMap();
571+
}
572+
if (!resultMapId.contains(".")) {
573+
resultMapId = type.getName() + "." + resultMapId;
574+
}
575+
return resultMapId;
576+
}
577+
578+
private boolean hasNestedResultMap(Result result) {
579+
if (result.one().resultMap().length() > 0 && result.many().resultMap().length() > 0) {
580+
throw new BuilderException("Cannot use both @One and @Many annotations in the same @Result");
581+
}
582+
return result.one().resultMap().length() > 0 || result.many().resultMap().length() > 0;
583+
}
584+
567585
private String nestedSelectId(Result result) {
568586
String nestedSelect = result.one().select();
569587
if (nestedSelect.length() < 1) {
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
--
2+
-- Copyright 2009-2019 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+
-- ----------------------------
18+
-- Table structure for role
19+
-- ----------------------------
20+
CREATE TABLE role (
21+
id int,
22+
role_name varchar(10)
23+
);
24+
25+
-- ----------------------------
26+
-- Records of role
27+
-- ----------------------------
28+
INSERT INTO role (id,role_name)
29+
VALUES ('1', 'teacher');
30+
INSERT INTO role (id,role_name)
31+
VALUES ('2', 'student');
32+
INSERT INTO role (id,role_name)
33+
VALUES ('3', 'Headmaster');
34+
INSERT INTO role (id,role_name)
35+
VALUES ('4', 'Learning-commissary');
36+
37+
CREATE TABLE user (
38+
id int,
39+
username varchar(32),
40+
);
41+
42+
-- ----------------------------
43+
-- Records of user
44+
-- ----------------------------
45+
INSERT INTO user (id,username)
46+
VALUES ('1', 'James Gosling');
47+
INSERT INTO user (id,username)
48+
VALUES ('2', 'Doug Lea');
49+
INSERT INTO user (id,username)
50+
VALUES ('3', 'Rod johnson');
51+
INSERT INTO user (id,username)
52+
VALUES ('4', 'Juergen Hoeller');
53+
54+
-- ----------------------------
55+
-- Table structure for `user_role`
56+
-- ----------------------------
57+
CREATE TABLE user_role (
58+
id int,
59+
role_id int,
60+
user_id int
61+
);
62+
63+
-- ----------------------------
64+
-- Records of user_role
65+
-- ----------------------------
66+
INSERT INTO user_role (id,role_id,user_id)
67+
VALUES ('1', '2', '4');
68+
INSERT INTO user_role (id,role_id,user_id)
69+
VALUES ('2', '3', '1');
70+
INSERT INTO user_role (id,role_id,user_id)
71+
VALUES ('3', '1', '2');
72+
INSERT INTO user_role (id,role_id,user_id)
73+
VALUES ('4', '2', '3');
74+
INSERT INTO user_role (id,role_id,user_id)
75+
VALUES ('5', '4', '4');
76+
INSERT INTO user_role (id,role_id,user_id)
77+
VALUES ('6', '1', '1');
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* Copyright 2009-2019 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+
package org.apache.ibatis.submitted.annotion_many_one_add_resultmapid;
17+
18+
import org.apache.ibatis.BaseDataTest;
19+
import org.apache.ibatis.io.Resources;
20+
import org.apache.ibatis.session.SqlSession;
21+
import org.apache.ibatis.session.SqlSessionFactory;
22+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
23+
import org.junit.jupiter.api.BeforeAll;
24+
import org.junit.jupiter.api.Test;
25+
26+
import static org.junit.jupiter.api.Assertions.*;
27+
28+
import java.io.Reader;
29+
import java.util.List;
30+
31+
class OneManyResultMapTest {
32+
33+
private static SqlSessionFactory sqlSessionFactory;
34+
35+
@BeforeAll
36+
static void setUp() throws Exception {
37+
// create an SqlSessionFactory
38+
try (Reader reader = Resources
39+
.getResourceAsReader("org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/SqlMapConfig.xml")) {
40+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
41+
}
42+
43+
// populate in-memory database
44+
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
45+
"org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/CreateDB.sql");
46+
}
47+
48+
@Test
49+
void shouldUseResultMapWithMany() {
50+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
51+
UserDao mapper = sqlSession.getMapper(UserDao.class);
52+
List<User> users = mapper.findAll();
53+
assertNotNull(users);
54+
assertEquals(4, users.size());
55+
assertEquals(2, users.get(0).getRoles().size());
56+
}
57+
}
58+
59+
@Test
60+
void shouldUseResultMapInXmlWithMany() {
61+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
62+
UserDao mapper = sqlSession.getMapper(UserDao.class);
63+
List<User> users = mapper.findAll2();
64+
assertNotNull(users);
65+
assertEquals(4, users.size());
66+
assertEquals(2, users.get(0).getRoles().size());
67+
}
68+
}
69+
70+
@Test
71+
void shouldUseResultMapWithOne() {
72+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
73+
UserDao mapper = sqlSession.getMapper(UserDao.class);
74+
List<User> users = mapper.findAll3();
75+
assertNotNull(users);
76+
assertEquals(2, users.size());
77+
assertNotNull(users.get(0).getRole());
78+
assertEquals("teacher", users.get(0).getRole().getRoleName());
79+
}
80+
}
81+
82+
@Test
83+
void shouldResolveResultMapInTheSameNamespace() {
84+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
85+
UserDao mapper = sqlSession.getMapper(UserDao.class);
86+
User headmaster = mapper.findHeadmaster();
87+
assertNotNull(headmaster);
88+
assertEquals(3, headmaster.getTeachers().size());
89+
assertEquals("Doug Lea", headmaster.getTeachers().get(0).getUsername());
90+
}
91+
}
92+
93+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright 2009-2019 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+
package org.apache.ibatis.submitted.annotion_many_one_add_resultmapid;
17+
18+
public class Role {
19+
private Integer id;
20+
21+
@Override
22+
public String toString() {
23+
return "Role{" +
24+
"id=" + id +
25+
", roleName='" + roleName + '\'' +
26+
'}';
27+
}
28+
29+
private String roleName;
30+
31+
public Integer getId() {
32+
return id;
33+
}
34+
35+
public void setId(Integer id) {
36+
this.id = id;
37+
}
38+
39+
public String getRoleName() {
40+
return roleName;
41+
}
42+
43+
public void setRoleName(String roleName) {
44+
this.roleName = roleName;
45+
}
46+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright 2009-2019 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+
package org.apache.ibatis.submitted.annotion_many_one_add_resultmapid;
17+
18+
import org.apache.ibatis.annotations.Result;
19+
import org.apache.ibatis.annotations.Results;
20+
import org.apache.ibatis.annotations.Select;
21+
22+
import java.util.List;
23+
24+
/**
25+
* @author lvyang
26+
*/
27+
public interface RoleDao {
28+
@Select("select * from role")
29+
@Results(id = "roleMap1", value = {
30+
@Result(id = true, column = "role_id", property = "id"),
31+
@Result(column = "role_name", property = "roleName")
32+
})
33+
public List<Role> findAll();
34+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2009-2019 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE mapper
20+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
21+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
22+
<mapper
23+
namespace="org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao">
24+
<resultMap id="roleMap2"
25+
type="org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.Role">
26+
<id column="role_id" property="id" />
27+
<result column="role_name" property="roleName" />
28+
</resultMap>
29+
</mapper>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright 2009-2019 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE configuration
20+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
21+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
22+
<configuration>
23+
<environments default="development">
24+
<environment id="development">
25+
<transactionManager type="JDBC" />
26+
<dataSource type="UNPOOLED">
27+
<property name="driver" value="org.hsqldb.jdbcDriver" />
28+
<property name="url"
29+
value="jdbc:hsqldb:mem:annotion_many_one_add_resultmapid" />
30+
<property name="username" value="sa" />
31+
</dataSource>
32+
</environment>
33+
</environments>
34+
35+
<mappers>
36+
<mapper
37+
class="org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao" />
38+
<mapper
39+
class="org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.UserDao" />
40+
</mappers>
41+
</configuration>

0 commit comments

Comments
 (0)