environment
mybatis 3.4.6
Target class
org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator.class
Question
Obtain the self increasing ID of the Batch Insert data, which will be set to the elements of the List collection. How should I obtain self increasing IDs if I use the following SQL:
<insert id="test" parameterType="java. util. List" useGeneratedKeys="true" keyColumn="id">
Insert into test (name) (select menuName as name from menu)
</insert>
public int test(List<Map> maps);
I don’t know how many pieces of data were actually inserted here, and due to performance reasons, I don’t want to query how many pieces of data will be executed under the current conditions. My parameters cannot set specific elements
Bug Position
I checked the source code and found that it is located at line 60 of the org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator.java
file. The code sets the echo ID based on the number of parameter elements, instead of using the ResultSet rs
variable to obtain the number of SQL operation data lines to generate the ID. Can we optimize it here?
public void processBatch(MappedStatement ms, Statement stmt, Collection<Object> parameters) {
ResultSet rs = null;
try {
rs = stmt.getGeneratedKeys();
final Configuration configuration = ms.getConfiguration();
final TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
final String[] keyProperties = ms.getKeyProperties();
final ResultSetMetaData rsmd = rs.getMetaData();
TypeHandler<?>[] typeHandlers = null;
if (keyProperties != null && rsmd.getColumnCount() >= keyProperties.length) {
// 这里循环参数的元素 是否可以改成: 如果 List 参数的元素个数为空,使用 while(rs.next) 往 List 容器中添加元素
for (Object parameter : parameters) {
// there should be one row for each statement (also one for each parameter)
if (!rs.next()) {
break;
}
final MetaObject metaParam = configuration.newMetaObject(parameter);
if (typeHandlers == null) {
typeHandlers = getTypeHandlers(typeHandlerRegistry, metaParam, keyProperties, rsmd);
}
// 设置一行的数据到 List 类型参数的元素中。
populateKeys(rs, metaParam, keyProperties, typeHandlers);
}
}
} catch (Exception e) {
throw new ExecutorException("Error getting generated key or setting result to parameter object. Cause: " + e, e);
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
// ignore
}
}
}
}
environment
mybatis 3.4.6
Target class
org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator.class
Question
Obtain the self increasing ID of the Batch Insert data, which will be set to the elements of the List collection. How should I obtain self increasing IDs if I use the following SQL:
<insert id="test" parameterType="java. util. List" useGeneratedKeys="true" keyColumn="id">
Insert into test (name) (select menuName as name from menu)
</insert>
public int test(List<Map> maps);
I don’t know how many pieces of data were actually inserted here, and due to performance reasons, I don’t want to query how many pieces of data will be executed under the current conditions. My parameters cannot set specific elements
Bug Position
I checked the source code and found that it is located at line 60 of the org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator.java
file. The code sets the echo ID based on the number of parameter elements, instead of using the ResultSet rs
variable to obtain the number of SQL operation data lines to generate the ID. Can we optimize it here?
public void processBatch(MappedStatement ms, Statement stmt, Collection<Object> parameters) {
ResultSet rs = null;
try {
rs = stmt.getGeneratedKeys();
final Configuration configuration = ms.getConfiguration();
final TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
final String[] keyProperties = ms.getKeyProperties();
final ResultSetMetaData rsmd = rs.getMetaData();
TypeHandler<?>[] typeHandlers = null;
if (keyProperties != null && rsmd.getColumnCount() >= keyProperties.length) {
// 这里循环参数的元素 是否可以改成: 如果 List 参数的元素个数为空,使用 while(rs.next) 往 List 容器中添加元素
for (Object parameter : parameters) {
// there should be one row for each statement (also one for each parameter)
if (!rs.next()) {
break;
}
final MetaObject metaParam = configuration.newMetaObject(parameter);
if (typeHandlers == null) {
typeHandlers = getTypeHandlers(typeHandlerRegistry, metaParam, keyProperties, rsmd);
}
// 设置一行的数据到 List 类型参数的元素中。
populateKeys(rs, metaParam, keyProperties, typeHandlers);
}
}
} catch (Exception e) {
throw new ExecutorException("Error getting generated key or setting result to parameter object. Cause: " + e, e);
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
// ignore
}
}
}
}