最近再看开源项目阿里巴巴的数据库同步项目Otter https://github.com/alibaba/otter的时候,看到里面很多时候都使用了JdbcTemplate 来对数据进行相应的数据操作。
这里写一个使用批操作的batchUpdate()的例子,来自spring-jdbctemplate-batchupdate-example
需要使用批操作来操作数据库的数据。
第一种用法:
//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();
}
});
}
第二种用法(当然你可以直接运行sql语句):
//insert batch example with SQL
public void insertBatchSQL(final String sql){
getJdbcTemplate().batchUpdate(new String[]{sql});
}
Spring的配置文件
<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>
运行:
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);
}
}
相关推荐
jdbc批量方法。多个sql语句可以一起执行
1.Spring4前 spring-jdbc包是独立的,4以后spring-jdbc 就已经没有了
NULL 博文链接:https://summerbell.iteye.com/blog/364702
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSet; import org.springframework.jdbc.support.rowset.SqlRowSet; import org....
spring中使用JdbcTemplate操作数据库crud,一图详解(脑图)
使用spring+springmvc+jdbcTemplate 数据库使用orcal、redis 完成一个webdemo
spring JdbcTemplate query方法使用示例,欢迎下载借鉴
strut2+spring+springjdbctemplate做的简易登录系统
15熟悉集成JdbcTemplate.md
spring配置事务五种方式 代初学都参考使用
全书分3篇共21章,具体内容包括:Spring环境的安装与使用、JSP与JSTL简介、 Spring基础概念与工具、用SpringJdbcTemplate访问数据库、使用Mayen工程、Spring MVC编程、基于 MVC的资源共享网站设计、Spring的AOP编程...
spring提供了一个工具类JdbcTemplate,在类中对JDBC增删改查做了封装 JdbcTemplate: Spring 框架中提供一个ORM技术,对JDBC进行了封装 类似于我们之前的mybatis框架,对数据库做增删改查操作的 学习目标:了解该技术...
一个非常简单基于注解的Spring JdbcTemplate,供初学者参考用。
Druid数据库连接池的SpringJDBCTemplate所需的jar包,Druid数据库连接池的SpringJDBCTemplate所需的jar包,Druid数据库连接池的SpringJDBCTemplate所需的jar包,Druid数据库连接池的SpringJDBCTemplate所需的jar包,...
Spring JDBCTemplate连接池jar包
Spring JdbcTemplate调用Oracle存储过程输出游标结果集实现增删改查
模仿spring jdbcTemplate的粗略实现,只有很小的参考价值,如果是java初学者可以使用这个封装好的工具进行数据库操作,只需要在db.properties里配置好driver,url等信息
JdbcTemplate.rar
spring 中对大数据的处理,包括clob,blob的数据。比之jdbc下简便很多。