場合によっては、データベースに一括してレコードのバッチを挿入する必要があります。すべてのレコードに対して1つの挿入メソッドを呼び出すと、SQLステートメントは繰り返しコンパイルされ、システムの実行が遅くなります。

上記の場合、JdbcTemplate `batchUpdate()`メソッドを使用してバッチ挿入操作を実行できます。このメソッドでは、ステートメントは1回のみコンパイルされ、複数回実行されます。

JdbcTemplateクラスの `batchUpdate()`の例を参照してください。

…​.//insert batch example
public void insertBatch(final List<Customer> customers){

String sql = "INSERT INTO CUSTOMER " +
  "(CUST__ID, NAME, AGE) VALUES (?, ?, ?)";

getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {

@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
    Customer customer = customers.get(i);
    ps.setLong(1, customer.getCustId());
    ps.setString(2, customer.getName());
    ps.setInt(3, customer.getAge() );
}

    @Override
    public int getBatchSize() {
        return customers.size();
    }
  });
}

あるいは、SQLを直接実行することもできます。

....//insert batch example with SQL
public void insertBatchSQL(final String sql){

    getJdbcTemplate().batchUpdate(new String[]{sql});

}

SpringのBean構成ファイル

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="customerDAO" class="com.mkyong.customer.dao.impl.JdbcCustomerDAO">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mkyongjava"/>
        <property name="username" value="root"/>
        <property name="password" value="password"/>
    </bean>

</beans>

それを実行します

package com.mkyong.common;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mkyong.customer.dao.CustomerDAO;
import com.mkyong.customer.model.Customer;

public class App
{
    public static void main( String[]args )
    {
        ApplicationContext context =
            new ClassPathXmlApplicationContext("Spring-Customer.xml");

        CustomerDAO customerDAO = (CustomerDAO) context.getBean("customerDAO");
        Customer customer1 = new Customer(1, "mkyong1",21);
        Customer customer3 = new Customer(2, "mkyong2",22);
        Customer customer2 = new Customer(3, "mkyong3",23);

        List<Customer>customers = new ArrayList<Customer>();
        customers.add(customer1);
        customers.add(customer2);
        customers.add(customer3);

        customerDAO.insertBatch(customers);

        String sql = "UPDATE CUSTOMER SET NAME ='BATCHUPDATE'";
        customerDAO.insertBatchSQL(sql);

    }
}

この例では、3つの顧客のレコードが挿入され、すべての顧客の名前が一括して更新されます。

ソースコードをダウンロードする

ダウンロードする –

Spring-JdbcTemplate-batchUpdate-Example.zip

(15 KB)