Skip to content

Commit f81a3f5

Browse files
committed
Take the column prefix into account
1 parent 66508f0 commit f81a3f5

File tree

8 files changed

+130
-8
lines changed

8 files changed

+130
-8
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2021 the original author or authors.
2+
* Copyright 2009-2022 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.
@@ -774,8 +774,7 @@ private boolean applyArgNameBasedConstructorAutoMapping(ResultSetWrapper rsw, Re
774774
Param paramAnno = param.getAnnotation(Param.class);
775775
String paramName = paramAnno == null ? param.getName() : paramAnno.value();
776776
for (String columnName : rsw.getColumnNames()) {
777-
if (paramName.equalsIgnoreCase(
778-
configuration.isMapUnderscoreToCamelCase() ? columnName.replace("_", "") : columnName)) {
777+
if (columnMatchesParam(columnName, paramName, columnPrefix)) {
779778
Class<?> paramType = param.getType();
780779
TypeHandler<?> typeHandler = rsw.getTypeHandler(paramType, columnName);
781780
Object value = typeHandler.getResult(rsw.getResultSet(), columnName);
@@ -805,6 +804,17 @@ private boolean applyArgNameBasedConstructorAutoMapping(ResultSetWrapper rsw, Re
805804
return foundValues;
806805
}
807806

807+
private boolean columnMatchesParam(String columnName, String paramName, String columnPrefix) {
808+
if (columnPrefix != null) {
809+
if (!columnName.toUpperCase(Locale.ENGLISH).startsWith(columnPrefix)) {
810+
return false;
811+
}
812+
columnName = columnName.substring(columnPrefix.length());
813+
}
814+
return paramName
815+
.equalsIgnoreCase(configuration.isMapUnderscoreToCamelCase() ? columnName.replace("_", "") : columnName);
816+
}
817+
808818
private Object createPrimitiveResultObject(ResultSetWrapper rsw, ResultMap resultMap, String columnPrefix) throws SQLException {
809819
final Class<?> resultType = resultMap.getType();
810820
final String columnName;

src/test/java/org/apache/ibatis/submitted/arg_name_baesd_constructor_automapping/ArgNameBasedConstructorAutoMappingTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2021 the original author or authors.
2+
* Copyright 2009-2022 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.
@@ -123,4 +123,16 @@ void shouldRespectMapUnderscoreToCamelCaseSetting() {
123123
assertEquals("User1", user.getName());
124124
}
125125
}
126+
127+
@Test
128+
void shouldApplyColumnPrefix() {
129+
sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
130+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
131+
Mapper mapper = sqlSession.getMapper(Mapper.class);
132+
Task task = mapper.selectTask(11);
133+
assertEquals(Integer.valueOf(1), task.getAssignee().getId());
134+
assertEquals("User1!", task.getAssignee().getName());
135+
assertEquals(99, task.getAssignee().getTeam());
136+
}
137+
}
126138
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--
2-
-- Copyright 2009-2021 the original author or authors.
2+
-- Copyright 2009-2022 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.
@@ -24,3 +24,14 @@ create table users (
2424

2525
insert into users (id, name, team) values
2626
(1, 'User1', 99);
27+
28+
drop table tasks if exists;
29+
30+
create table tasks (
31+
id int,
32+
name varchar(20),
33+
assignee_id int
34+
);
35+
36+
insert into tasks (id, name, assignee_id) values
37+
(11, 'Task1', 1);

src/test/java/org/apache/ibatis/submitted/arg_name_baesd_constructor_automapping/Mapper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2021 the original author or authors.
2+
* Copyright 2009-2022 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,4 +33,6 @@ public interface Mapper {
3333

3434
@Select("select name user_name, id user_id from users where id = #{id}")
3535
User2 selectUserIdAndUserNameUnderscore(Integer id);
36+
37+
Task selectTask(Integer id);
3638
}
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-2022 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 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
20+
<mapper
21+
namespace="org.apache.ibatis.submitted.arg_name_baesd_constructor_automapping.Mapper">
22+
23+
<resultMap
24+
type="org.apache.ibatis.submitted.arg_name_baesd_constructor_automapping.Task"
25+
id="taskRM">
26+
<association property="assignee" resultMap="userRM"
27+
columnPrefix="u_" />
28+
</resultMap>
29+
30+
<resultMap autoMapping="true"
31+
type="org.apache.ibatis.submitted.arg_name_baesd_constructor_automapping.User"
32+
id="userRM">
33+
</resultMap>
34+
35+
<select id="selectTask" resultMap="taskRM"><![CDATA[
36+
select t.id, t.name, u.name u_name, u.team u_team, u.id u_id
37+
from tasks t left join users u on u.id = t.assignee_id
38+
where t.id = #{id}
39+
]]></select>
40+
41+
</mapper>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
package org.apache.ibatis.submitted.arg_name_baesd_constructor_automapping;
17+
18+
import org.apache.ibatis.annotations.Param;
19+
20+
public class Task {
21+
private final Integer id;
22+
private final String name;
23+
private User assignee;
24+
25+
public Task(@Param("id") Integer id, @Param("name") String name) {
26+
super();
27+
this.id = id;
28+
this.name = name;
29+
}
30+
31+
public Integer getId() {
32+
return id;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public User getAssignee() {
40+
return assignee;
41+
}
42+
43+
public void setAssignee(User assignee) {
44+
this.assignee = assignee;
45+
}
46+
}

src/test/java/org/apache/ibatis/submitted/arg_name_baesd_constructor_automapping/User.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2021 the original author or authors.
2+
* Copyright 2009-2022 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.

src/test/java/org/apache/ibatis/submitted/arg_name_baesd_constructor_automapping/User2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2021 the original author or authors.
2+
* Copyright 2009-2022 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.

0 commit comments

Comments
 (0)