Skip to content

Commit 3b016b8

Browse files
committed
Merge pull request #337 from magicdoom/master
fix Issues #322
2 parents 31ba2fe + da6e478 commit 3b016b8

File tree

8 files changed

+314
-1
lines changed

8 files changed

+314
-1
lines changed

src/main/java/org/apache/ibatis/executor/BatchExecutor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,19 @@ public int doUpdate(MappedStatement ms, Object parameterObject) throws SQLExcept
6060
if (sql.equals(currentSql) && ms.equals(currentStatement)) {
6161
int last = statementList.size() - 1;
6262
stmt = statementList.get(last);
63+
handler.parameterize(stmt);//fix Issues 322
6364
BatchResult batchResult = batchResultList.get(last);
6465
batchResult.addParameterObject(parameterObject);
6566
} else {
6667
Connection connection = getConnection(ms.getStatementLog());
6768
stmt = handler.prepare(connection);
69+
handler.parameterize(stmt); //fix Issues 322
6870
currentSql = sql;
6971
currentStatement = ms;
7072
statementList.add(stmt);
7173
batchResultList.add(new BatchResult(ms, sql, parameterObject));
7274
}
73-
handler.parameterize(stmt);
75+
// handler.parameterize(stmt);
7476
handler.batch(stmt);
7577
return BATCH_UPDATE_RETURN_VALUE;
7678
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2009-2013 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.batch_test;
17+
18+
import java.io.Reader;
19+
import java.sql.Connection;
20+
21+
import org.apache.ibatis.io.Resources;
22+
import org.apache.ibatis.jdbc.ScriptRunner;
23+
import org.apache.ibatis.session.ExecutorType;
24+
import org.apache.ibatis.session.SqlSession;
25+
import org.apache.ibatis.session.SqlSessionFactory;
26+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27+
import org.junit.Assert;
28+
import org.junit.BeforeClass;
29+
import org.junit.Test;
30+
31+
public class BatchTest
32+
{
33+
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/batch_test/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/batch_test/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 shouldGetAUserNoException() {
56+
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
57+
try {
58+
Mapper mapper = sqlSession.getMapper(Mapper.class);
59+
60+
User user = mapper.getUser(1);
61+
62+
user.setId(2);
63+
user.setName("User2");
64+
mapper.insertUser(user);
65+
Assert.assertEquals("Dept1", mapper.getUser(2).getDept().getName());
66+
}
67+
catch (Exception e)
68+
{
69+
Assert.fail(e.getMessage());
70+
71+
}
72+
73+
finally {
74+
sqlSession.commit();
75+
sqlSession.close();
76+
}
77+
}
78+
79+
80+
81+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--
2+
-- Copyright 2009-2012 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+
dept_id int
23+
);
24+
25+
insert into users (id, name,dept_id) values(1, 'User1',1);
26+
27+
drop table depts if exists;
28+
29+
create table depts (
30+
id int,
31+
name varchar(20)
32+
);
33+
34+
insert into depts (id, name) values(1, 'Dept1');
35+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2009-2012 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.batch_test;
17+
18+
public class Dept
19+
{
20+
21+
private Integer id;
22+
private String name;
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+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2009-2012 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.batch_test;
17+
18+
public interface Mapper {
19+
20+
User getUser(Integer id);
21+
22+
Dept getDept(Integer id) ;
23+
24+
void insertUser(User user);
25+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2009-2013 the original author or authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!DOCTYPE mapper
18+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
19+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
20+
21+
<mapper namespace="org.apache.ibatis.submitted.batch_test.Mapper">
22+
<resultMap id="userResultMap" type="org.apache.ibatis.submitted.batch_test.User">
23+
<id property="id" column="id" />
24+
<result property="name" column="name" />
25+
<association property="dept" column="dept_id" select="getDept"/>
26+
</resultMap>
27+
<select id="getUser" resultMap="userResultMap">
28+
select * from users where id = #{id}
29+
</select>
30+
31+
<select id="getDept" resultType="org.apache.ibatis.submitted.batch_test.Dept" >
32+
select * from depts where id = #{id}
33+
</select>
34+
35+
<insert id="insertUser">
36+
insert into users values(#{id}, #{name},#{dept.id})
37+
</insert>
38+
</mapper>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2009-2012 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.batch_test;
17+
18+
public class User {
19+
20+
private Integer id;
21+
private String name;
22+
private Dept dept;
23+
24+
public Dept getDept()
25+
{
26+
return dept;
27+
}
28+
29+
public void setDept(Dept dept)
30+
{
31+
this.dept = dept;
32+
}
33+
34+
public Integer getId() {
35+
return id;
36+
}
37+
38+
public void setId(Integer id) {
39+
this.id = id;
40+
}
41+
42+
public String getName() {
43+
return name;
44+
}
45+
46+
public void setName(String name) {
47+
this.name = name;
48+
}
49+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
Copyright 2009-2013 the original author or authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!DOCTYPE configuration
18+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
19+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
20+
21+
<configuration>
22+
<settings>
23+
<setting name="lazyLoadingEnabled" value="true"/>
24+
<setting name="aggressiveLazyLoading" value="false"/>
25+
<setting name="defaultExecutorType" value="BATCH"/>
26+
</settings>
27+
<environments default="development">
28+
<environment id="development">
29+
<transactionManager type="JDBC">
30+
<property name="" value="" />
31+
</transactionManager>
32+
<dataSource type="UNPOOLED">
33+
<property name="driver" value="org.hsqldb.jdbcDriver" />
34+
<property name="url" value="jdbc:hsqldb:mem:basetest" />
35+
<property name="username" value="sa" />
36+
</dataSource>
37+
</environment>
38+
</environments>
39+
40+
<mappers>
41+
<mapper class="org.apache.ibatis.submitted.batch_test.Mapper" />
42+
</mappers>
43+
44+
</configuration>

0 commit comments

Comments
 (0)