Skip to content

Commit ea8182c

Browse files
authored
Merge pull request #1829 from moonService/dev/@one_@many_add_columnprefix
Add columnPrefix to @one and @many
2 parents 454aece + 494dccf commit ea8182c

File tree

11 files changed

+528
-5
lines changed

11 files changed

+528
-5
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2019 the original author or authors.
2+
* Copyright 2009-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,13 @@
3333
@Retention(RetentionPolicy.RUNTIME)
3434
@Target({})
3535
public @interface Many {
36+
/**
37+
* Returns the columnPrefix.
38+
*
39+
* @return the columnPrefix.
40+
*/
41+
String columnPrefix() default "";
42+
3643
/**
3744
* Returns the result map id used to map collection.
3845
*

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2019 the original author or authors.
2+
* Copyright 2009-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,13 @@
3333
@Retention(RetentionPolicy.RUNTIME)
3434
@Target({})
3535
public @interface One {
36+
/**
37+
* Returns the columnPrefix.
38+
*
39+
* @return the columnPrefix.
40+
*/
41+
String columnPrefix() default "";
42+
3643
/**
3744
* Returns the result map id used to map single object.
3845
*

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2019 the original author or authors.
2+
* Copyright 2009-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -545,16 +545,17 @@ private void applyResults(Result[] results, Class<?> resultType, List<ResultMapp
545545
@SuppressWarnings("unchecked")
546546
Class<? extends TypeHandler<?>> typeHandler = (Class<? extends TypeHandler<?>>)
547547
((result.typeHandler() == UnknownTypeHandler.class) ? null : result.typeHandler());
548+
boolean hasNestedResultMap = hasNestedResultMap(result);
548549
ResultMapping resultMapping = assistant.buildResultMapping(
549550
resultType,
550551
nullOrEmpty(result.property()),
551552
nullOrEmpty(result.column()),
552553
result.javaType() == void.class ? null : result.javaType(),
553554
result.jdbcType() == JdbcType.UNDEFINED ? null : result.jdbcType(),
554555
hasNestedSelect(result) ? nestedSelectId(result) : null,
555-
hasNestedResultMap(result) ? nestedResultMapId(result) : null,
556-
null,
556+
hasNestedResultMap ? nestedResultMapId(result) : null,
557557
null,
558+
hasNestedResultMap ? findColumnPrefix(result) : null,
558559
typeHandler,
559560
flags,
560561
null,
@@ -564,6 +565,14 @@ private void applyResults(Result[] results, Class<?> resultType, List<ResultMapp
564565
}
565566
}
566567

568+
private String findColumnPrefix(Result result) {
569+
String columnPrefix = result.one().columnPrefix();
570+
if (columnPrefix.length() < 1) {
571+
columnPrefix = result.many().columnPrefix();
572+
}
573+
return columnPrefix;
574+
}
575+
567576
private String nestedResultMapId(Result result) {
568577
String resultMapId = result.one().resultMap();
569578
if (resultMapId.length() < 1) {
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
--
2+
-- Copyright 2009-2020 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+
name varchar(30)
23+
);
24+
25+
-- ----------------------------
26+
-- Records of role
27+
-- ----------------------------
28+
INSERT INTO role (id,name)
29+
VALUES ('1', 'teacher');
30+
INSERT INTO role (id,name)
31+
VALUES ('2', 'student');
32+
INSERT INTO role (id,name)
33+
VALUES ('3', 'Headmaster');
34+
INSERT INTO role (id,name)
35+
VALUES ('4', 'Learning-commissary');
36+
37+
CREATE TABLE user (
38+
id int,
39+
username varchar(32),
40+
friend_id int
41+
);
42+
43+
-- ----------------------------
44+
-- Records of user
45+
-- ----------------------------
46+
INSERT INTO user (id,username)
47+
VALUES ('1', 'James Gosling');
48+
INSERT INTO user (id,username)
49+
VALUES ('2', 'Doug Lea');
50+
INSERT INTO user (id,username)
51+
VALUES ('3', 'Rod johnson');
52+
INSERT INTO user (id,username, friend_id)
53+
VALUES ('4', 'Juergen Hoeller', 1);
54+
55+
-- ----------------------------
56+
-- Table structure for `user_role`
57+
-- ----------------------------
58+
CREATE TABLE user_role (
59+
id int,
60+
role_id int,
61+
user_id int
62+
);
63+
64+
-- ----------------------------
65+
-- Records of user_role
66+
-- ----------------------------
67+
INSERT INTO user_role (id,role_id,user_id)
68+
VALUES ('1', '2', '4');
69+
INSERT INTO user_role (id,role_id,user_id)
70+
VALUES ('2', '3', '1');
71+
INSERT INTO user_role (id,role_id,user_id)
72+
VALUES ('3', '1', '2');
73+
INSERT INTO user_role (id,role_id,user_id)
74+
VALUES ('4', '2', '3');
75+
INSERT INTO user_role (id,role_id,user_id)
76+
VALUES ('5', '4', '4');
77+
INSERT INTO user_role (id,role_id,user_id)
78+
VALUES ('6', '1', '1');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* Copyright 2009-2020 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_columnprefix;
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 OneManyColumnPrefixTest {
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_columnprefix/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_columnprefix/CreateDB.sql");
46+
}
47+
48+
@Test
49+
void shouldUseColumnPrefixWithMany() {
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 shouldUseColumnPrefixInXmlWithMany() {
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 shouldUseColumnPrefixWithOne() {
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().getName());
79+
}
80+
}
81+
82+
@Test
83+
void shouldResolveNestedColumnPrefix() {
84+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
85+
UserDao mapper = sqlSession.getMapper(UserDao.class);
86+
User user = mapper.findUserWithFriend(4);
87+
assertEquals(4, user.getId());
88+
assertEquals(2, user.getRoles().size());
89+
assertEquals("student", user.getRoles().get(0).getName());
90+
assertEquals("Learning-commissary", user.getRoles().get(1).getName());
91+
assertEquals(1, user.getFriend().getId());
92+
assertEquals(2, user.getFriend().getRoles().size());
93+
assertEquals("teacher", user.getFriend().getRoles().get(0).getName());
94+
assertEquals("Headmaster", user.getFriend().getRoles().get(1).getName());
95+
}
96+
}
97+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright 2009-2020 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_columnprefix;
17+
18+
public class Role {
19+
private Integer id;
20+
21+
@Override
22+
public String toString() {
23+
return "Role{" +
24+
"id=" + id +
25+
", roleName='" + name + '\'' +
26+
'}';
27+
}
28+
29+
private String name;
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 getName() {
40+
return name;
41+
}
42+
43+
public void setName(String name) {
44+
this.name = name;
45+
}
46+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright 2009-2020 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_columnprefix;
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 = "id", property = "id"),
31+
@Result(column = "name", property = "name")
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-2020 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_columnprefix.RoleDao">
24+
<resultMap id="roleMap2"
25+
type="org.apache.ibatis.submitted.annotion_many_one_add_columnprefix.Role">
26+
<id column="id" property="id" />
27+
<result column="name" property="name" />
28+
</resultMap>
29+
</mapper>

0 commit comments

Comments
 (0)