批量抓取理解:如果我们需要查找到客户的所有联系人的话,按照正常的思路,一般是首先查询所有的客户,得到返回的客户的List集合。然后遍历List集合,得到集合中的每一个客户,在取出客户中的联系人(客户表和联系人表是一个一对多的关系,一个客户有多个联系人),对于这种情况,我们就可以使用Hibernate的批量抓取,因为批量抓取进行了优化,比上面的先得到客户,在查询客户的联系人的效率更加的高效。

  

原始方法实现:

 // 批量抓取的原始做法
@Test
public void fun2() {
Transaction ts = null;
Session session = null;
SessionFactory factory = null;
try {
factory = Tools.getSessionFactory();
session = factory.openSession();
ts = session.beginTransaction();
// 通过id找到对应的数据记录
Criteria criteria = session.createCriteria(Customer.class);
List<Customer> customers = criteria.list();
for(Customer customer : customers) {
System.out.println(customer.getCid() + " -- " + customer.getCustName());
Set<LinkMan> sets = customer.getMans();
for (LinkMan set : sets) {
System.out.println("\t"+set.getLid()+"::"+set.getLinkName());
}
}
ts.commit();
} catch (Exception e) {
ts.rollback();
e.printStackTrace();
} finally {
session.close();
}
}

运行截图:

  对于原始的方式进行查询,每次查询一个联系人均会向数据库发送一条查询语句,这样的话,效率就低了。

控制台输出:

九月 12, 2017 9:33:49 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
Tue Sep 12 21:33:49 PDT 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Hibernate:
select
this_.cid as cid1_0_0_,
this_.custLevel as custLeve2_0_0_,
this_.custName as custName3_0_0_,
this_.custSource as custSour4_0_0_,
this_.custMobile as custMobi5_0_0_
from
t_customer this_
4028db335e71d615015e71d61bb60000 -- Geore
Hibernate:
select
mans0_.clid as clid5_1_0_,
mans0_.lid as lid1_1_0_,
mans0_.lid as lid1_1_1_,
mans0_.linkName as linkName2_1_1_,
mans0_.linkGender as linkGend3_1_1_,
mans0_.linkPhone as linkPhon4_1_1_,
mans0_.clid as clid5_1_1_
from
t_linkman mans0_
where
mans0_.clid=?
4028db335e71d615015e71d61bf30001::Mr.Li 4028db335e71d646015e71d64c8e0000 -- Alley
Hibernate:
select
mans0_.clid as clid5_1_0_,
mans0_.lid as lid1_1_0_,
mans0_.lid as lid1_1_1_,
mans0_.linkName as linkName2_1_1_,
mans0_.linkGender as linkGend3_1_1_,
mans0_.linkPhone as linkPhon4_1_1_,
mans0_.clid as clid5_1_1_
from
t_linkman mans0_
where
mans0_.clid=?
4028db335e71d6ff015e71d705ad0001::Mr.Zhang
4028db335e71d753015e71d759930001::Mr.Xie
4028db335e71d646015e71d64ccc0001::Mr.Wang 4028db335e721e35015e721e3b030000 -- Mary
Hibernate:
select
mans0_.clid as clid5_1_0_,
mans0_.lid as lid1_1_0_,
mans0_.lid as lid1_1_1_,
mans0_.linkName as linkName2_1_1_,
mans0_.linkGender as linkGend3_1_1_,
mans0_.linkPhone as linkPhon4_1_1_,
mans0_.clid as clid5_1_1_
from
t_linkman mans0_
where
mans0_.clid=?
4028db335e721e35015e721e3b410001::Mr.Zhao

Hibernate批量抓取实现:

  对于Hibernate的批量抓取的实现,其实在代码上并不需要进行改变,而只要修改配置文件的set标签,在set标签中添加属性batch-size,对于batch-size的值是一个整形的数据,整形的数据越大越好,越大发送的sql语句越少

配置代码:

 <set name="mans" cascade="save-update,delete" fetch="select" lazy="extra" batch-size="20">

控制台输出:

 INFO: HHH000228: Running hbm2ddl schema update
Tue Sep 12 21:38:34 PDT 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Hibernate:
select
this_.cid as cid1_0_0_,
this_.custLevel as custLeve2_0_0_,
this_.custName as custName3_0_0_,
this_.custSource as custSour4_0_0_,
this_.custMobile as custMobi5_0_0_
from
t_customer this_
4028db335e71d615015e71d61bb60000 -- Geore
Hibernate:
select
mans0_.clid as clid5_1_1_,
mans0_.lid as lid1_1_1_,
mans0_.lid as lid1_1_0_,
mans0_.linkName as linkName2_1_0_,
mans0_.linkGender as linkGend3_1_0_,
mans0_.linkPhone as linkPhon4_1_0_,
mans0_.clid as clid5_1_0_
from
t_linkman mans0_
where
mans0_.clid in (
?, ?, ?
)
4028db335e71d615015e71d61bf30001::Mr.Li
4028db335e71d646015e71d64c8e0000 -- Alley
4028db335e71d753015e71d759930001::Mr.Xie
4028db335e71d646015e71d64ccc0001::Mr.Wang
4028db335e71d6ff015e71d705ad0001::Mr.Zhang
4028db335e721e35015e721e3b030000 -- Mary
4028db335e721e35015e721e3b410001::Mr.Zhao

