日期:2014-05-18 浏览次数:20946 次
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();
}
});
//手动提交
&nb