Spring JdbcTemplate batchUpdate() example
来源:互联网 发布:网络摄像头 编辑:程序博客网 时间:2024/06/05 01:12
In some cases, you may required to insert a batch of records into database in one shot. If you call a single insert method for every record, the SQL statement will be compiled repeatedly and causing your system slow to perform.
In above case, you can use JdbcTemplate
batchUpdate()
method to perform the batch insert operations. With this method, the statement is compiled only once and executed multiple times.
See batchUpdate()
example in JdbcTemplate
class.
//insert batch examplepublic 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(); } });}
Alternatively, you can execute the SQL directly.
//insert batch example with SQLpublic void insertBatchSQL(final String sql){ getJdbcTemplate().batchUpdate(new String[]{sql});}
Spring’s bean configuration file
<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.mkyong.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/mkyongjava" /> <property name="username" value="root" /> <property name="password" value="password" /> </bean></beans>
Run it
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 customerDAO = (CustomerDAO) context.getBean("customerDAO"); Customer customer1 = new Customer(1, "mkyong1",21); Customer customer3 = new Customer(2, "mkyong2",22); Customer customer2 = new Customer(3, "mkyong3",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); }}
In this example, you are inserted three customers
’ records and update all customer
’s name in batch.
0 0
- Spring JdbcTemplate batchUpdate() example
- Spring JdbcTemplate batchUpdate() example
- Spring SimpleJdbcTemplate batchUpdate() example
- Spring SimpleJdbcTemplate batchUpdate() example
- Spring批量更新JdbcTemplate.batchUpdate()
- Spring批量更新JdbcTemplate.batchUpdate()
- Spring批量更新JdbcTemplate.batchUpdate()
- Spring批量更新JdbcTemplate.batchUpdate()
- Spring JDBCtemplate.batchupdate 批量跟新数据 实例
- Spring—— 源码分析之 JDBCTemplate.batchUpdate()
- jdbctemplate batchUpdate方法。
- jdbctemplate batchupdate 的事务管理
- 关于Spring的JdbcTemplate批量更新batchUpdate()方法的返回值为-2的异常
- Spring JdbcTemplate的batchUpdate中,没有看到conn.setAutoCommit(false)的操作
- JdbcTemplate batchUpdate 批量操作加事务
- Java实现 调用JdbcTemplate.batchUpdate方法批量更新
- spring JdbcTemplate
- spring jdbcTemplate
- 用两个栈实现一个队列
- CodeForces #317 (div2) C.Lengthening Sticks (容斥)
- 黑马程序员—学习笔记—接口
- MySQL中/*!代码 ……*/的使用
- Servlet的线程安全问题
- Spring JdbcTemplate batchUpdate() example
- C++基础---string类的operator=/assign
- lua元表理解
- equals和==的区别
- 月之数
- java EE 5,JSP中核心标签库的无法使用
- 存储过程引发的思考
- hdu1233 还是畅通工程 prim
- DesignPattern_Java:Abstract Factory Pattern