Skip to content

Commit 6d33f25

Browse files
committed
Merge pull request #518 from kazuki43zoo/issues/444_allow-EnumTypeHandler-on-annotation-config
Allow TypeHandler for Enum on annotation configuration #444
2 parents 4038ee6 + 57ff897 commit 6d33f25

File tree

10 files changed

+370
-6
lines changed

10 files changed

+370
-6
lines changed

src/main/java/org/apache/ibatis/annotations/Arg.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
JdbcType jdbcType() default JdbcType.UNDEFINED;
4040

41-
Class<? extends TypeHandler<?>> typeHandler() default UnknownTypeHandler.class;
41+
Class<? extends TypeHandler> typeHandler() default UnknownTypeHandler.class;
4242

4343
String select() default "";
4444

src/main/java/org/apache/ibatis/annotations/Result.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
JdbcType jdbcType() default JdbcType.UNDEFINED;
4242

43-
Class<? extends TypeHandler<?>> typeHandler() default UnknownTypeHandler.class;
43+
Class<? extends TypeHandler> typeHandler() default UnknownTypeHandler.class;
4444

4545
One one() default @One;
4646

src/main/java/org/apache/ibatis/annotations/TypeDiscriminator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
JdbcType jdbcType() default JdbcType.UNDEFINED;
3838

39-
Class<? extends TypeHandler<?>> typeHandler() default UnknownTypeHandler.class;
39+
Class<? extends TypeHandler> typeHandler() default UnknownTypeHandler.class;
4040

4141
Case[] cases();
4242
}

