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 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(); } }); }
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="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.
相关推荐
NULL 博文链接:https://rayfuxk.iteye.com/blog/2286519
strut2+spring+springjdbctemplate做的简易登录系统
Druid数据库连接池的SpringJDBCTemplate所需的jar包,Druid数据库连接池的SpringJDBCTemplate所需的jar包,Druid数据库连接池的SpringJDBCTemplate所需的jar包,Druid数据库连接池的SpringJDBCTemplate所需的jar包,...
一个非常简单基于注解的Spring JdbcTemplate,供初学者参考用。
Spring JDBCTemplate连接池jar包
Spring JdbcTemplate调用Oracle存储过程输出游标结果集实现增删改查
模仿spring jdbcTemplate的粗略实现,只有很小的参考价值,如果是java初学者可以使用这个封装好的工具进行数据库操作,只需要在db.properties里配置好driver,url等信息
spring-jdbcTemplate实例工程
Spring JdbcTemplate的操作,包括接口,BaseDao,log4J配置文件,主要为oracle数据库操作,很多特殊方法只对oracle有效
SpringJdbcTemplate封装工具包,包括规范model格式接口,封装SpringJdbcTemplate,实现分页,自适应多种数据库
1.Spring4前 spring-jdbc包是独立的,4以后spring-jdbc 就已经没有了
spring JdbcTemplate query方法使用示例,欢迎下载借鉴
JdbcTemplate api 下载 Spring
使用Spring的JdbcTemplate调用Oracle的存储过程
使用Spring的JdbcTemplate实现分页功能
spring jdbctemplate组件的简单实例。可以直接运行该实例来学习spring的jdbctemplate。处于初学者,或开发互联网性能要求较高的比较有料。
SSH笔记-Spring JdbcTemplate,使用JdbcTemplate对数据库进行操作,使用具名参数和JDBC模板,简化操作
运用Spring中关于JDBC的一个辅助类(JDBC Template),进行curd操作
本篇文档主要介绍Spring JDBCTemplate知识。亦可作为Spring JDBCTemplate的API文档。文档中的内容,对于学习Spring JDBCTemplate有很大的帮助,也可帮助他人学习Spring Boot中的JDBCTemplate知识。
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSet; import org.springframework.jdbc.support.rowset.SqlRowSet; import org....