Spring JdbcTemplate batchUpdate() 實(shí)例

在某些情況下,可能需要將一批記錄插入到數(shù)據(jù)庫中。如果你對每條記錄調(diào)用一個(gè)插件的方法,SQL語句將被重復(fù)編譯,造成系統(tǒng)緩慢進(jìn)行。
在上述情況下,你可以使用 JdbcTemplate BATCHUPDATE()方法來執(zhí)行批量插入操作。用這種方法,該語句只被編譯一次,執(zhí)行多次。
詳見 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();
}

});
}
或者,可以直接執(zhí)行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.yiibai.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/yiibaijava" />
    <property name="username" value="root" />
    <property name="password" value="password" />
</bean>

</beans>
執(zhí)行它

package com.yiibai.common;

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

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yiibai.customer.dao.CustomerDAO;
import com.yiibai.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, "yiibai1",21);
    Customer customer3 = new Customer(2, "yiibai2",22);
    Customer customer2 = new Customer(3, "yiibai3",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);
  
}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,273評論 6 342
  • Spring 技術(shù)筆記Day 1 預(yù)熱知識一、 基本術(shù)語Blob類型,二進(jìn)制對象Object Graph:對象圖...
    OchardBird閱讀 1,078評論 0 2
  • 這里有幾個(gè)例子向您展示如何使用JdbcTemplate的query()方法來查詢或從數(shù)據(jù)庫提取數(shù)據(jù)。整個(gè)項(xiàng)目的目錄...
    呵呵飄過閱讀 2,877評論 0 0
  • 在使用Go開發(fā)web項(xiàng)目的過程中, 數(shù)據(jù)庫讀寫操作與JSON格式的輸入輸出是兩塊最基礎(chǔ)的模塊, Go的標(biāo)準(zhǔn)庫已經(jīng)幫...
    qgymje閱讀 659評論 0 2

友情鏈接更多精彩內(nèi)容