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 ...
随机推荐
- 201521123081《Java程序设计》 第2周学习总结
201521123081<Java程序设计>第2周学习总结 2017-03-04 14:35:49 1. 本周学习总结 学习了Java一些基本语句,了解了变量类型及其相互转化的方法: 学 ...
- 201521123059 《Java程序设计》第十四周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 1.关系型数据库 --建立表格时表中一列中的数据类型必须一致.关系表中的行必须是唯一的,列是不可分的,某些行的某 ...
- 201521123096《Java程序设计》第十三周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 书面作业 1. 网络基础 1.1 比较ping www.baidu.com与ping cec.jmu ...
- Java程序设计——学生基本信息管理系统
1.团队课程设计博客链接 http://www.cnblogs.com/handsome321/p/7067121.html 2.个人负责模块说明 本组课题:学生信息管理系统 本人任务:插入.删除学生 ...
- SVN不出现绿色对勾的情况
就目前而言,我出现了两种情况. Num1:电脑云盘可能不兼容,导致无法出现svn提示小icon:----->删除云盘重新启动. Num2:被设置覆盖.----->鼠标右键-->Tor ...
- 基于图形检测API(shape detection API)的人脸检测
原文:https://paul.kinlan.me/face-detection/ 在 Google 开发者峰会中,谷歌成员 Miguel Casas-Sanchez 跟我说:"嘿 Paul ...
- 二分求最长上升子序列 二分LIS
#include <iostream> #include <cstring> #define N 50010 using namespace std; int n; int n ...
- nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed
在用AOP 的时候出现了如下的错误, 警告: Exception encountered during context initialization - cancelling refresh atte ...
- openGPS.cn - 高精度IP定位原理,定位误差说明
[ip定位历史] 关于IP定位,最早是通过运营商实现,每个运营商申请到的ip段,在某个范围内使用. 因此早期只能是国家为单位的基础数据. 对于比较大的国家,就进一步划分,比如,中国某通讯公司(不打广告 ...
- 在 macOS High Sierra 10.13 搭建 PHP 开发环境
2017 年 9 月 26 日,苹果公司正式发布了新一代 macOS,版本为 High Sierra (11.13). macOS High Sierra 预装了 Ruby(2.3.3).PHP(7. ...