日期:2014-05-16  浏览次数:20649 次

Spring JDBC: Introduction to JdbcTemplate (IV)--Batch Operations

1. As we know, it is meaningless to execute batch query.

? ? There is only batchUpdate provided in JdbcTemplate.

/**
 * @param sql defining an array of SQL statements that will be executed.
 */
public int[] batchUpdate(final String[] sql);

/**
 * @param sql defining PreparedStatement that will be reused.
 * @param pss object to set parameters on the PreparedStatement
 */
public int[] batchUpdate(String sql, final BatchPreparedStatementSetter pss);

/**
 * @param sql the SQL statement to execute
 * @param batchArgs the List of Object arrays containing the batch of arguments for the query
 */
public int[] batchUpdate(String sql, List<Object[]> batchArgs);

/**
 * @param sql the SQL statement to execute.
 * @param batchArgs the List of Object arrays containing the batch of arguments for the query
 * @param argTypes SQL types of the arguments
 */
public int[] batchUpdate(String sql, List<Object[]> batchArgs, int[] argTypes);

/**
 * @param sql the SQL statement to execute.
 * @param batchArgs the List of Object arrays containing the batch of arguments for the query
 * @param batchSize batch size
 * @param pss ParameterizedPreparedStatementSetter to use
 */
public <T> int[][] batchUpdate(String sql, final Collection<T> batchArgs, final int batchSize, final ParameterizedPreparedStatementSetter<T> pss);

Example:

package edu.xmu.jdbc.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import edu.xmu.jdbc.bean.Student;

public class BatchDao extends JdbcDaoSupport {

    public int[] batchExecution() {
	JdbcTemplate jdbcTemplate = getJdbcTemplate();

	String sql = "update student set name='aaa' where id=1";
	String sql2 = "update student set name='bbb' where id=2";
	String sql3 = "update student set name='ccc' where id=3";
	final String[] sqls = new String[] { sql, sql2, sql3 };

	return jdbcTemplate.batchUpdate(sqls);
    }

    public int[] batchCreate(final List<Student> studentList) {
	JdbcTemplate jdbcTemplate = getJdbcTemplate();
	String sql = "insert into student(name, age) values(?, ?)";

	int[] updateCounts = jdbcTemplate.batchUpdate(sql,
		new BatchPreparedStatementSetter() {
		    public void setValues(PreparedStatement ps, int i)
			    throws SQLException {
			Student student = studentList.get(i);
			ps.setString(1, student.getName());
			ps.setInt(2, student.getAge());
		    }

		    public int getBatchSize() {
			return studentList.size();
		    }
		});

	return updateCounts;
    }

    public int[] batchCreate2(final List<Student> studentList) {
	JdbcTemplate jdbcTemplate = getJdbcTemplate();
	String sql = "insert into student(name, age) values(?, ?)";

	List<Object[]> batchArgs = new ArrayList<Object[]>();

	for (Student student : studentList) {
	    String name = student.getName();
	    int age = student.getAge();

	    Object[] objects = new Object[] { name, age };
	    batchArgs.add(objects);
	}

	return jdbcTemplate.batchUpdate(sql, batchArgs);
    }

    public int[] batchCreate3(final List<Student> studentList) {
	JdbcTemplate jdbcTemplate = getJdbcTemplate();
	String sql = "insert into student(name, age) values(?, ?)";

	List<Object[]> batchArgs = new ArrayList<Object[]>();

	for (Student student : studentList) {
	    String name = student.getName();
	    int age = student.getAge();

	    Object[] objects = new Object[] { name, age };
	    batchArgs.add(objects);
	}

	re