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的更多相关文章

  1. Spring JdbcTemplate batchUpdate() example

    In some cases, you may required to insert a batch of records into database in one shot. If you call ...

  2. Spring JdbcTemplate batchUpdate() 实例

    在某些情况下,可能需要将一批记录插入到数据库中.如果你对每条记录调用一个插件的方法,SQL语句将被重复编译,造成系统缓慢进行. 在上述情况下,你可以使用 JdbcTemplate BATCHUPDAT ...

  3. Spring SimpleJdbcTemplate Querying examples

    Here are few examples to show how to use SimpleJdbcTemplate query() methods to query or extract data ...

  4. Spring SimpleJdbcTemplate查询示例

    这里有几个例子来说明如何使用SimpleJdbcTemplate query()方法来查询或从数据库中提取数据.在 JdbcTemplate query() 方法,需要手动转换返回的结果转换为一个目标 ...

  5. 一个spring jdbc实例

    一.使用示例 (1)springJdbcContext.xml <?xml version="1.0" encoding="UTF-8"?> < ...

  6. 开涛spring3(7.4) - 对JDBC的支持 之 7.4 Spring提供的其它帮助

    7.4  Spring提供的其它帮助 7.4.1  SimpleJdbc方式 Spring JDBC抽象框架提供SimpleJdbcInsert和SimpleJdbcCall类,这两个类通过利用JDB ...

  7. ref:Spring JdbcTemplate+JdbcDaoSupport实例

    ref:https://www.yiibai.com/spring/spring-jdbctemplate-jdbcdaosupport-examples.html 在Spring JDBC开发中,可 ...

  8. spring3: 对JDBC的支持 之 Spring提供的其它帮助 SimpleJdbcInsert/SimpleJdbcCall/SqlUpdate/JdbcTemplate 生成主键/批量处理

    7.4  Spring提供的其它帮助 7.4.1  SimpleJdbc方式 Spring JDBC抽象框架提供SimpleJdbcInsert和SimpleJdbcCall类,这两个类通过利用JDB ...

  9. Spring的学习(IoC,AOP)等

    下面这个系列是非常好的例子: http://www.yiibai.com/spring/spring-3-hello-world-example.html 正在看,把一些基础夯实. IoC可以从下面一 ...

随机推荐

  1. Android权限安全(9)Android权限特点及权限管理服务AppOps Service

    Android权限特点 权限管理服务AppOps Service 图中元素介绍: Ignore 是不提示的,Allow 是允许,Reject 是拒绝 Client是一个使用sms 的应用, AppOp ...

  2. “LC.exe已退出,代码为-1错误”解决办法

    有的时间,在项目中编辑运行以后,竟然出错了,错误提示就是: “LC.exe”已退出,代码为 -1. 具体解决方法如下: 因为证书的原因,把项目中“properties”目录下的“license.lic ...

  3. Toad创建DBLINKsop

    Toad创建DBLINKsop 1.创建服务: 点击“测试”,出现如下测试窗口后点击更改登录,用户名和密码数据目标主机用户名.密码; 出现如下窗口后,点击“关闭”,然后点击“完成”即可; 2.创建db ...

  4. bzoj1064

    很巧妙的题 首先有几种情况 1. 有环 2.两点间有多条路径 3.其他 3.显然最简单,最小是3,最大是每个弱联通块中最长链 2.显然,两点间两条路径的差是答案的倍数 1.出现环,那答案一定是其约数, ...

  5. linq 之左连接

    List<ArticleModel> articleList = articleRepository.GetAllArticle(); List<UsersModel> use ...

  6. Material Design 设计--阴影的重要性

    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_cont ...

  7. 使用AngularJS 进行Hybrid App 开发已经有一年多时间了,这里做一个总结

    一.AngularJS 初始化加载流程 1.浏览器载入HTML,然后把它解析成DOM.2.浏览器载入angular.js脚本.3.AngularJS等到DOMContentLoaded事件触发.4.A ...

  8. 使用mp4v2将H264+AAC合成mp4文件

    录制程序要添加新功能:录制CMMB电视节目,我们的板卡发送出来的是RTP流(H264视频和AAC音频),录制程序要做的工作是: (1)接收并解析RTP包,分离出H264和AAC数据流: (2)将H26 ...

  9. Oracle数据库启动时:ORA-00119: invalid specification for system parameter LOCAL_LISTENER; ORA-00132错误解决

    问题描述: 1. em打开中提示 https://localhost:1158/em/console/database/instance/repDown?target=orclweng&typ ...

  10. ThinkAndroid是简洁,快速的进行Android应用程序的框架

    ThinkAndroid简介ThinkAndroid是一个免费的开源的.简易的.遵循Apache2开源协议发布的Android开发框架,其开发宗旨是简单.快速的进行Android应用程序的开发,包含A ...