Spring SimpleJdbcTemplate batchUpdate() example
In this tutorial, we show you how to use batchUpdate()
in SimpleJdbcTemplate
class.
See batchUpdate()
example in SimpleJdbcTemplate
class.
//insert batch example
public void insertBatch(final List<Customer> customers){
String sql = "INSERT INTO CUSTOMER " +
"(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
List<Object[]> parameters = new ArrayList<Object[]>();
for (Customer cust : customers) {
parameters.add(new Object[] {cust.getCustId(),
cust.getName(), cust.getAge()}
);
}
getSimpleJdbcTemplate().batchUpdate(sql, parameters);
}
Alternatively, you can execute the SQL directly.
//insert batch example with SQL
public 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="customerSimpleDAO"
class="com.mkyong.customer.dao.impl.SimpleJdbcCustomerDAO">
<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 customerSimpleDAO =
(CustomerDAO) context.getBean("customerSimpleDAO");
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);
customerSimpleDAO.insertBatch(customers);
String sql = "UPDATE CUSTOMER SET NAME ='BATCHUPDATE'";
customerSimpleDAO.insertBatchSQL(sql);
}
}
In this example, you are inserted three customers
’ records and update all customer
’s name in batch.
Spring SimpleJdbcTemplate batchUpdate() example的更多相关文章
- Spring JdbcTemplate batchUpdate() example
In some cases, you may required to insert a batch of records into database in one shot. If you call ...
- Spring JdbcTemplate batchUpdate() 实例
在某些情况下,可能需要将一批记录插入到数据库中.如果你对每条记录调用一个插件的方法,SQL语句将被重复编译,造成系统缓慢进行. 在上述情况下,你可以使用 JdbcTemplate BATCHUPDAT ...
- Spring SimpleJdbcTemplate Querying examples
Here are few examples to show how to use SimpleJdbcTemplate query() methods to query or extract data ...
- Spring SimpleJdbcTemplate查询示例
这里有几个例子来说明如何使用SimpleJdbcTemplate query()方法来查询或从数据库中提取数据.在 JdbcTemplate query() 方法,需要手动转换返回的结果转换为一个目标 ...
- 一个spring jdbc实例
一.使用示例 (1)springJdbcContext.xml <?xml version="1.0" encoding="UTF-8"?> < ...
- 开涛spring3(7.4) - 对JDBC的支持 之 7.4 Spring提供的其它帮助
7.4 Spring提供的其它帮助 7.4.1 SimpleJdbc方式 Spring JDBC抽象框架提供SimpleJdbcInsert和SimpleJdbcCall类,这两个类通过利用JDB ...
- ref:Spring JdbcTemplate+JdbcDaoSupport实例
ref:https://www.yiibai.com/spring/spring-jdbctemplate-jdbcdaosupport-examples.html 在Spring JDBC开发中,可 ...
- spring3: 对JDBC的支持 之 Spring提供的其它帮助 SimpleJdbcInsert/SimpleJdbcCall/SqlUpdate/JdbcTemplate 生成主键/批量处理
7.4 Spring提供的其它帮助 7.4.1 SimpleJdbc方式 Spring JDBC抽象框架提供SimpleJdbcInsert和SimpleJdbcCall类,这两个类通过利用JDB ...
- Spring的学习(IoC,AOP)等
下面这个系列是非常好的例子: http://www.yiibai.com/spring/spring-3-hello-world-example.html 正在看,把一些基础夯实. IoC可以从下面一 ...
随机推荐
- android多分辨率多屏幕密度下UI适配方案
相关概念 分辨率:整个屏幕的像素数目,为了表示方便一般用屏幕的像素宽度(水平像素数目)乘以像素高度表示,形如1280x720,反之分辨率为1280x720的屏幕,像素宽度不一定为1280 屏幕密度:表 ...
- Math.sqrt
java.lang.Math.sqrt(double a) 返回正确舍入的一个double值的正平方根.特殊情况: 如果参数是NaN或小于为零,那么结果是NaN. 如果参数是正无穷大,那么结果为正无穷 ...
- MySql和Hibernate中关于cascade的用法
数据库里的cascade的用法,Mysql和Hibernate里面是不相同. 在数据库里,进行增加.修改.删除记录的时候,经常会涉及到父子关系的表. 例如:有省份表和城市表,其中城市表有一个外键pro ...
- Asp.Net生成RSS方法
一.RSS简介 什么是RSS? RSS是一种网页内容联合格式(web content sydication format). 它的名字是Really Simple Syndication的缩写. RS ...
- 8款PHP调试工具
Web 开发并不是一项轻松的任务,有超级多服务端脚本语言提供给开发者,但是当前 PHP 因为具有额外的一些强大的功能而越来越流行.PHP 是最强大的服务端脚本语言之一,同时也是 web 开发者和设计者 ...
- tkprof 解释
使用 tkprof 工具 tkprof orcl_ora_3048_安庆怀宁.trc 安徽安庆怀宁.txt sys=no aggregate=yes sys=no waits=yes sort=fc ...
- 实现一个基于FTP协议的程序——文件上传下载器(十三)
此为一个系列,后续会把内容补上...
- SQL优化34条
我们要做到不但会写SQL,还要做到写出性能优良的SQL,以下为笔者学习.摘录.并汇总部分资料与大家分享! (1) 选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE 的解析 ...
- CF 577B Modulo Sum
题意:给一个长度为n的正整数序列,问能不能找到一个不连续的子序列的和可以被m整除. 解法:抽屉原理+dp.首先当m<n时一定是有答案的,因为根据抽屉原理,当得到这个序列的n个前缀和%m时,一定会 ...
- C/C++面试小知识点
1.static有什么用途. 解答: 在函数体中,一个被声明为静态的变量在这一函数被调用过程中维持其值不变. 在模块内(但在函数体外),一个被声明为静态的变量可以被模块内所有函数访问,但不能被模块外其 ...