Skip to content

Commit 6006b92

Browse files
authored
Merge pull request mybatis#1490 from kazuki43zoo/mybatisgh-1489-enum-with-method
Support anonymous enum object that implement method
2 parents 0890553 + a90af3d commit 6006b92

File tree

12 files changed

+350
-5
lines changed

12 files changed

+350
-5
lines changed

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,12 @@ private Map<JdbcType, TypeHandler<?>> getJdbcHandlerMap(Type type) {
241241
}
242242
if (jdbcHandlerMap == null && type instanceof Class) {
243243
Class<?> clazz = (Class<?>) type;
244-
if (clazz.isEnum()) {
245-
jdbcHandlerMap = getJdbcHandlerMapForEnumInterfaces(clazz, clazz);
244+
if (Enum.class.isAssignableFrom(clazz)) {
245+
Class<?> enumClass = clazz.isAnonymousClass() ? clazz.getSuperclass() : clazz;
246+
jdbcHandlerMap = getJdbcHandlerMapForEnumInterfaces(enumClass, enumClass);
246247
if (jdbcHandlerMap == null) {
247-
register(clazz, getInstance(clazz, defaultEnumTypeHandler));
248-
return typeHandlerMap.get(clazz);
248+
register(enumClass, getInstance(enumClass, defaultEnumTypeHandler));
249+
return typeHandlerMap.get(enumClass);
249250
}
250251
} else {
251252
jdbcHandlerMap = getJdbcHandlerMapForSuperclass(clazz);

src/test/java/org/apache/ibatis/submitted/enum_interface_type_handler/EnumInterfaceTypeHandlerTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,18 @@ void shouldInsertAUser() {
6565
assertEquals(Color.BLUE, result.getColor());
6666
}
6767
}
68+
69+
@Test
70+
void shouldInsertAUserWithoutParameterTypeInXmlElement() {
71+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
72+
XmlMapper mapper = sqlSession.getMapper(XmlMapper.class);
73+
User user = new User();
74+
user.setId(2);
75+
user.setColor(Color.BLUE);
76+
mapper.insertUser(user);
77+
User result = sqlSession.getMapper(Mapper.class).getUser(2);
78+
assertEquals(Color.BLUE, result.getColor());
79+
}
80+
}
81+
6882
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright 2009-2019 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.enum_interface_type_handler;
17+
18+
public interface XmlMapper {
19+
int insertUser(User user);
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2009-2019 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.enum_interface_type_handler.XmlMapper">
24+
25+
<!-- without parameterType attribute -->
26+
<insert id="insertUser">
27+
insert into users (id, color) values (#{id}, #{color})
28+
</insert>
29+
30+
</mapper>

src/test/java/org/apache/ibatis/submitted/enum_interface_type_handler/mybatis-config.xml

+2-1
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-2019 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.
@@ -39,5 +39,6 @@
3939
</environments>
4040
<mappers>
4141
<mapper class="org.apache.ibatis.submitted.enum_interface_type_handler.Mapper" />
42+
<mapper class="org.apache.ibatis.submitted.enum_interface_type_handler.XmlMapper" />
4243
</mappers>
4344
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--
2+
-- Copyright 2009-2019 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+
cur varchar(20)
23+
);
24+
25+
insert into users (id, name, cur) values(1, 'User1', 'RMB');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright 2009-2019 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.enum_with_method;
17+
18+
import java.math.BigDecimal;
19+
20+
public enum Currency {
21+
22+
Dollar {
23+
@Override
24+
public BigDecimal getExchange() {
25+
return null;
26+
}
27+
},
28+
29+
RMB {
30+
@Override
31+
public BigDecimal getExchange() {
32+
return null;
33+
}
34+
};
35+
36+
public abstract BigDecimal getExchange();
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright 2009-2019 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.enum_with_method;
17+
18+
import java.io.Reader;
19+
20+
import org.apache.ibatis.BaseDataTest;
21+
import org.apache.ibatis.io.Resources;
22+
import org.apache.ibatis.session.SqlSession;
23+
import org.apache.ibatis.session.SqlSessionFactory;
24+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
25+
import org.junit.jupiter.api.Assertions;
26+
import org.junit.jupiter.api.BeforeAll;
27+
import org.junit.jupiter.api.Test;
28+
29+
class EnumWithMethodTest {
30+
31+
private static SqlSessionFactory sqlSessionFactory;
32+
33+
@BeforeAll
34+
static void setUp() throws Exception {
35+
// create an SqlSessionFactory
36+
try (Reader reader = Resources
37+
.getResourceAsReader("org/apache/ibatis/submitted/enum_with_method/mybatis-config.xml")) {
38+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
39+
}
40+
41+
// populate in-memory database
42+
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
43+
"org/apache/ibatis/submitted/enum_with_method/CreateDB.sql");
44+
}
45+
46+
@Test
47+
void shouldGetAUser() {
48+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
49+
Mapper mapper = sqlSession.getMapper(Mapper.class);
50+
User user = mapper.getUser(1);
51+
Assertions.assertEquals("User1", user.getName());
52+
}
53+
}
54+
55+
@Test
56+
void shouldInsertAUser() {
57+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
58+
Mapper mapper = sqlSession.getMapper(Mapper.class);
59+
User user = new User();
60+
user.setId(2);
61+
user.setName("User2");
62+
user.setCur(Currency.Dollar);
63+
mapper.insertUser(user);
64+
}
65+
}
66+
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright 2009-2019 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.enum_with_method;
17+
18+
public interface Mapper {
19+
20+
User getUser(Integer id);
21+
22+
void insertUser(User user);
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2009-2019 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.enum_with_method.Mapper">
24+
25+
<select id="getUser" resultType="org.apache.ibatis.submitted.enum_with_method.User">
26+
select *
27+
from users
28+
where id = #{id}
29+
</select>
30+
31+
<!-- without parameterType="org.apache.ibatis.submitted.enum_with_method.User" -->
32+
<insert id="insertUser">
33+
insert into users
34+
values (#{id}, #{name}, #{cur})
35+
</insert>
36+
37+
</mapper>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright 2009-2019 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.enum_with_method;
17+
18+
public class User {
19+
20+
private Integer id;
21+
private String name;
22+
private Currency cur;
23+
24+
public Integer getId() {
25+
return id;
26+
}
27+
28+
public void setId(Integer id) {
29+
this.id = id;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public Currency getCur() {
41+
return cur;
42+
}
43+
44+
public void setCur(Currency cur) {
45+
this.cur = cur;
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright 2009-2019 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+
<environments default="development">
26+
<environment id="development">
27+
<transactionManager type="JDBC">
28+
<property name="" value="" />
29+
</transactionManager>
30+
<dataSource type="UNPOOLED">
31+
<property name="driver" value="org.hsqldb.jdbcDriver" />
32+
<property name="url" value="jdbc:hsqldb:mem:enumwithmethod" />
33+
<property name="username" value="sa" />
34+
</dataSource>
35+
</environment>
36+
</environments>
37+
38+
<mappers>
39+
<mapper class="org.apache.ibatis.submitted.enum_with_method.Mapper" />
40+
</mappers>
41+
42+
</configuration>

0 commit comments

Comments
 (0)