class Main {
public static void main(String args[]) throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("context.xml", Main.class);
DataSource dataSource = (DataSource) ac.getBean("dataSource");
// DataSource mysqlDataSource = (DataSource) ac.getBean("mysqlDataSource");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
final int count = 2000;
final List<String> firstNames = new ArrayList<String>(count);
final List<String> lastNames = new ArrayList<String>(count);
for (int i = 0; i < count; i++) {
firstNames.add("First Name " + i);
lastNames.add("Last Name " + i);
}
jdbcTemplate
.batchUpdate(
"insert into customer (id, first_name, last_name, last_login, comments)
values (?, ?, ?, ?, ?)",
new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setLong(1, i + 10);
ps.setString(2, firstNames.get(i));
ps.setString(3, lastNames.get(i));
ps.setNull(4, Types.TIMESTAMP);
ps.setNull(5, Types.CLOB);
}
public int getBatchSize() {
return count;
}
});
}
}
方法二:
Transaction tx=session.beginSession();
String HQL=“delete STUDENT”;
Query query=session.createQuery(HQL);
int size=query.executeUpdate();
tx.commit();
本文介绍了使用 JdbcTemplate 进行批量数据插入的方法,并展示了如何通过 Hibernate Session 执行批量删除操作。具体包括设置数据源、构造批量 SQL 语句及执行批处理等步骤。
6161

被折叠的 条评论
为什么被折叠?



