Spring批量更新batchUpdate提交和Hibernate批量更新executeUpdate
1:先看hibernate的批量更新处理。
版本背景:hibernate 5.0.8
applicationContext.xml 配置清单:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="org.yugh.test.mvc">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 属性文件位置 -->
<bean id="annotationPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:init.properties</value>
</list>
</property>
</bean>
<!-- 配置数据源 c3p0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 请求超时时间 -->
<property name="checkoutTimeout" value="30000" />
<!-- 每60秒检查所有连接池中的空闲连接。默认值: 0,不检查 -->
<property name="idleConnectionTestPeriod" value="30" />
<!-- 连接数据库连接池最大空闲时间 -->
<property name="maxIdleTime" value="30" />
<!-- 连接池初始化连接数 -->
<property name="initialPoolSize" value="5" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。默认值: 3 -->
<property name="acquireIncrement" value="5" />
</bean>
<!-- 配置hibernate的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入数据源 相关信息看源码 -->
<property name="dataSource" ref="dataSource" />
<!-- 扫描hibernate注解 -->
<property name="packagesToScan" value="org.yugh.test.mvc" />
<!-- hibernate配置信息 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<!-- 开启二级缓存 ehcache -->
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.cache.provider_configuration_file_resource_path}
</prop>
</props>
</property>
</bean>
<!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource"> <ref bean="dataSource" /> </property> </bean> -->
<!-- Spring处理方式 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- Spring处理方式 -->
<!-- <aop:aspectj-autoproxy proxy-target-class="true"/>
<aop:config proxy-target-class="true">
<aop:pointcut id="pointcut" expression="execution(* org.yugh.test.mvc.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<context:component-scan base-package="org.yugh.test.mvc" />
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean> -->
<!-- Spring处理方式 -->
<!-- 支持 @Transactional 标记 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务增强处理Bean,指定事务管理器 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<!-- 配置详细事务处理语义 -->
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="load*" propagation="SUPPORTS" read-only="true" />
<!-- 其他采用默认事务方式 -->
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- Spring aop事务管理 -->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut id="transactionPointcut" expression="execution(* org.yugh.test.mvc.services.*.*(..))" />
<!-- 指定在txAdvice切入点应用txAdvice事务增强处理-->
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config>
<!-- redis工厂 -->
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" />
<!-- p:host-name="172.20.22.4" p:port="6379" p:password="" -->
<!-- redis服务封装 -->
<bean id="redisServer" class="org.yugh.test.mvc.redis.RedisServer">
</bean>
</beans>
主要事务用的hibernate。
DAO层封装代码:
@Resource private SessionFactory sessionFactory; /**
* 获取当前工作的Session */ protected Session getSession() { return sessionFactory.getCurrentSession(); }
业务层DAO大致代码:
public int updateForHibernateTest(List<Object> list){
Transaction transaction = getSession().beginTransaction();
Long beTime = System.currentTimeMillis();
String sql = "update t_sale_order_ty_temp a set a.taker_name=?,a.taker_phone=?,a.pay_time=sysdate where a.order_id=? ";
Query query = getSession().createSQLQuery(sql);
for (int i = 0; i < list.size(); i++) {
query.setParameter(0, "yugh");
query.setParameter(1, "批量提交更新");
query.setParameter(2, list.get(i));
query.executeUpdate();
}
//transaction.commit();
Long enTime = System.currentTimeMillis();
System.out.println("结束时间,共计花费:" + String.valueOf( (enTime - beTime)/1000 ) + " 秒!" );
return query.getMaxResults();
}
解释:list<object>的list就是取order_id的字段来批量更新 taker_name和 taker_phone值,pay_time字段是记录是否同步批量提交。
这种写法每次循环去更新一条,最原始的update方式,一秒5条数据左右。
好处:很容易和项目融合,弊端:慢。
10793条数据更新时间:
Hibernate: update t_sale_order_ty_temp a set a.taker_name=?,a.taker_phone=?,a.pay_time=sysdate where a.order_id=? 结束时间,共计花费:48 秒! 17:37:14,061 INFO GenericApplicationContext:960 - Closing org.springframework.context.support.GenericApplicationContext@18eed359: startup date [Mon Oct 23 17:36:22 CST 2017]; root of context hierarchy
2:再看spring的批量提交处理。
版本背景:spring 4.2.5
变动applicationContext.xml 配置清单中有一条:
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean>
个人觉得spring的jdbctemplate可简可繁,属于小型、大型项目都可以随时拿来用的。
业务层DAO大致代码:
public int updateForSpring(final List<Object> list) {
String sql = "update t_sale_order_ty_temp a set a.taker_name=?,a.taker_phone=?,a.pay_time=sysdate where a.order_id=? ";
long beTime = System.currentTimeMillis();
jdbcTemplate.batchUpdate(sql.toString(), new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement pstmt, int i) throws SQLException {
pstmt.setString(1, "yugh");
pstmt.setString(2, "批量提交更新");
pstmt.setString(3, list.get(i)+"");
}
@Override
public int getBatchSize() {
return list.size();
}
});
long enTime = System.currentTimeMillis();
System.out.println("批量更新结束,共计花费时间:" + String.valueOf( (enTime - beTime) /1000 ) + " 秒!" );
return list.size();
}
解释:list<object>的list就是取10793条数据的order_id字段来批量更新 taker_name和 taker_phone值,pay_time字段是记录是否同步批量提交。
通过执行10793条数据后得出同步批量更新的数据。
好处:很容易和项目融合,代码精短。
10793条数据更新时间:
Hibernate: select order_id from t_sale_order_ty_temp where 1=1 批量更新结束,共计花费时间:20 秒! 17:52:37,912 INFO GenericApplicationContext:960 - Closing org.springframework.context.support.GenericApplicationContext@370736d9: startup date [Mon Oct 23 17:52:14 CST 2017]; root of context hierarchy
本文只介绍批量更新,批量insert和批量delete相比要简单多,事务和性能上也没有批量更新注意得多。
Spring批量更新batchUpdate提交和Hibernate批量更新executeUpdate的更多相关文章
- hibernate批量更新和删除数据
批量处理 不建议用Hibernate,它的insert效率实在不搞,不过最新版本的Hibernate似乎已经在批量处理的时候做过优化了,设置一些参数如batch_size,不过性能我没有测试过,听说 ...
- Hibernate批量处理数据
01.批量插入数据 步骤一.创建实体类,Dept和Emp /** * 员工类 * @author Administrator * */ public class Emp { private Integ ...
- Hibernate批量处理海量数据的方法
本文实例讲述了Hibernate批量处理海量数据的方法.分享给大家供大家参考,具体如下: Hibernate批量处理海量其实从性能上考虑,它是很不可取的,浪费了很大的内存.从它的机制上讲,Hibern ...
- hibernate 批量增加 修改 删除
4.2 Hibernate的批量处理 Hibernate完全以面向对象的方式来操作数据库,当程序里以面向对象的方式操作持久化对象时,将被自动转换为对数据库的操作.例如调用Session的delete ...
- hibernate 批量插入数据
如题,有两种方法 1)使用FLUSH 2)使用JDBC 分别来解释: 1)hibernate在进行数据库操作的时候,都要有事务支持的.可能你曾遇到过,没有加事务,程序会报错的情况. 而事务每次提交的时 ...
- mybatis批量增、删、改(更新)操作oracle和mysql批量写法小记
前言:用mybatis也好几年了,mybatis在批量的增删操作也写起来也是比较简单的,只有批量更新这一块是特别坑,特此记录. 注:本文主要用来记录oracle和mysql数据库在使用mybatis的 ...
- Hibernate批量抓取
------------------siwuxie095 Hibernate 批量抓取 以客户和联系人为例(一对多) 1.批量抓取 同时查询多个对象的关联对象,是 Hibernate 抓取策略的一种 ...
- Hibernate 批量update数据时,怎么样做可以回滚,
Hibernate 批量update数据时,怎么样做可以回滚, 1.serviceManagerDaoImpl代码里对异常不进行try,catch抛出, 2.或者抛出throw new Runtime ...
- 多线程查询数据,将结果存入到redis中,最后批量从redis中取数据批量插入数据库中【我】
多线程查询数据,将结果存入到redis中,最后批量从redis中取数据批量插入数据库中 package com.xxx.xx.reve.service; import java.util.ArrayL ...
随机推荐
- 201521123065《java程序设计》第12周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 1.流的划分:输入流:字节流(InputStream).字符流(reader): 输出流:字节流(Output ...
- 201521123051《Java程序设计》第十一周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. Java多线程同步的方法: (1)同步方法:即有synchronized关键字修饰的方法. 由于java的每个对象 ...
- sqlserver2012安装过程
第三次安装sqlserver2012 记录下安装过程,受益的人多多指教. 一.以我的系统64位为例,所以先准备安装包,从官网 https://www.microsoft.com/zh-CN/downl ...
- webservice第二篇【自定义webservice服务、soa、uddi概念、soap协议】
自定义webservice服务 我们在上一章节中已经使用wsimport生成本地代理来调用webservice的服务了,其实我们自己写的web应用程序也是可以发布webservice的 我们发布了we ...
- 在linux环境下搭建java web测试环境(非常详细!!)
一.项目必备软件及基本思路 项目必备:虚拟机:VMware Workstation (已安装linux的 CentOS6.5版本) 项目:java web项目 (必须在本地部署编译后选择项目的webR ...
- YYHS-鏖战字符串
题目描述 Abwad在nbc即将完成她的程序的时候,急中生智拔掉了她电脑的电源线,争取到了宝贵的时间.作为著名论文<论Ctrl-C与Ctrl-V在信息学竞赛中的应用>的作者,他巧妙地使用了 ...
- LeetCode解题中位运算的运用
位运算是我最近才开始重视的东西,因为在LeetCode上面刷题的时候发现很多题目使用位运算会快很多.位运算的使用包含着许多技巧(详细可以参考http://blog.csdn.net/zmazon/ar ...
- filter的两种使用方法
1. 在模板中使用filter 我们可以直接在{{}}中使用filter,跟在表达式后面用 | 分割,语法如下: {{ expression | filter }} 也可以多个filter连用,上一个 ...
- mvc一对多模型表单的快速构建
功能需求描述 Q:在实际的开发中,经常会遇到一个模型中包含有多个条目的表单.如何将数据提交到后台? A: 以数组的形式提交到后台就Ok了(真的那么简单么,如果再嵌套一层呢?) A2:拆分多个模型,映射 ...
- 教育,创新,提升:Indiegogo和Kickstarter上受中国用户支持的10个众筹项目
中国的经济正在迅速发展,已成为世界第二大经济体.中国家庭随着经济水平的提高,越来越多父母愿意将自己的子女送到海外留学. 家长们希望自己的子女可以有机会接受国外大学优质的教育, 以便他们将来可以学成归来 ...