forked from mybatis/mybatis-3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test when compile with '-parameters'
- Loading branch information
Showing
6 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/test/java/org/apache/ibatis/submitted/param_name_resolve/CreateDB.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
-- | ||
-- Copyright 2009-2016 the original author or authors. | ||
-- | ||
-- Licensed under the Apache License, Version 2.0 (the "License"); | ||
-- you may not use this file except in compliance with the License. | ||
-- You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
-- | ||
|
||
drop table users if exists; | ||
|
||
create table users ( | ||
id int, | ||
name varchar(20) | ||
); | ||
|
||
insert into users (id, name) values | ||
(1, 'User1'), (2, 'User2'), (3, 'User3'); |
7 changes: 7 additions & 0 deletions
7
src/test/java/org/apache/ibatis/submitted/param_name_resolve/Mapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.apache.ibatis.submitted.param_name_resolve; | ||
|
||
import java.util.List; | ||
|
||
public interface Mapper { | ||
Long getUserCount(List<Integer> ids); | ||
} |
35 changes: 35 additions & 0 deletions
35
src/test/java/org/apache/ibatis/submitted/param_name_resolve/Mapper.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright 2009-2016 the original author or authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<!DOCTYPE mapper | ||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||
|
||
<mapper namespace="org.apache.ibatis.submitted.param_name_resolve.Mapper"> | ||
<!-- when compiled with -parameters and useActualParamName enabled, --> | ||
<!-- the first parameter should be accessed by its name 'ids', --> | ||
<!-- in addition with default name 'list' and 'collection'. --> | ||
<select id="getUserCount" resultType="Long"> | ||
select count(*) | ||
from users u | ||
where u.id in | ||
<foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'> | ||
#{item} | ||
</foreach> | ||
</select> | ||
</mapper> |
50 changes: 50 additions & 0 deletions
50
src/test/java/org/apache/ibatis/submitted/param_name_resolve/ParamNameResolveTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.apache.ibatis.submitted.param_name_resolve; | ||
|
||
import org.apache.ibatis.io.Resources; | ||
import org.apache.ibatis.jdbc.ScriptRunner; | ||
import org.apache.ibatis.session.SqlSession; | ||
import org.apache.ibatis.session.SqlSessionFactory; | ||
import org.apache.ibatis.session.SqlSessionFactoryBuilder; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import java.io.Reader; | ||
import java.sql.Connection; | ||
import java.util.Arrays; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class ParamNameResolveTest { | ||
private static SqlSessionFactory sqlSessionFactory; | ||
|
||
@BeforeClass | ||
public static void setUp() throws Exception { | ||
// create an SqlSessionFactory | ||
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/param_name_resolve/mybatis-config.xml"); | ||
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); | ||
reader.close(); | ||
|
||
// populate in-memory database | ||
SqlSession session = sqlSessionFactory.openSession(); | ||
Connection conn = session.getConnection(); | ||
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/param_name_resolve/CreateDB.sql"); | ||
ScriptRunner runner = new ScriptRunner(conn); | ||
runner.setLogWriter(null); | ||
runner.runScript(reader); | ||
conn.close(); | ||
reader.close(); | ||
session.close(); | ||
} | ||
|
||
@Test | ||
public void testSingleParameter() { | ||
SqlSession sqlSession = sqlSessionFactory.openSession(); | ||
try { | ||
Mapper mapper = sqlSession.getMapper(Mapper.class); | ||
long count = mapper.getUserCount(Arrays.asList(1, 2)); | ||
assertEquals(2, count); | ||
} finally { | ||
sqlSession.close(); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/test/java/org/apache/ibatis/submitted/param_name_resolve/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.apache.ibatis.submitted.param_name_resolve; | ||
|
||
public class User { | ||
private int id; | ||
private String name; | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/test/java/org/apache/ibatis/submitted/param_name_resolve/mybatis-config.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!-- | ||
Copyright 2009-2016 the original author or authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<!DOCTYPE configuration | ||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN" | ||
"http://mybatis.org/dtd/mybatis-3-config.dtd"> | ||
|
||
<configuration> | ||
|
||
<environments default="development"> | ||
<environment id="development"> | ||
<transactionManager type="JDBC"> | ||
<property name="" value="" /> | ||
</transactionManager> | ||
<dataSource type="UNPOOLED"> | ||
<property name="driver" value="org.hsqldb.jdbcDriver" /> | ||
<property name="url" value="jdbc:hsqldb:mem:param_name_resolve" /> | ||
<property name="username" value="sa" /> | ||
</dataSource> | ||
</environment> | ||
</environments> | ||
|
||
<mappers> | ||
<mapper class="org.apache.ibatis.submitted.param_name_resolve.Mapper" /> | ||
</mappers> | ||
|
||
</configuration> |