Spring jdbc 批量更新数据
使用spring封装的jdbc批量进行数据库更新操作,代码如下:
Person类
package com.kedacom.spring;
public class Person {
public int id;
public String name;
public int age;
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public void insertPersons(final List<Person> list)
throws CannotGetJdbcConnectionException, SQLException {
super.getJdbcTemplate().batchUpdate(INSERT_INTO_PERSON,
new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement preparedStatement,
int i) throws SQLException {
preparedStatement.setObject(1, list.get(i).getName());
preparedStatement.setObject(2, list.get(i).getAge());
}
@Override
public int getBatchSize() {
return list.Size();
}
});
}
public void test() {
List<Person> persons = new ArrayList<Person>();
// 生成1000个Person对象
for (int i = 0; i < 1000; i++) {
Person person = new Person();
person.setName("NO" + String.valueOf(i));
person.setAge(i);
persons.add(person);
}
long start = System.currentTimeMillis();
this.insertPersons(persons);
long end = System.currentTimeMillis();
System.out.println("所用时长" + (end - start) + "毫秒");
}
public void insertPersons(final List<Person> list)
throws CannotGetJdbcConnectionException, SQLException {
//设置不自动提交
getConnection().setAutoCommit(false);
super.getJdbcTemplate().batchUpdate(INSERT_INTO_PERSON,
new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement preparedStatement,
int i) throws SQLException {
preparedStatement.setObject(1, list.get(i).getName());
preparedStatement.setObject(2, list.get(i).getAge());
}
@Override
public int getBatchSize() {
return list.Size();
}
});
//手动提交
getConnection().commit();
getConnection().setAutoCommit(true);
}
public int[] batchUpdate(String sql, final BatchPreparedStatementSetter pss) throws DataAccessException {
if (logger.isDebugEnabled()) {
logger.debug("Executing SQL batch update [" + sql + "]");
}
return execute(sql, new PreparedStatementCallback<int[]>() {
public int[] doInPreparedStatement(PreparedStatement ps) throws SQLException {
try {
int batchSize = pss.getBatchSize();
InterruptibleBatchPreparedStatementSetter ipss =
(pss instanceof InterruptibleBatchPreparedStatementSetter ?
(InterruptibleBatchPreparedStatementSetter) pss : null);
if (JdbcUtils.supportsBatchUpdates(ps.getConnection())) {
for (int i = 0; i < batchSize; i++) {
pss.setValues(ps, i);
if (ipss != null && ipss.isBatchExhausted(i)) {
break;
}
ps.addBatch();
}
return ps.executeBatch();
}
else {
List<Integer> rowsAffected = new ArrayList<Integer>();
for (int i = 0; i < batchSize; i++) {
pss.setValues(ps, i);
if (ipss != null && ipss.isBatchExhausted(i)) {
break;
}
rowsAffected.add(ps.executeUpdate());
}
int[] rowsAffectedArray = new int[rowsAffected.size()];
for (int i = 0; i < rowsAffectedArray.length; i++) {
rowsAffectedArray[i] = rowsAffected.get(i);
}
return rowsAffectedArray;
}
}
finally {
if (pss instanceof ParameterDisposer) {
((ParameterDisposer) pss).cleanupParameters();
}
}
}
});
}
public static String valueOf(int i) {
return Integer.toString(i);
}
public static String toString(int i) {
if (i == Integer.MIN_VALUE)
return "-2147483648";
int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
char[] buf = new char[size];
getChars(i, size, buf);
return new String(buf, true);
}