Skip to content

Commit 7b430d3

Browse files
committed
fixes mybatis#1089 Never apply type handler on ParamMap.
1 parent f0a3b3d commit 7b430d3

File tree

8 files changed

+338
-0
lines changed

8 files changed

+338
-0
lines changed

src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Set;
3333
import java.util.concurrent.ConcurrentHashMap;
3434

35+
import org.apache.ibatis.binding.MapperMethod.ParamMap;
3536
import org.apache.ibatis.io.ResolverUtil;
3637
import org.apache.ibatis.io.Resources;
3738
import org.apache.ibatis.reflection.Jdk;
@@ -195,6 +196,9 @@ public <T> TypeHandler<T> getTypeHandler(TypeReference<T> javaTypeReference, Jdb
195196

196197
@SuppressWarnings("unchecked")
197198
private <T> TypeHandler<T> getTypeHandler(Type type, JdbcType jdbcType) {
199+
if (ParamMap.class.equals(type)) {
200+
return null;
201+
}
198202
Map<JdbcType, TypeHandler<?>> jdbcHandlerMap = getJdbcHandlerMap(type);
199203
TypeHandler<?> handler = null;
200204
if (jdbcHandlerMap != null) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--
2+
-- Copyright 2009-2017 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 users if exists;
18+
19+
create table users (
20+
id int,
21+
name varchar(20)
22+
);
23+
24+
insert into users (id, name) values(1, 'User1');
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright 2009-2017 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+
package org.apache.ibatis.submitted.hashmaptypehandler;
18+
19+
import java.sql.CallableStatement;
20+
import java.sql.PreparedStatement;
21+
import java.sql.ResultSet;
22+
import java.sql.SQLException;
23+
import java.util.HashMap;
24+
25+
import org.apache.ibatis.type.BaseTypeHandler;
26+
import org.apache.ibatis.type.JdbcType;
27+
28+
public class HashMapTypeHandler extends BaseTypeHandler<HashMap<String, String>> {
29+
30+
@Override
31+
public void setNonNullParameter(PreparedStatement ps, int i, HashMap<String, String> parameter, JdbcType jdbcType)
32+
throws SQLException {
33+
ps.setString(i, parameter.get("name"));
34+
}
35+
36+
@Override
37+
public HashMap<String, String> getNullableResult(ResultSet rs, String columnName) throws SQLException {
38+
// TODO Auto-generated method stub
39+
return null;
40+
}
41+
42+
@Override
43+
public HashMap<String, String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
44+
// TODO Auto-generated method stub
45+
return null;
46+
}
47+
48+
@Override
49+
public HashMap<String, String> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
50+
// TODO Auto-generated method stub
51+
return null;
52+
}
53+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* Copyright 2009-2017 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.hashmaptypehandler;
17+
18+
import java.io.Reader;
19+
import java.sql.Connection;
20+
import java.util.HashMap;
21+
22+
import org.apache.ibatis.io.Resources;
23+
import org.apache.ibatis.jdbc.ScriptRunner;
24+
import org.apache.ibatis.session.SqlSession;
25+
import org.apache.ibatis.session.SqlSessionFactory;
26+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27+
import org.junit.Assert;
28+
import org.junit.BeforeClass;
29+
import org.junit.Test;
30+
31+
public class HashMapTypeHandlerTest {
32+
33+
private static SqlSessionFactory sqlSessionFactory;
34+
35+
@BeforeClass
36+
public static void setUp() throws Exception {
37+
// create an SqlSessionFactory
38+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/hashmaptypehandler/mybatis-config.xml");
39+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
40+
reader.close();
41+
42+
// populate in-memory database
43+
SqlSession session = sqlSessionFactory.openSession();
44+
Connection conn = session.getConnection();
45+
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/hashmaptypehandler/CreateDB.sql");
46+
ScriptRunner runner = new ScriptRunner(conn);
47+
runner.setLogWriter(null);
48+
runner.runScript(reader);
49+
conn.close();
50+
reader.close();
51+
session.close();
52+
}
53+
54+
@Test
55+
public void shouldNotApplyTypeHandlerToParamMap() {
56+
SqlSession sqlSession = sqlSessionFactory.openSession();
57+
try {
58+
Mapper mapper = sqlSession.getMapper(Mapper.class);
59+
User user = mapper.getUser(1, "User1");
60+
Assert.assertEquals("User1", user.getName());
61+
} finally {
62+
sqlSession.close();
63+
}
64+
}
65+
66+
@Test
67+
public void shouldNotApplyTypeHandlerToParamMapXml() {
68+
SqlSession sqlSession = sqlSessionFactory.openSession();
69+
try {
70+
Mapper mapper = sqlSession.getMapper(Mapper.class);
71+
User user = mapper.getUserXml(1, "User1");
72+
Assert.assertEquals("User1", user.getName());
73+
} finally {
74+
sqlSession.close();
75+
}
76+
}
77+
78+
@Test
79+
public void shouldApplyHashMapTypeHandler() {
80+
SqlSession sqlSession = sqlSessionFactory.openSession();
81+
try {
82+
Mapper mapper = sqlSession.getMapper(Mapper.class);
83+
HashMap<String, String> map = new HashMap<String, String>();
84+
map.put("name", "User1");
85+
User user = mapper.getUserWithTypeHandler(map);
86+
Assert.assertNotNull(user);
87+
} finally {
88+
sqlSession.close();
89+
}
90+
}
91+
92+
@Test
93+
public void shouldApplyHashMapTypeHandlerXml() {
94+
SqlSession sqlSession = sqlSessionFactory.openSession();
95+
try {
96+
Mapper mapper = sqlSession.getMapper(Mapper.class);
97+
HashMap<String, String> map = new HashMap<String, String>();
98+
map.put("name", "User1");
99+
User user = mapper.getUserWithTypeHandlerXml(map);
100+
Assert.assertNotNull(user);
101+
} finally {
102+
sqlSession.close();
103+
}
104+
}
105+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright 2009-2017 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.hashmaptypehandler;
17+
18+
import java.util.HashMap;
19+
20+
import org.apache.ibatis.annotations.Param;
21+
import org.apache.ibatis.annotations.Select;
22+
23+
public interface Mapper {
24+
25+
@Select("select * from users where id = #{id} and name = #{name}")
26+
User getUser(@Param("id") Integer id, @Param("name") String name);
27+
28+
User getUserXml(@Param("id") Integer id, @Param("name") String name);
29+
30+
@Select("select * from users where name = #{map}")
31+
User getUserWithTypeHandler(HashMap<String, String> map);
32+
33+
User getUserWithTypeHandlerXml(HashMap<String, String> map);
34+
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2009-2017 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 namespace="org.apache.ibatis.submitted.hashmaptypehandler.Mapper">
24+
25+
<select id="getUserXml" resultType="org.apache.ibatis.submitted.hashmaptypehandler.User">
26+
select * from users where id = #{id} and name = #{name}
27+
</select>
28+
29+
<select id="getUserWithTypeHandlerXml" resultType="org.apache.ibatis.submitted.hashmaptypehandler.User">
30+
select * from users where name = #{name}
31+
</select>
32+
33+
</mapper>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2009-2017 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.hashmaptypehandler;
17+
18+
public class User {
19+
20+
private Integer id;
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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright 2009-2017 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+
23+
<configuration>
24+
25+
<typeHandlers>
26+
<typeHandler handler="org.apache.ibatis.submitted.hashmaptypehandler.HashMapTypeHandler" />
27+
</typeHandlers>
28+
29+
<environments default="development">
30+
<environment id="development">
31+
<transactionManager type="JDBC">
32+
<property name="" value="" />
33+
</transactionManager>
34+
<dataSource type="UNPOOLED">
35+
<property name="driver" value="org.hsqldb.jdbcDriver" />
36+
<property name="url" value="jdbc:hsqldb:mem:hashmaptypehandler" />
37+
<property name="username" value="sa" />
38+
</dataSource>
39+
</environment>
40+
</environments>
41+
42+
<mappers>
43+
<mapper class="org.apache.ibatis.submitted.hashmaptypehandler.Mapper" />
44+
</mappers>
45+
46+
</configuration>

0 commit comments

Comments
 (0)