Beispiel für Spring SimpleJdbcTemplate batchUpdate ()
In diesem Tutorial zeigen wir Ihnen, wie SiebatchUpdate() in der SimpleJdbcTemplate-Klasse verwenden.
Siehe das Beispiel vonbatchUpdate()in der SimpleJdbcTemplate-Klasse.
//insert batch example public void insertBatch(final Listcustomers){ String sql = "INSERT INTO CUSTOMER " + "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)"; List
Alternativ können Sie die SQL auch direkt ausführen.
//insert batch example with SQL
public void insertBatchSQL(final String sql){
getJdbcTemplate().batchUpdate(new String[]{sql});
}
Spring’s Bean-Konfigurationsdatei
Starte es
package com.example.common;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.example.customer.dao.CustomerDAO;
import com.example.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, "example1",21);
Customer customer3 = new Customer(2, "example2",22);
Customer customer2 = new Customer(3, "example3",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);
}
}
In diesem Beispiel werden drei Kundendatensätze eingefügt und der gesamte Kundenname wird stapelweise aktualisiert.
Quellcode herunterladen
Laden Sie es herunter -Spring-SimpleJdbcTemplate-batchUpdate-Example.zip (15 KB)