Skip to content

Commit 51710ed

Browse files
zhzhsxkazuki43zoo
authored andcommitted
add a test to reproduce problem of enum type handler
MyBatis cannot find the type handler for an enum when 1. the enum value has methods, 2. no parameterType is provided in corresponding insert mappers
1 parent 0419f49 commit 51710ed

File tree

7 files changed

+280
-0
lines changed

7 files changed

+280
-0
lines changed
Lines changed: 25 additions & 0 deletions
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');
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
package org.apache.ibatis.submitted.enum_with_method;
18+
19+
import java.math.BigDecimal;
20+
21+
public enum Currency {
22+
23+
Dollar {
24+
@Override
25+
public BigDecimal getExchange() {
26+
return null;
27+
}
28+
},
29+
30+
RMB {
31+
@Override
32+
public BigDecimal getExchange() {
33+
return null;
34+
}
35+
};
36+
37+
public abstract BigDecimal getExchange();
38+
}
Lines changed: 67 additions & 0 deletions
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+
}
Lines changed: 24 additions & 0 deletions
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+
}
Lines changed: 37 additions & 0 deletions
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>
Lines changed: 47 additions & 0 deletions
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+
}
Lines changed: 42 additions & 0 deletions
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)