Skip to content

Commit 75171e8

Browse files
authored
Merge pull request #2957 from harawata/gh/2956
Registered type handler should be used for anonymous enum
2 parents fdbd5e5 + 06ec2c0 commit 75171e8

File tree

9 files changed

+139
-11
lines changed

9 files changed

+139
-11
lines changed

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,13 @@ private Map<JdbcType, TypeHandler<?>> getJdbcHandlerMap(Type type) {
263263
if (type instanceof Class) {
264264
Class<?> clazz = (Class<?>) type;
265265
if (Enum.class.isAssignableFrom(clazz)) {
266-
Class<?> enumClass = clazz.isAnonymousClass() ? clazz.getSuperclass() : clazz;
267-
jdbcHandlerMap = getJdbcHandlerMapForEnumInterfaces(enumClass, enumClass);
266+
if (clazz.isAnonymousClass()) {
267+
return getJdbcHandlerMap(clazz.getSuperclass());
268+
}
269+
jdbcHandlerMap = getJdbcHandlerMapForEnumInterfaces(clazz, clazz);
268270
if (jdbcHandlerMap == null) {
269-
register(enumClass, getInstance(enumClass, defaultEnumTypeHandler));
270-
return typeHandlerMap.get(enumClass);
271+
register(clazz, getInstance(clazz, defaultEnumTypeHandler));
272+
return typeHandlerMap.get(clazz);
271273
}
272274
} else {
273275
jdbcHandlerMap = getJdbcHandlerMapForSuperclass(clazz);

src/test/java/org/apache/ibatis/scripting/defaults/DefaultParameterHandlerTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import static org.mockito.ArgumentMatchers.anyInt;
2020
import static org.mockito.Mockito.doThrow;
2121
import static org.mockito.Mockito.mock;
22-
import static org.mockito.Mockito.when;
23-
import static org.mockito.Mockito.verify;
2422
import static org.mockito.Mockito.times;
23+
import static org.mockito.Mockito.verify;
24+
import static org.mockito.Mockito.when;
2525

2626
import java.sql.PreparedStatement;
2727
import java.sql.SQLException;

src/test/java/org/apache/ibatis/submitted/enum_with_method/EnumWithMethodTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void shouldGetAUser() {
4949
Mapper mapper = sqlSession.getMapper(Mapper.class);
5050
User user = mapper.getUser(1);
5151
Assertions.assertEquals("User1", user.getName());
52+
Assertions.assertEquals(Mood.GOOD, user.getMood());
5253
}
5354
}
5455

@@ -60,7 +61,15 @@ void shouldInsertAUser() {
6061
user.setId(2);
6162
user.setName("User2");
6263
user.setCur(Currency.Dollar);
64+
user.setMood(Mood.BAD);
6365
mapper.insertUser(user);
66+
sqlSession.commit();
67+
}
68+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
69+
Mapper mapper = sqlSession.getMapper(Mapper.class);
70+
User user = mapper.getUser(2);
71+
Assertions.assertEquals("User2", user.getName());
72+
Assertions.assertEquals(Mood.BAD, user.getMood());
6473
}
6574
}
6675

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2009-2023 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+
* https://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.enum_with_method;
18+
19+
public enum Mood {
20+
GOOD(1) {
21+
@Override
22+
public String getMessage() {
23+
return "Yeehaw";
24+
}
25+
},
26+
BAD(2) {
27+
@Override
28+
public String getMessage() {
29+
return "whatevs";
30+
}
31+
};
32+
33+
private int value;
34+
35+
private Mood(int value) {
36+
this.value = value;
37+
}
38+
39+
public int getValue() {
40+
return value;
41+
}
42+
43+
public static Mood fromValue(int i) {
44+
for (Mood t : values()) {
45+
if (t.value == i) {
46+
return t;
47+
}
48+
}
49+
throw new IllegalArgumentException("Unknown value for Mood: " + i);
50+
}
51+
52+
public abstract String getMessage();
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2009-2023 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+
* https://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.enum_with_method;
18+
19+
import java.sql.CallableStatement;
20+
import java.sql.PreparedStatement;
21+
import java.sql.ResultSet;
22+
import java.sql.SQLException;
23+
24+
import org.apache.ibatis.type.BaseTypeHandler;
25+
import org.apache.ibatis.type.JdbcType;
26+
27+
public class MoodTypeTypeHandler extends BaseTypeHandler<Mood> {
28+
29+
@Override
30+
public void setNonNullParameter(PreparedStatement ps, int i, Mood parameter, JdbcType jdbcType) throws SQLException {
31+
ps.setInt(i, parameter.getValue());
32+
}
33+
34+
@Override
35+
public Mood getNullableResult(ResultSet rs, String columnName) throws SQLException {
36+
return Mood.fromValue(rs.getInt(columnName));
37+
}
38+
39+
@Override
40+
public Mood getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
41+
return Mood.fromValue(rs.getInt(columnIndex));
42+
}
43+
44+
@Override
45+
public Mood getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
46+
return Mood.fromValue(cs.getInt(columnIndex));
47+
}
48+
49+
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class User {
2020
private Integer id;
2121
private String name;
2222
private Currency cur;
23+
private Mood mood;
2324

2425
public Integer getId() {
2526
return id;
@@ -44,4 +45,12 @@ public Currency getCur() {
4445
public void setCur(Currency cur) {
4546
this.cur = cur;
4647
}
48+
49+
public Mood getMood() {
50+
return mood;
51+
}
52+
53+
public void setMood(Mood mood) {
54+
this.mood = mood;
55+
}
4756
}

src/test/resources/org/apache/ibatis/submitted/enum_with_method/CreateDB.sql

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--
2-
-- Copyright 2009-2022 the original author or authors.
2+
-- Copyright 2009-2023 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.
@@ -19,7 +19,8 @@ drop table users if exists;
1919
create table users (
2020
id int,
2121
name varchar(20),
22-
cur varchar(20)
22+
cur varchar(20),
23+
mood int
2324
);
2425

25-
insert into users (id, name, cur) values(1, 'User1', 'RMB');
26+
insert into users (id, name, cur, mood) values(1, 'User1', 'RMB', 1);

src/test/resources/org/apache/ibatis/submitted/enum_with_method/Mapper.xml

+2-2
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-2022 the original author or authors.
4+
Copyright 2009-2023 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.
@@ -31,7 +31,7 @@
3131
<!-- without parameterType="org.apache.ibatis.submitted.enum_with_method.User" -->
3232
<insert id="insertUser">
3333
insert into users
34-
values (#{id}, #{name}, #{cur})
34+
values (#{id}, #{name}, #{cur}, #{mood})
3535
</insert>
3636

3737
</mapper>

src/test/resources/org/apache/ibatis/submitted/enum_with_method/mybatis-config.xml

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222

2323
<configuration>
2424

25+
<typeHandlers>
26+
<typeHandler
27+
handler="org.apache.ibatis.submitted.enum_with_method.MoodTypeTypeHandler" />
28+
</typeHandlers>
29+
2530
<environments default="development">
2631
<environment id="development">
2732
<transactionManager type="JDBC">

0 commit comments

Comments
 (0)