В этом уроке мы покажем вам, как использовать пакетное обновление() в классе SimpleJdbcTemplate.
Смотрите batchUpdate() пример в классе SimpleJdbcTemplate.
//insert batch example public void insertBatch(final Listcustomers){ String sql = "INSERT INTO CUSTOMER " + "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)"; List
В качестве альтернативы вы можете выполнить SQL напрямую.
//insert batch example with SQL
public void insertBatchSQL(final String sql){
getJdbcTemplate().batchUpdate(new String[]{sql});
}
Файл конфигурации компонентов Spring
Запустите его
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 customerSimpleDAO =
(CustomerDAO) context.getBean("customerSimpleDAO");
Customer customer1 = new Customer(1, "mkyong1",21);
Customer customer3 = new Customer(2, "mkyong2",22);
Customer customer2 = new Customer(3, "mkyong3",23);
Listcustomers = new ArrayList();
customers.add(customer1);
customers.add(customer2);
customers.add(customer3);
customerSimpleDAO.insertBatch(customers);
String sql = "UPDATE CUSTOMER SET NAME ='BATCHUPDATE'";
customerSimpleDAO.insertBatchSQL(sql);
}
}
В этом примере вы вставляете записи трех клиентов и обновляете все имена клиентов в пакетном режиме.
Скачать Исходный Код
Оригинал: “https://mkyong.com/spring/spring-simplejdbctemplate-batchupdate-example/”