Skip to content

Commit 74c817e

Browse files
committed
Added test case.
1 parent 5cdac19 commit 74c817e

File tree

7 files changed

+296
-0
lines changed

7 files changed

+296
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright 2009-2016 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.mapper_type_parameter;
17+
18+
import java.util.List;
19+
import java.util.Map;
20+
21+
import org.apache.ibatis.annotations.MapKey;
22+
import org.apache.ibatis.annotations.SelectProvider;
23+
24+
public interface BaseMapper<R> {
25+
26+
@SelectProvider(type = StatementProvider.class, method = "provideSelect")
27+
R select(Integer id);
28+
29+
@SelectProvider(type = StatementProvider.class, method = "provideSelect")
30+
List<R> selectList(Integer id);
31+
32+
@SelectProvider(type = StatementProvider.class, method = "provideSelect")
33+
@MapKey("id")
34+
Map<Integer, R> selectMap(Integer id);
35+
36+
public class StatementProvider {
37+
public String provideSelect(Integer id) {
38+
StringBuilder query = new StringBuilder("select * from person");
39+
if (id != null) {
40+
query.append(" where id = #{id}");
41+
}
42+
query.append(" order by id");
43+
return query.toString();
44+
}
45+
}
46+
47+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--
2+
-- Copyright 2009-2016 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 person if exists;
18+
19+
create table person(
20+
id int,
21+
name varchar(20)
22+
);
23+
24+
insert into person(id, name) values (1, 'Jane'), (2, 'John');
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* Copyright 2009-2016 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.mapper_type_parameter;
17+
18+
import static org.junit.Assert.*;
19+
20+
import java.io.Reader;
21+
import java.sql.Connection;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
import org.apache.ibatis.io.Resources;
26+
import org.apache.ibatis.jdbc.ScriptRunner;
27+
import org.apache.ibatis.session.SqlSession;
28+
import org.apache.ibatis.session.SqlSessionFactory;
29+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
33+
public class MapperTypeParameterTest {
34+
private static SqlSessionFactory sqlSessionFactory;
35+
36+
@BeforeClass
37+
public static void setUp() throws Exception {
38+
// create an SqlSessionFactory
39+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/mapper_type_parameter/mybatis-config.xml");
40+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
41+
reader.close();
42+
43+
// populate in-memory database
44+
SqlSession session = sqlSessionFactory.openSession();
45+
Connection conn = session.getConnection();
46+
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/mapper_type_parameter/CreateDB.sql");
47+
ScriptRunner runner = new ScriptRunner(conn);
48+
runner.setLogWriter(null);
49+
runner.runScript(reader);
50+
reader.close();
51+
session.close();
52+
}
53+
54+
@Test
55+
public void shouldResolveSelect() {
56+
SqlSession sqlSession = sqlSessionFactory.openSession();
57+
try {
58+
PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
59+
Person person = mapper.select(1);
60+
assertEquals("Jane", person.getName());
61+
} finally {
62+
sqlSession.close();
63+
}
64+
}
65+
66+
@Test
67+
public void shouldResolveSelectList() {
68+
SqlSession sqlSession = sqlSessionFactory.openSession();
69+
try {
70+
PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
71+
List<Person> persons = mapper.selectList(null);
72+
assertEquals(2, persons.size());
73+
assertEquals("Jane", persons.get(0).getName());
74+
assertEquals("John", persons.get(1).getName());
75+
} finally {
76+
sqlSession.close();
77+
}
78+
}
79+
80+
@Test
81+
public void shouldResolveMapKeyTypeParameter() {
82+
SqlSession sqlSession = sqlSessionFactory.openSession();
83+
try {
84+
PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
85+
Map<Integer, Person> persons = mapper.selectMap(null);
86+
assertEquals(2, persons.size());
87+
assertEquals("Jane", persons.get(1).getName());
88+
assertEquals("John", persons.get(2).getName());
89+
} finally {
90+
sqlSession.close();
91+
}
92+
}
93+
94+
@Test
95+
public void shouldResolveListTypeParameter() {
96+
SqlSession sqlSession = sqlSessionFactory.openSession();
97+
try {
98+
PersonListMapper mapper = sqlSession.getMapper(PersonListMapper.class);
99+
List<Person> persons = mapper.select(null);
100+
assertEquals(2, persons.size());
101+
assertEquals("Jane", persons.get(0).getName());
102+
} finally {
103+
sqlSession.close();
104+
}
105+
}
106+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2009-2016 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.mapper_type_parameter;
17+
18+
public class Person {
19+
private int id;
20+
21+
private String name;
22+
23+
public int getId() {
24+
return id;
25+
}
26+
27+
public void setId(int 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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright 2009-2016 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.mapper_type_parameter;
17+
18+
import java.util.List;
19+
20+
public interface PersonListMapper extends BaseMapper<List<Person>> {
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2009-2016 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.mapper_type_parameter;
17+
18+
public interface PersonMapper extends BaseMapper<Person> {
19+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright 2009-2016 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 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
20+
21+
<configuration>
22+
23+
<environments default="development">
24+
<environment id="development">
25+
<transactionManager type="JDBC">
26+
<property name="" value="" />
27+
</transactionManager>
28+
<dataSource type="UNPOOLED">
29+
<property name="driver" value="org.hsqldb.jdbcDriver" />
30+
<property name="url" value="jdbc:hsqldb:mem:mappertypeparam" />
31+
<property name="username" value="sa" />
32+
</dataSource>
33+
</environment>
34+
</environments>
35+
36+
<mappers>
37+
<mapper class="org.apache.ibatis.submitted.mapper_type_parameter.PersonMapper" />
38+
<mapper class="org.apache.ibatis.submitted.mapper_type_parameter.PersonListMapper" />
39+
</mappers>
40+
41+
</configuration>

0 commit comments

Comments
 (0)