Skip to content

Commit f31526d

Browse files
authored
Merge pull request #1318 from harawata/constructor-columnprefix
Allow specifying `columnPrefix` in constructor mapping.
2 parents f32c624 + 058d6d1 commit f31526d

File tree

12 files changed

+422
-9
lines changed

12 files changed

+422
-9
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 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.
@@ -46,4 +46,9 @@
4646
String resultMap() default "";
4747

4848
String name() default "";
49+
50+
/**
51+
* @since 3.5.0
52+
*/
53+
String columnPrefix() default "";
4954
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ private void applyConstructorArgs(Arg[] args, Class<?> resultType, List<ResultMa
609609
nullOrEmpty(arg.select()),
610610
nullOrEmpty(arg.resultMap()),
611611
null,
612-
null,
612+
nullOrEmpty(arg.columnPrefix()),
613613
typeHandler,
614614
flags,
615615
null,

src/main/java/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<!--
33
4-
Copyright 2009-2017 the original author or authors.
4+
Copyright 2009-2018 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -90,6 +90,7 @@ typeHandler CDATA #IMPLIED
9090
select CDATA #IMPLIED
9191
resultMap CDATA #IMPLIED
9292
name CDATA #IMPLIED
93+
columnPrefix CDATA #IMPLIED
9394
>
9495

9596
<!ELEMENT arg EMPTY>
@@ -101,6 +102,7 @@ typeHandler CDATA #IMPLIED
101102
select CDATA #IMPLIED
102103
resultMap CDATA #IMPLIED
103104
name CDATA #IMPLIED
105+
columnPrefix CDATA #IMPLIED
104106
>
105107

106108
<!ELEMENT collection (constructor?,id*,result*,association*,collection*, discriminator?)>

src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap r
350350
skipRows(rsw.getResultSet(), rowBounds);
351351
while (shouldProcessMoreRows(resultContext, rowBounds) && rsw.getResultSet().next()) {
352352
ResultMap discriminatedResultMap = resolveDiscriminatedResultMap(rsw.getResultSet(), resultMap, null);
353-
Object rowValue = getRowValue(rsw, discriminatedResultMap);
353+
Object rowValue = getRowValue(rsw, discriminatedResultMap, null);
354354
storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
355355
}
356356
}
@@ -389,16 +389,16 @@ private void skipRows(ResultSet rs, RowBounds rowBounds) throws SQLException {
389389
// GET VALUE FROM ROW FOR SIMPLE RESULT MAP
390390
//
391391

392-
private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap) throws SQLException {
392+
private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, String columnPrefix) throws SQLException {
393393
final ResultLoaderMap lazyLoader = new ResultLoaderMap();
394-
Object rowValue = createResultObject(rsw, resultMap, lazyLoader, null);
394+
Object rowValue = createResultObject(rsw, resultMap, lazyLoader, columnPrefix);
395395
if (rowValue != null && !hasTypeHandlerForResultObject(rsw, resultMap.getType())) {
396396
final MetaObject metaObject = configuration.newMetaObject(rowValue);
397397
boolean foundValues = this.useConstructorMappings;
398398
if (shouldApplyAutomaticMappings(resultMap, false)) {
399-
foundValues = applyAutomaticMappings(rsw, resultMap, metaObject, null) || foundValues;
399+
foundValues = applyAutomaticMappings(rsw, resultMap, metaObject, columnPrefix) || foundValues;
400400
}
401-
foundValues = applyPropertyMappings(rsw, resultMap, metaObject, lazyLoader, null) || foundValues;
401+
foundValues = applyPropertyMappings(rsw, resultMap, metaObject, lazyLoader, columnPrefix) || foundValues;
402402
foundValues = lazyLoader.size() > 0 || foundValues;
403403
rowValue = foundValues || configuration.isReturnInstanceForEmptyRow() ? rowValue : null;
404404
}
@@ -629,7 +629,7 @@ Object createParameterizedResultObject(ResultSetWrapper rsw, Class<?> resultType
629629
value = getNestedQueryConstructorValue(rsw.getResultSet(), constructorMapping, columnPrefix);
630630
} else if (constructorMapping.getNestedResultMapId() != null) {
631631
final ResultMap resultMap = configuration.getResultMap(constructorMapping.getNestedResultMapId());
632-
value = getRowValue(rsw, resultMap);
632+
value = getRowValue(rsw, resultMap, constructorMapping.getColumnPrefix());
633633
} else {
634634
final TypeHandler<?> typeHandler = constructorMapping.getTypeHandler();
635635
value = typeHandler.getResult(rsw.getResultSet(), prependPrefix(column, columnPrefix));
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2009-2018 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.constructor_columnprefix;
17+
18+
public class Article {
19+
20+
private EntityKey id;
21+
22+
private String name;
23+
24+
private Author author;
25+
26+
private Author coauthor;
27+
28+
public Article(EntityKey id, String name, Author author, Author coauthor) {
29+
super();
30+
this.id = id;
31+
this.name = name;
32+
this.author = author;
33+
this.coauthor = coauthor;
34+
}
35+
36+
public EntityKey getId() {
37+
return id;
38+
}
39+
40+
public String getName() {
41+
return name;
42+
}
43+
44+
public Author getAuthor() {
45+
return author;
46+
}
47+
48+
public Author getCoauthor() {
49+
return coauthor;
50+
}
51+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2009-2018 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.constructor_columnprefix;
17+
18+
public class Author {
19+
private Integer id;
20+
21+
private String name;
22+
23+
public Integer getId() {
24+
return id;
25+
}
26+
27+
public void setId(Integer id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Copyright 2009-2018 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.constructor_columnprefix;
17+
18+
import static org.junit.Assert.*;
19+
20+
import java.io.Reader;
21+
import java.util.List;
22+
23+
import org.apache.ibatis.BaseDataTest;
24+
import org.apache.ibatis.io.Resources;
25+
import org.apache.ibatis.session.SqlSession;
26+
import org.apache.ibatis.session.SqlSessionFactory;
27+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28+
import org.junit.BeforeClass;
29+
import org.junit.Test;
30+
31+
public class ConstructorColumnPrefixTest {
32+
33+
private static SqlSessionFactory sqlSessionFactory;
34+
35+
@BeforeClass
36+
public static void setUp() throws Exception {
37+
// create an SqlSessionFactory
38+
try (Reader reader = Resources
39+
.getResourceAsReader("org/apache/ibatis/submitted/constructor_columnprefix/mybatis-config.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/constructor_columnprefix/CreateDB.sql");
46+
}
47+
48+
@Test
49+
public void shouldGetArticles() {
50+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
51+
Mapper mapper = sqlSession.getMapper(Mapper.class);
52+
List<Article> articles = mapper.getArticles();
53+
assertArticles(articles);
54+
}
55+
}
56+
57+
@Test
58+
public void shouldGetArticlesAnno() {
59+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
60+
Mapper mapper = sqlSession.getMapper(Mapper.class);
61+
List<Article> articles = mapper.getArticlesAnno();
62+
assertArticles(articles);
63+
}
64+
}
65+
66+
protected void assertArticles(List<Article> articles) {
67+
assertEquals(2, articles.size());
68+
Article article1 = articles.get(0);
69+
assertEquals(Integer.valueOf(1), article1.getId().getId());
70+
assertEquals("Article 1", article1.getName());
71+
assertEquals("Mary", article1.getAuthor().getName());
72+
assertEquals("Bob", article1.getCoauthor().getName());
73+
Article article2 = articles.get(1);
74+
assertEquals(Integer.valueOf(2), article2.getId().getId());
75+
assertEquals("Article 2", article2.getName());
76+
assertEquals("Jane", article2.getAuthor().getName());
77+
assertEquals("Mary", article2.getCoauthor().getName());
78+
}
79+
80+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--
2+
-- Copyright 2009-2018 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+
drop table articles if exists;
18+
drop table authors if exists;
19+
20+
create table articles (
21+
id int,
22+
name varchar(20),
23+
author_id int,
24+
coauthor_id int
25+
);
26+
27+
create table authors (
28+
id int,
29+
name varchar(20)
30+
);
31+
32+
insert into articles (id, name, author_id, coauthor_id) values
33+
(1, 'Article 1', 1, 2),
34+
(2, 'Article 2', 3, 1);
35+
36+
insert into authors (id, name) values
37+
(1, 'Mary'), (2, 'Bob'), (3, 'Jane');
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright 2009-2018 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.constructor_columnprefix;
17+
18+
public class EntityKey {
19+
private Integer id;
20+
21+
public Integer getId() {
22+
return id;
23+
}
24+
25+
public void setId(Integer id) {
26+
this.id = id;
27+
}
28+
29+
@Override
30+
public int hashCode() {
31+
final int prime = 31;
32+
int result = 1;
33+
result = prime * result + ((id == null) ? 0 : id.hashCode());
34+
return result;
35+
}
36+
37+
@Override
38+
public boolean equals(Object obj) {
39+
if (this == obj)
40+
return true;
41+
if (obj == null)
42+
return false;
43+
if (getClass() != obj.getClass())
44+
return false;
45+
EntityKey other = (EntityKey) obj;
46+
if (id == null) {
47+
if (other.id != null)
48+
return false;
49+
} else if (!id.equals(other.id))
50+
return false;
51+
return true;
52+
}
53+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright 2009-2018 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.constructor_columnprefix;
17+
18+
import java.util.List;
19+
20+
import org.apache.ibatis.annotations.Arg;
21+
import org.apache.ibatis.annotations.ConstructorArgs;
22+
import org.apache.ibatis.annotations.Select;
23+
24+
public interface Mapper {
25+
26+
List<Article> getArticles();
27+
28+
@ConstructorArgs({
29+
@Arg(id = true, resultMap = "keyRM", columnPrefix = "key_", javaType = EntityKey.class),
30+
@Arg(column = "name", javaType = String.class),
31+
@Arg(resultMap = "authorRM", columnPrefix = "author_", javaType = Author.class),
32+
@Arg(resultMap = "authorRM", columnPrefix = "coauthor_", javaType = Author.class),
33+
})
34+
@Select({
35+
"select id key_id, name, author.id author_id, author.name author_name,",
36+
" coauthor.id coauthor_id, coauthor.name coauthor_name",
37+
"from articles",
38+
"left join authors author on author.id = articles.author_id",
39+
"left join authors coauthor on coauthor.id = articles.coauthor_id",
40+
"order by articles.id"
41+
})
42+
List<Article> getArticlesAnno();
43+
44+
}

0 commit comments

Comments
 (0)