Skip to content

Commit 17a9930

Browse files
committed
Added a test case for #2754
Also related to #206 #575
1 parent 35262b0 commit 17a9930

File tree

6 files changed

+224
-0
lines changed

6 files changed

+224
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2009-2022 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+
* https://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.bind_in_foreach;
17+
18+
import static org.junit.jupiter.api.Assertions.*;
19+
20+
import java.io.Reader;
21+
import java.util.List;
22+
23+
import org.apache.ibatis.BaseDataTest;
24+
import org.apache.ibatis.io.Resources;
25+
import org.apache.ibatis.session.SqlSession;
26+
import org.apache.ibatis.session.SqlSessionFactory;
27+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28+
import org.junit.jupiter.api.BeforeAll;
29+
import org.junit.jupiter.api.Test;
30+
31+
class BindInForeachTest {
32+
33+
private static SqlSessionFactory sqlSessionFactory;
34+
35+
@BeforeAll
36+
static void setUp() throws Exception {
37+
try (Reader reader = Resources
38+
.getResourceAsReader("org/apache/ibatis/submitted/bind_in_foreach/mybatis-config.xml")) {
39+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
40+
}
41+
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
42+
"org/apache/ibatis/submitted/bind_in_foreach/CreateDB.sql");
43+
}
44+
45+
@Test
46+
void testBindInForeach() {
47+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
48+
Mapper mapper = sqlSession.getMapper(Mapper.class);
49+
assertEquals(3, mapper.createUsers(List.of(2, 4, 6)));
50+
List<User> users = mapper.selectUsers();
51+
assertEquals(3, users.size());
52+
assertEquals(1, users.get(0).getId());
53+
assertEquals("User2", users.get(0).getName());
54+
assertEquals(2, users.get(1).getId());
55+
assertEquals("User4", users.get(1).getName());
56+
assertEquals(3, users.get(2).getId());
57+
assertEquals("User6", users.get(2).getName());
58+
}
59+
}
60+
61+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2009-2022 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+
* https://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.bind_in_foreach;
17+
18+
import java.util.List;
19+
20+
import org.apache.ibatis.annotations.Select;
21+
22+
public interface Mapper {
23+
24+
@Select("select * from users order by id")
25+
List<User> selectUsers();
26+
27+
int createUsers(List<Integer> numbers);
28+
29+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2009-2022 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+
* https://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.bind_in_foreach;
17+
18+
public class User {
19+
20+
private Integer id;
21+
private String name;
22+
23+
public Integer getId() {
24+
return id;
25+
}
26+
27+
public void setId(Integer 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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--
2+
-- Copyright 2009-2022 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+
-- https://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+
);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2009-2022 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+
https://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 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
20+
<mapper
21+
namespace="org.apache.ibatis.submitted.bind_in_foreach.Mapper">
22+
23+
<insert id="createUsers">
24+
insert into users (id, name) values
25+
<foreach item="num" index="idx" collection="list"
26+
separator=",">
27+
<bind name="id" value="idx + 1" />
28+
<bind name="name" value="'User' + num" />
29+
(#{id}, #{name})
30+
</foreach>
31+
</insert>
32+
33+
</mapper>
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-2022 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+
https://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+
"https://mybatis.org/dtd/mybatis-3-config.dtd">
22+
23+
<configuration>
24+
<environments default="development">
25+
<environment id="development">
26+
<transactionManager type="JDBC">
27+
<property name="" value="" />
28+
</transactionManager>
29+
<dataSource type="UNPOOLED">
30+
<property name="driver" value="org.hsqldb.jdbcDriver" />
31+
<property name="url" value="jdbc:hsqldb:mem:bindinforeach" />
32+
<property name="username" value="sa" />
33+
</dataSource>
34+
</environment>
35+
</environments>
36+
37+
<mappers>
38+
<mapper class="org.apache.ibatis.submitted.bind_in_foreach.Mapper" />
39+
</mappers>
40+
41+
</configuration>

0 commit comments

Comments
 (0)