首页 技术 正文
技术 2022年11月16日
0 收藏 568 点赞 4,490 浏览 2313 个字

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.

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,360
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,853