Skip to content

Commit a9d7ed0

Browse files
committed
#547 Merged into 3.3.x branch
1 parent 232ae0a commit a9d7ed0

File tree

7 files changed

+280
-9
lines changed

7 files changed

+280
-9
lines changed

src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
import java.sql.ResultSetMetaData;
2020
import java.sql.SQLException;
2121
import java.sql.Statement;
22-
import java.util.ArrayList;
23-
import java.util.List;
22+
import java.util.*;
2423

2524
import org.apache.ibatis.executor.Executor;
2625
import org.apache.ibatis.executor.ExecutorException;
2726
import org.apache.ibatis.mapping.MappedStatement;
2827
import org.apache.ibatis.reflection.MetaObject;
2928
import org.apache.ibatis.session.Configuration;
29+
import org.apache.ibatis.type.JdbcType;
3030
import org.apache.ibatis.type.TypeHandler;
3131
import org.apache.ibatis.type.TypeHandlerRegistry;
3232

@@ -42,12 +42,10 @@ public void processBefore(Executor executor, MappedStatement ms, Statement stmt,
4242

4343
@Override
4444
public void processAfter(Executor executor, MappedStatement ms, Statement stmt, Object parameter) {
45-
List<Object> parameters = new ArrayList<Object>();
46-
parameters.add(parameter);
47-
processBatch(ms, stmt, parameters);
45+
processBatch(ms, stmt, getParameters(parameter));
4846
}
4947

50-
public void processBatch(MappedStatement ms, Statement stmt, List<Object> parameters) {
48+
public void processBatch(MappedStatement ms, Statement stmt, Collection<Object> parameters) {
5149
ResultSet rs = null;
5250
try {
5351
rs = stmt.getGeneratedKeys();
@@ -64,7 +62,7 @@ public void processBatch(MappedStatement ms, Statement stmt, List<Object> parame
6462
}
6563
final MetaObject metaParam = configuration.newMetaObject(parameter);
6664
if (typeHandlers == null) {
67-
typeHandlers = getTypeHandlers(typeHandlerRegistry, metaParam, keyProperties);
65+
typeHandlers = getTypeHandlers(typeHandlerRegistry, metaParam, keyProperties, rsmd);
6866
}
6967
populateKeys(rs, metaParam, keyProperties, typeHandlers);
7068
}
@@ -82,12 +80,33 @@ public void processBatch(MappedStatement ms, Statement stmt, List<Object> parame
8280
}
8381
}
8482

85-
private TypeHandler<?>[] getTypeHandlers(TypeHandlerRegistry typeHandlerRegistry, MetaObject metaParam, String[] keyProperties) {
83+
private Collection<Object> getParameters(Object parameter) {
84+
Collection<Object> parameters = null;
85+
if (parameter instanceof Collection) {
86+
parameters = (Collection) parameter;
87+
} else if (parameter instanceof Map) {
88+
Map parameterMap = (Map) parameter;
89+
if (parameterMap.containsKey("collection")) {
90+
parameters = (Collection) parameterMap.get("collection");
91+
} else if (parameterMap.containsKey("list")) {
92+
parameters = (List) parameterMap.get("list");
93+
} else if (parameterMap.containsKey("array")) {
94+
parameters = Arrays.asList((Object[]) parameterMap.get("array"));
95+
}
96+
}
97+
if (parameters == null) {
98+
parameters = new ArrayList<Object>();
99+
parameters.add(parameter);
100+
}
101+
return parameters;
102+
}
103+
104+
private TypeHandler<?>[] getTypeHandlers(TypeHandlerRegistry typeHandlerRegistry, MetaObject metaParam, String[] keyProperties, ResultSetMetaData rsmd) throws SQLException {
86105
TypeHandler<?>[] typeHandlers = new TypeHandler<?>[keyProperties.length];
87106
for (int i = 0; i < keyProperties.length; i++) {
88107
if (metaParam.hasSetter(keyProperties[i])) {
89108
Class<?> keyPropertyType = metaParam.getSetterType(keyProperties[i]);
90-
TypeHandler<?> th = typeHandlerRegistry.getTypeHandler(keyPropertyType);
109+
TypeHandler<?> th = typeHandlerRegistry.getTypeHandler(keyPropertyType, JdbcType.forCode(rsmd.getColumnType(i + 1)));
91110
typeHandlers[i] = th;
92111
}
93112
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright 2009-2015 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.keygen;
17+
18+
/**
19+
* @author liuzh
20+
*/
21+
public class Country {
22+
private Integer id;
23+
private String countryname;
24+
private String countrycode;
25+
26+
public Country() {
27+
}
28+
29+
public Country(String countryname, String countrycode) {
30+
this.countryname = countryname;
31+
this.countrycode = countrycode;
32+
}
33+
34+
public Country(Integer id, String countryname, String countrycode) {
35+
this.id = id;
36+
this.countryname = countryname;
37+
this.countrycode = countrycode;
38+
}
39+
40+
public Integer getId() {
41+
return id;
42+
}
43+
44+
public void setId(Integer id) {
45+
this.id = id;
46+
}
47+
48+
public String getCountryname() {
49+
return countryname;
50+
}
51+
52+
public void setCountryname(String countryname) {
53+
this.countryname = countryname;
54+
}
55+
56+
public String getCountrycode() {
57+
return countrycode;
58+
}
59+
60+
public void setCountrycode(String countrycode) {
61+
this.countrycode = countrycode;
62+
}
63+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright 2009-2015 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.keygen;
17+
18+
import java.util.List;
19+
20+
public interface CountryMapper {
21+
22+
int insertList(List<Country> countries);
23+
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2009-2012 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+
<mapper namespace="org.apache.ibatis.submitted.keygen.CountryMapper" >
21+
<insert id="insertList" parameterType="org.apache.ibatis.submitted.keygen.Country" useGeneratedKeys="true" keyProperty="id">
22+
insert into country (countryname,countrycode)
23+
values
24+
<foreach collection="list" separator="," item="country">
25+
(#{country.countryname},#{country.countrycode})
26+
</foreach>
27+
</insert>
28+
</mapper>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 country IF EXISTS;
18+
19+
CREATE TABLE country (
20+
Id int IDENTITY,
21+
countryname varchar(255) DEFAULT NULL,
22+
countrycode varchar(255) DEFAULT NULL,
23+
);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Copyright 2009-2015 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.keygen;
17+
18+
import static org.junit.Assert.*;
19+
20+
import java.io.Reader;
21+
import java.sql.Connection;
22+
import java.util.ArrayList;
23+
import java.util.List;
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+
/**
34+
* @author liuzh
35+
*/
36+
public class Jdbc3KeyGeneratorTest {
37+
38+
private static SqlSessionFactory sqlSessionFactory;
39+
40+
@BeforeClass
41+
public static void setUp() throws Exception {
42+
// create an SqlSessionFactory
43+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/keygen/MapperConfig.xml");
44+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
45+
reader.close();
46+
47+
// populate in-memory database
48+
SqlSession session = sqlSessionFactory.openSession();
49+
Connection conn = session.getConnection();
50+
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/keygen/CreateDB.sql");
51+
ScriptRunner runner = new ScriptRunner(conn);
52+
runner.setLogWriter(null);
53+
runner.runScript(reader);
54+
reader.close();
55+
session.close();
56+
}
57+
58+
@Test
59+
public void shouldInsertListAndRetrieveId() throws Exception {
60+
SqlSession sqlSession = sqlSessionFactory.openSession();
61+
try {
62+
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
63+
List<Country> countries = new ArrayList<Country>();
64+
countries.add(new Country("China", "CN"));
65+
countries.add(new Country("United Kiongdom", "GB"));
66+
countries.add(new Country("United States of America", "US"));
67+
mapper.insertList(countries);
68+
for (Country country : countries) {
69+
assertNotNull(country.getId());
70+
}
71+
} finally {
72+
sqlSession.rollback();
73+
sqlSession.close();
74+
}
75+
}
76+
}
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-2012 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+
<configuration>
21+
22+
<environments default="development">
23+
<environment id="development">
24+
<transactionManager type="JDBC">
25+
<property name="" value="" />
26+
</transactionManager>
27+
<dataSource type="UNPOOLED">
28+
<property name="driver" value="org.hsqldb.jdbcDriver" />
29+
<property name="url" value="jdbc:hsqldb:mem:keygen" />
30+
<property name="username" value="sa" />
31+
</dataSource>
32+
</environment>
33+
</environments>
34+
35+
<mappers>
36+
<mapper resource="org/apache/ibatis/submitted/keygen/CountryMapper.xml" />
37+
</mappers>
38+
</configuration>

0 commit comments

Comments
 (0)