Hibernate的批量抓取的更多相关文章

  1. Hibernate批量抓取

    ------------------siwuxie095 Hibernate 批量抓取 以客户和联系人为例(一对多) 1.批量抓取 同时查询多个对象的关联对象,是 Hibernate 抓取策略的一种 ...

  2. Hibernate学习---第十一节:Hibernate之数据抓取策略&批量抓取

    1.hibernate 也可以通过标准的 SQL 进行查询 (1).将SQL查询写在 java 代码中 /** * 查询所有 */ @Test public void testQuery(){ // ...

  3. day36 08-Hibernate抓取策略:批量抓取

    package cn.itcast.test; import java.util.List; import org.hibernate.Hibernate; import org.hibernate. ...

  4. Python3利用BeautifulSoup4批量抓取站点图片的代码

    边学边写代码,记录下来.这段代码用于批量抓取主站下所有子网页中符合特定尺寸要求的的图片文件,支持中断. 原理很简单:使用BeautifulSoup4分析网页,获取网页<a/>和<im ...

  5. 使用HtmlAgilityPack批量抓取网页数据

    原文:使用HtmlAgilityPack批量抓取网页数据 相关软件点击下载登录的处理.因为有些网页数据需要登陆后才能提取.这里要使用ieHTTPHeaders来提取登录时的提交信息.抓取网页  Htm ...

  6. Web自动化框架LazyUI使用手册(4)--控件抓取工具Elements Extractor详解(批量抓取)

    概述 前面的一篇博文详细介绍了单个控件抓取的设计思路&逻辑以及使用方法,本文将详述批量控件抓取功能. 批量抓取:打开一个web页面,遍历页面上所有能被抓取的元素,获得每个元素的iframe.和 ...

  7. 如何上传Packages到PyPI并批量抓取

    1.如何上传包到PyPI ? 更新中... 2.批量抓取simple网站第三方模块 https://pypi.python.org/simple/ 3. 第三方模块的安装和使用 python  set ...

  8. python实现列表页数据的批量抓取练手练手的

    python实现列表页数据的批量抓取,练手的,下回带分页的 #!/usr/bin/env python # coding=utf-8 import requests from bs4 import B ...

  9. 使用IDM批量抓取音效素材下载

    IDM下载器的站点抓取功能,能够抓取网站上的图片.音频.视频.PDF.压缩包等等文件.更重要的是,能够实现批量抓取操作,省时省力.今天就来看一下,如何用IDM巧妙的批量抓取音效素材. 1.进入音效合辑 ...

随机推荐

  1. aidl 详解

    aidl 是 android interface define language 的缩写,主要是作为进程间通讯的一个接口规范,这种通讯是一种普通的 client-server 的模式,对于 clien ...

  2. 正确重写hashCode方法

    https://blog.csdn.net/HD243608836/article/details/87367763 到这里,对象写完了,开始描述问题. 计算hashCode的注意事项: 1.不能包含 ...

  3. wepy-wxss报错

    慢慢积攒下wepy 的一些BUG吧 1.页面在page目录下明明删除了某个子页面文件,打开wepy却一直报错!wxml报错或者wxss报错,提示的页面我为了排错都直接delete掉了,还是报错???思 ...

  4. SpringBoot整合AOP

    一.创建LogComponent类     类上加上@Component和@Aspect 表示把该类注册成spring组件和aop         二.创建6个方法     2.1     @Poin ...

  5. vue中的v-model原理,与组件自定义v-model

    VUE中的v-model可以实现双向绑定,但是原理是什么呢?往下看看吧 根据官方文档的解释,v-model其实是一个语法糖,它会自动的在元素或者组件上面解析为 :value="" ...

  6. linux运维、架构之路-SSH远程管理服务

    一.SSH服务功能介绍 1.远程登录管理 提供类似telnet远程联机服务器的服务,即上面提到的SSH服务 2.远程传输文件 是类似FTP服务的sftp-server,借助SSH协议来传输数据的,提供 ...

  7. java上传大文件(局域网环境)

    文件上传是最古老的互联网操作之一,20多年来几乎没有怎么变化,还是操作麻烦.缺乏交互.用户体验差. 一.前端代码 英国程序员Remy Sharp总结了这些新的接口 ,本文在他的基础之上,讨论在前端采用 ...

  8. SpringBoot:使用IDEA快速构建项目

    西部开源-秦疆老师:基于SpringBoot 2.1.6 的博客教程 秦老师交流Q群号: 664386224 未授权禁止转载!编辑不易 , 转发请注明出处!防君子不防小人,共勉! SpringBoot ...

  9. win7 编译postgresql9.6.8

    一.环境 windows7 postgresql9.6.8 vs2010 perl5.24.3 二.编译安装 1.安装perl,安装到C:\Perl64路径下,安装完成后设置环境变量PATH和Perl ...

  10. MacOS X 安装OpenCV3.2

    windows平台和linux平台安装参见 官方文档:http://docs.opencv.org/3.2.0/da/df6/tutorial_py_table_of_contents_setup.h ...