绝大多数JDBC驱动针对批量调用相同的prepared statement对象提供了性能提升。通过将这些更新操作封装到一个批量操作中,可以大量减少与数据库的操作频繁度。 本章节将详细描述使用JdbcTemplate或者SimpleJdbcTemplate进行批量操作的流程。
1、使用JdbcTemplate进行批量操作
JdbcTemplate的批量操作特性需要实现特定的接口BatchPreparedStatementSetter
来 进行的, 通过实现这个接口,并将其传入batchUpdate
方法进行调用。 这个接口有两个方法需要实现。一个叫做getBatchSize
来提供当前需要批 量操作的数量。另外一个方法是setValues
允许你为prepared statement设置参数。这个方法将在整个过程中被调用的次数,则取决于你在getBatchSize
中所指定的大小。 下面的示例展示了根据传入的list参数更新actor表,而传入的list同时作为批量操作的参数。
- public class JdbcActorDao implements ActorDao {
- private JdbcTemplate jdbcTemplate;
- public void setDataSource(DataSource dataSource) {
- this.jdbcTemplate = new JdbcTemplate(dataSource);
- }
- public int[] batchUpdate(final List actors) {
- int[] updateCounts = jdbcTemplate.batchUpdate(
- "update t_actor set first_name = ?, last_name = ? where id = ?",
- new BatchPreparedStatementSetter() {
- public void setValues(PreparedStatement ps, int i) throws SQLException {
- ps.setString(1, ((Actor)actors.get(i)).getFirstName());
- ps.setString(2, ((Actor)actors.get(i)).getLastName());
- ps.setLong(3, ((Actor)actors.get(i)).getId().longValue());
- }
- public int getBatchSize() {
- return actors.size();
- }
- } );
- return updateCounts;
- }
- // ... additional methods
- }
如果你是通过读取文件进行批量操作,那么你可能需要一个特定的批量操作的数量,不过最后一次的批量操作,你可能没有那么多数量的记录。 在这种情况下,你可以实现InterruptibleBatchPreparedStatementSetter
接 口,从而允许你在某些情况中断批量操作,isBatchExhausted
方法允许你指定一个终止批量操作的信号量。
SimpleJdbcTemplate
类提供了另外一种批量操作的方式。无需实现 一个特定的接口,你只需要提供所有在调用过程中要用到的参数,框架会遍历这些参数值,并使用内置的prepared statement类进行批量操作。API将根据你是否使用命名参数而有所不同。对于使用命名参数的情况,你需要提供一个SqlParameterSource
的数组, 其中的每个元素将将作为批量操作的参数。 你可以使用SqlParameterSource.createBatch
方法, 通过传入一个JavaBean的数组或者一个包含了参数键值对的Map数组来创建这个数组。
下面的示例展示了使用命名参数进行批量更新的方法:
- public class JdbcActorDao implements ActorDao {
- private SimpleJdbcTemplate simpleJdbcTemplate;
- public void setDataSource(DataSource dataSource) {
- this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
- }
- public int[] batchUpdate(final List<Actor> actors) {
- SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(actors.toArray());
- int[] updateCounts = simpleJdbcTemplate.batchUpdate(
- "update t_actor set first_name = :firstName, last_name = :lastName where id = :id",
- batch);
- return updateCounts;
- }
- // ... additional methods
- }
对于使用传统的“?”作为参数占位符的情况,你可以传入一个List,包含了所有需要进行批量更新的对象。这样的对象数组必须与每个SQL Statement的占位符以及他们在SQL Statement中出现的位置一一对应。
下面是同样的例子,使用的传统的“?”作为参数占位符:
- public class JdbcActorDao implements ActorDao {
- private SimpleJdbcTemplate simpleJdbcTemplate;
- public void setDataSource(DataSource dataSource) {
- this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
- }
- public int[] batchUpdate(final List<Actor> actors) {
- List<Object[]> batch = new ArrayList<Object[]>();
- for (Actor actor : actors) {
- Object[] values = new Object[] {
- actor.getFirstName(),
- actor.getLastName(),
- actor.getId()};
- batch.add(values);
- }
- int[] updateCounts = simpleJdbcTemplate.batchUpdate(
- "update t_actor set first_name = ?, last_name = ? where id = ?",
- batch);
- return updateCounts;
- }
- // ... additional methods
- }
所有的批量更新的方法都会返回一组int的数组,表示在整个操作过程中有多少记录被批量更新。 这个数量是由JDBC驱动所返回的,有时这个返回并不可靠,尤其对于某些JDBC驱动只是简单的返回-2作为返回值。