Skip to content

Commit 25a41f8

Browse files
committed
Added columnPrefix to <idArg /> and <arg />.
1 parent f32c624 commit 25a41f8

File tree

10 files changed

+382
-7
lines changed

10 files changed

+382
-7
lines changed

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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
assertEquals(2, articles.size());
54+
Article article1 = articles.get(0);
55+
assertEquals(Integer.valueOf(1), article1.getId().getId());
56+
assertEquals("Article 1", article1.getName());
57+
assertEquals("Mary", article1.getAuthor().getName());
58+
assertEquals("Bob", article1.getCoauthor().getName());
59+
Article article2 = articles.get(1);
60+
assertEquals(Integer.valueOf(2), article2.getId().getId());
61+
assertEquals("Article 2", article2.getName());
62+
assertEquals("Jane", article2.getAuthor().getName());
63+
assertEquals("Mary", article2.getCoauthor().getName());
64+
}
65+
}
66+
67+
}
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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
public interface Mapper {
21+
22+
List<Article> getArticles();
23+
24+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2009-2018 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+
23+
<mapper
24+
namespace="org.apache.ibatis.submitted.constructor_columnprefix.Mapper">
25+
26+
<resultMap id="keyRM"
27+
type="org.apache.ibatis.submitted.constructor_columnprefix.EntityKey">
28+
<id property="id" column="id" />
29+
</resultMap>
30+
31+
<resultMap id="authorRM"
32+
type="org.apache.ibatis.submitted.constructor_columnprefix.Author">
33+
<id property="id" column="id" />
34+
<result property="name" column="name" />
35+
</resultMap>
36+
37+
<resultMap id="articleRM"
38+
type="org.apache.ibatis.submitted.constructor_columnprefix.Article">
39+
<constructor>
40+
<idArg resultMap="keyRM" columnPrefix="key_"
41+
javaType="org.apache.ibatis.submitted.constructor_columnprefix.EntityKey" />
42+
<arg column="name" javaType="string" />
43+
<arg resultMap="authorRM" columnPrefix="author_"
44+
javaType="org.apache.ibatis.submitted.constructor_columnprefix.Author" />
45+
<arg resultMap="authorRM" columnPrefix="coauthor_"
46+
javaType="org.apache.ibatis.submitted.constructor_columnprefix.Author" />
47+
</constructor>
48+
</resultMap>
49+
50+
<select id="getArticles" resultMap="articleRM"><![CDATA[
51+
select id key_id, name, author.id author_id, author.name author_name,
52+
coauthor.id coauthor_id, coauthor.name coauthor_name
53+
from articles
54+
left join authors author on author.id = articles.author_id
55+
left join authors coauthor on coauthor.id = articles.coauthor_id
56+
order by articles.id
57+
]]></select>
58+
59+
</mapper>

0 commit comments

Comments
 (0)