Spring JdbcTemplate batchUpdate() 实例

在某些情况下,可能需要将一批记录插入到数据库中。如果你对每条记录调用一个插件的方法,SQL语句将被重复编译,造成系统缓慢进行。
在上述情况下,你可以使用 JdbcTemplate BATCHUPDATE()方法来执行批量插入操作。用这种方法,该语句只被编译一次,执行多次。
详见 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.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>
执行它

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);
  
}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,399评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,054评论 6 342
  • Spring 技术笔记Day 1 预热知识一、 基本术语Blob类型,二进制对象Object Graph:对象图...
    OchardBird阅读 4,563评论 0 2
  • 这里有几个例子向您展示如何使用JdbcTemplate的query()方法来查询或从数据库提取数据。整个项目的目录...
    呵呵飘过阅读 7,751评论 0 0
  • 在使用Go开发web项目的过程中, 数据库读写操作与JSON格式的输入输出是两块最基础的模块, Go的标准库已经帮...
    qgymje阅读 3,690评论 0 2