src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,9 @@ private Discriminator applyDiscriminator(String resultMapId, Class<?> resultType
240240
String column = discriminator.column();
241241
Class<?> javaType = discriminator.javaType() == void.class ? String.class : discriminator.javaType();
242242
JdbcType jdbcType = discriminator.jdbcType() == JdbcType.UNDEFINED ? null : discriminator.jdbcType();
243-
Class<? extends TypeHandler<?>> typeHandler = discriminator.typeHandler() == UnknownTypeHandler.class ? null : discriminator.typeHandler();
243+
@SuppressWarnings("unchecked")
244+
Class<? extends TypeHandler<?>> typeHandler = (Class<? extends TypeHandler<?>>)
245+
(discriminator.typeHandler() == UnknownTypeHandler.class ? null : discriminator.typeHandler());
244246
Case[] cases = discriminator.cases();
245247
Map<String, String> discriminatorMap = new HashMap<String, String>();
246248
for (Case c : cases) {
@@ -491,6 +493,9 @@ private void applyResults(Result[] results, Class<?> resultType, List<ResultMapp
491493
if (result.id()) {
492494
flags.add(ResultFlag.ID);
493495
}
496+
@SuppressWarnings("unchecked")
497+
Class<? extends TypeHandler<?>> typeHandler = (Class<? extends TypeHandler<?>>)
498+
((result.typeHandler() == UnknownTypeHandler.class) ? null : result.typeHandler());
494499
ResultMapping resultMapping = assistant.buildResultMapping(
495500
resultType,
496501
nullOrEmpty(result.property()),
@@ -501,7 +506,7 @@ private void applyResults(Result[] results, Class<?> resultType, List<ResultMapp
501506
null,
502507
null,
503508
null,
504-
result.typeHandler() == UnknownTypeHandler.class ? null : result.typeHandler(),
509+
typeHandler,
505510
flags,
506511
null,
507512
null,
@@ -545,6 +550,9 @@ private void applyConstructorArgs(Arg[] args, Class<?> resultType, List<ResultMa
545550
if (arg.id()) {
546551
flags.add(ResultFlag.ID);
547552
}
553+
@SuppressWarnings("unchecked")
554+
Class<? extends TypeHandler<?>> typeHandler = (Class<? extends TypeHandler<?>>)
555+
(arg.typeHandler() == UnknownTypeHandler.class ? null : arg.typeHandler());
548556
ResultMapping resultMapping = assistant.buildResultMapping(
549557
resultType,
550558
null,
@@ -555,7 +563,7 @@ private void applyConstructorArgs(Arg[] args, Class<?> resultType, List<ResultMa
555563
nullOrEmpty(arg.resultMap()),
556564
null,
557565
null,
558-
arg.typeHandler() == UnknownTypeHandler.class ? null : arg.typeHandler(),
566+
typeHandler,
559567
flags,
560568
null,
561569
null,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--
2+
-- Copyright 2009-2015 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+
create table person (
18+
id int,
19+
firstName varchar(100),
20+
lastName varchar(100),
21+
personType int -- important: Enum original number (starting from 0)
22+
);
23+
24+
INSERT INTO person (id, firstName, lastName, personType) VALUES (1, 'John', 'Smith', 0);
25+
INSERT INTO person (id, firstName, lastName, personType) VALUES (2, 'Mike', 'Jordan', 1);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright 2009-2015 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.enumtypehandler_on_annotation;
17+
18+
/**
19+
* @author Kazuki Shimizu
20+
* @since #444
21+
*/
22+
public class Employee extends Person {
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/**
2+
* Copyright 2009-2015 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.enumtypehandler_on_annotation;
17+
18+
import org.apache.ibatis.io.Resources;
19+
import org.apache.ibatis.jdbc.ScriptRunner;
20+
import org.apache.ibatis.session.SqlSession;
21+
import org.apache.ibatis.session.SqlSessionFactory;
22+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.BeforeClass;
26+
import org.junit.Test;
27+
28+
import java.io.Reader;
29+
import java.sql.Connection;
30+
import java.sql.DriverManager;
31+
32+
import static org.junit.Assert.*;
33+
import static org.hamcrest.core.Is.*;
34+
35+
/**
36+
* Tests for type handler of enum using annotations.
37+
*
38+
* @see org.apache.ibatis.annotations.Arg
39+
* @see org.apache.ibatis.annotations.Result
40+
* @see org.apache.ibatis.annotations.TypeDiscriminator
41+
*
42+
* @author Kazuki Shimizu
43+
* @since #444
44+
*/
45+
public class EnumTypeHandlerUsingAnnotationTest {
46+
47+
private static SqlSessionFactory sqlSessionFactory;
48+
private SqlSession sqlSession;
49+
50+
@BeforeClass
51+
public static void initDatabase() throws Exception {
52+
Connection conn = null;
53+
54+
try {
55+
Class.forName("org.hsqldb.jdbcDriver");
56+
conn = DriverManager.getConnection("jdbc:hsqldb:mem:enumtypehandler_on_annotation", "sa", "");
57+
58+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/enumtypehandler_on_annotation/CreateDB.sql");
59+
60+
ScriptRunner runner = new ScriptRunner(conn);
61+
runner.setLogWriter(null);
62+
runner.setErrorLogWriter(null);
63+
runner.runScript(reader);
64+
conn.commit();
65+
reader.close();
66+
67+
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/enumtypehandler_on_annotation/mybatis-config.xml");
68+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
69+
sqlSessionFactory.getConfiguration().getMapperRegistry().addMapper(PersonMapper.class);
70+
reader.close();
71+
} finally {
72+
if (conn != null) {
73+
conn.close();
74+
}
75+
}
76+
}
77+
78+
@Before
79+
public void openSqlSession() {
80+
this.sqlSession = sqlSessionFactory.openSession();
81+
}
82+
83+
@After
84+
public void closeSqlSession() {
85+
sqlSession.close();
86+
}
87+
88+
@Test
89+
public void testForArg() {
90+
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
91+
{
92+
Person person = personMapper.findOneUsingConstructor(1);
93+
assertThat(person.getId(), is(1));
94+
assertThat(person.getFirstName(), is("John"));
95+
assertThat(person.getLastName(), is("Smith"));
96+
assertThat(person.getPersonType(), is(Person.PersonType.PERSON)); // important
97+
}
98+
{
99+
Person employee = personMapper.findOneUsingConstructor(2);
100+
assertThat(employee.getId(), is(2));
101+
assertThat(employee.getFirstName(), is("Mike"));
102+
assertThat(employee.getLastName(), is("Jordan"));
103+
assertThat(employee.getPersonType(), is(Person.PersonType.EMPLOYEE)); // important
104+
}
105+
}
106+
107+
@Test
108+
public void testForResult() {
109+
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
110+
{
111+
Person person = personMapper.findOneUsingSetter(1);
112+
assertThat(person.getId(), is(1));
113+
assertThat(person.getFirstName(), is("John"));
114+
assertThat(person.getLastName(), is("Smith"));
115+
assertThat(person.getPersonType(), is(Person.PersonType.PERSON)); // important
116+
}
117+
{
118+
Person employee = personMapper.findOneUsingSetter(2);
119+
assertThat(employee.getId(), is(2));
120+
assertThat(employee.getFirstName(), is("Mike"));
121+
assertThat(employee.getLastName(), is("Jordan"));
122+
assertThat(employee.getPersonType(), is(Person.PersonType.EMPLOYEE)); // important
123+
}
124+
}
125+
126+
@Test
127+
public void testForTypeDiscriminator() {
128+
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
129+
{
130+
Person person = personMapper.findOneUsingTypeDiscriminator(1);
131+
assertTrue(person.getClass() == Person.class); // important
132+
assertThat(person.getId(), is(1));
133+
assertThat(person.getFirstName(), is("John"));
134+
assertThat(person.getLastName(), is("Smith"));
135+
assertThat(person.getPersonType(), is(Person.PersonType.PERSON));
136+
}
137+
{
138+
Person employee = personMapper.findOneUsingTypeDiscriminator(2);
139+
assertTrue(employee.getClass() == Employee.class); // important
140+
assertThat(employee.getId(), is(2));
141+
assertThat(employee.getFirstName(), is("Mike"));
142+
assertThat(employee.getLastName(), is("Jordan"));
143+
assertThat(employee.getPersonType(), is(Person.PersonType.EMPLOYEE));
144+
}
145+
}
146+
147+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright 2009-2015 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.enumtypehandler_on_annotation;
17+
18+
/**
19+
* @author Kazuki Shimizu
20+
* @since #444
21+
*/
22+
public class Person {
23+
24+
enum PersonType {
25+
PERSON,
26+
EMPLOYEE
27+
}
28+
29+
private Integer id;
30+
private String firstName;
31+
private String lastName;
32+
private PersonType personType;
33+
34+
public Person() {
35+
}
36+
37+
public Person(Integer id, String firstName, String lastName, PersonType personType) {
38+
this.id = id;
39+
this.firstName = firstName;
40+
this.lastName = lastName;
41+
this.personType = personType;
42+
}
43+
44+
public String getFirstName() {
45+
return firstName;
46+
}
47+
public void setFirstName(String firstName) {
48+
this.firstName = firstName;
49+
}
50+
public String getLastName() {
51+
return lastName;
52+
}
53+
public void setLastName(String lastName) {
54+
this.lastName = lastName;
55+
}
56+
public Integer getId() {
57+
return id;
58+
}
59+
public void setId(Integer id) {
60+
this.id = id;
61+
}
62+
public PersonType getPersonType() {
63+
return personType;
64+
}
65+
public void setPersonType(PersonType personType) {
66+
this.personType = personType;
67+
}
68+
69+
}

0 commit comments

Comments
 (0)