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);
      
    }
}
在本例中,插入三条顾客的记录,并批量更新所有客户的名字。
 
下载代码 –  http://pan.baidu.com/s/1jGTXfgi

本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/p/6367566.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值