Hibernate--QBC举例+详解(一)

article/2025/11/7 20:46:47

QBC检索

QBC(Query By Criteria)是Hibernate提供的另一种检索对象的方式,它主要由Criteria接口、Criterion接口和Expression类组成。

Criteria接口是Hibernate API中的一个查询接口,它需要由session进行创建。一个单独的查询就是Criterion接口的一个实例,用于限制Criteria对象的擦查询,在Hibernate中Criterion对象的创建通常是通过Restrictions工厂类完成的,它提供了条件查询方法。

Criterion是Criteria的查询条件,在Criteria中提供了add(Criterion criterion)方法来添加查询条件。

使用QBC检索对象的实例代码,如下所示

	//创建criteria对象Criteria criteria = session.createCriteria(Customer.class);//设定查询条件Criterion criterion = Restrictions.eq("id",1);//添加查询条件criteria.add(criterion);//执行查询,返回查询结果List<Customer> cs = criteria.list();

上述代码中查询的是id为1的Customer对象。

QBC检索是使用Restricions对象编写查询条件的,在Restrictions类中提供了大量的静态方法来创建查询条件。

–》Restrictions常量和方法

准备工作

创建一个Customer类:

package pers.zhang.domain;public class Customer {private Long cust_id;private String cust_name;private String cust_source;private String cust_industry;private String cust_level;private String cust_linkman;private String cust_phone;private String cust_mobile;public Long getCust_id() {return cust_id;}public void setCust_id(Long cust_id) {this.cust_id = cust_id;}public String getCust_name() {return cust_name;}public void setCust_name(String cust_name) {this.cust_name = cust_name;}public String getCust_source() {return cust_source;}public void setCust_source(String cust_source) {this.cust_source = cust_source;}public String getCust_industry() {return cust_industry;}public void setCust_industry(String cust_industry) {this.cust_industry = cust_industry;}public String getCust_level() {return cust_level;}public void setCust_level(String cust_level) {this.cust_level = cust_level;}public String getCust_linkman() {return cust_linkman;}public void setCust_linkman(String cust_linkman) {this.cust_linkman = cust_linkman;}public String getCust_phone() {return cust_phone;}public void setCust_phone(String cust_phone) {this.cust_phone = cust_phone;}public String getCust_mobile() {return cust_mobile;}public void setCust_mobile(String cust_mobile) {this.cust_mobile = cust_mobile;}@Overridepublic String toString() {return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]";}
}

配置ORM元数据:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="pers.zhang.domain" ><class name="Customer" table="cst_customer" ><id name="cust_id"  ><generator class="identity"></generator></id><property name="cust_name" column="cust_name" ></property><property name="cust_source" column="cust_source" ></property><property name="cust_industry" column="cust_industry" ></property><property name="cust_level" column="cust_level" ></property><property name="cust_linkman" column="cust_linkman" ></property><property name="cust_phone" column="cust_phone" ></property><property name="cust_mobile" column="cust_mobile" ></property></class>
</hibernate-mapping>

向表中插入数据:
在这里插入图片描述

基本查询–查询所有
@Test//基本查询public void fun1(){Session session = HibernateUtils.openSession();Transaction tx = session.beginTransaction();//-------------------------------------------//查询所有的Customer对象Criteria criteria = session.createCriteria(Customer.class);	List<Customer> list = criteria.list();System.out.println(list);//-------------------------------------------tx.commit();session.close();}

运行JUnit测试输出:

Hibernate: selectthis_.cust_id as cust_id1_0_0_,this_.cust_name as cust_nam2_0_0_,this_.cust_source as cust_sou3_0_0_,this_.cust_industry as cust_ind4_0_0_,this_.cust_level as cust_lev5_0_0_,this_.cust_linkman as cust_lin6_0_0_,this_.cust_phone as cust_pho7_0_0_,this_.cust_mobile as cust_mob8_0_0_ fromcst_customer this_
[Customer [cust_id=1, cust_name=Google], Customer [cust_id=2, cust_name=联想], Customer [cust_id=3, cust_name=百度], Customer [cust_id=4, cust_name=阿里巴巴], Customer [cust_id=5, cust_name=腾讯]]
条件查询

条件查询需要调用criteria.add()方法,参数为Restricions对象。
–》Restrictions常量和方法

@Test//条件查询// > 				gt// >=				ge// <				lt// <=				le// ==				eq// !=				ne// in				in// between and		between// like 			like// is not null 		isNotNull// is null			isNull// or				or// and				andpublic void fun2(){Session session = HibernateUtils.openSession();Transaction tx = session.beginTransaction();//-------------------------------------------//创建criteria查询对象Criteria criteria = session.createCriteria(Customer.class);//添加查询参数 => 查询cust_id为1的Customer对象criteria.add(Restrictions.eq("cust_id", 1l));//执行查询获得结果Customer c = (Customer) criteria.uniqueResult();System.out.println(c);//-------------------------------------------tx.commit();session.close();}

运行JUnit测试输出:

Hibernate: selectthis_.cust_id as cust_id1_0_0_,this_.cust_name as cust_nam2_0_0_,this_.cust_source as cust_sou3_0_0_,this_.cust_industry as cust_ind4_0_0_,this_.cust_level as cust_lev5_0_0_,this_.cust_linkman as cust_lin6_0_0_,this_.cust_phone as cust_pho7_0_0_,this_.cust_mobile as cust_mob8_0_0_ fromcst_customer this_ wherethis_.cust_id=?
Customer [cust_id=1, cust_name=Google]
分页查询

QBC的分页查询与MySql的limit十分相似,使用两个方法:
1.criteria.setFirstResult(arg),设置第一条结果从哪个索引开始,相当于limit中的第一个?。
2.criteria.setMaxResults(arg),设置一次查询多少条数据,相遇于limit中的第二个?。

	@Test//分页查询public void fun3(){Session session = HibernateUtils.openSession();Transaction tx = session.beginTransaction();//-------------------------------------------Criteria criteria = session.createCriteria(Customer.class);//设置分页信息 limit ?,?  从第1条开始查,查询2条数据criteria.setFirstResult(1);criteria.setMaxResults(2);List<Customer> list = criteria.list();System.out.println(list);//-------------------------------------------tx.commit();session.close();}

运行JUnit测试输出:

Hibernate: selectthis_.cust_id as cust_id1_0_0_,this_.cust_name as cust_nam2_0_0_,this_.cust_source as cust_sou3_0_0_,this_.cust_industry as cust_ind4_0_0_,this_.cust_level as cust_lev5_0_0_,this_.cust_linkman as cust_lin6_0_0_,this_.cust_phone as cust_pho7_0_0_,this_.cust_mobile as cust_mob8_0_0_ fromcst_customer this_ limit ?,?
[Customer [cust_id=2, cust_name=联想], Customer [cust_id=3, cust_name=百度]]
排序查询

条件查询需要调用criteria.addOrder()方法,参数为Order对象。
Order类的常用方法:作为查询容器的参数

方法名称描述使用
Order.asc升序Order.asc(String propertyName)
Order.desc降序Order.desc(String propertyName)
@Testpublic void fun4() {Session session = HibernateUtils.openSession();Transaction tx = session.beginTransaction();//-------------------------------------------Criteria criteria = session.createCriteria(Customer.class);//设置排序规则 按id降序排序criteria.addOrder(Order.desc("cust_id"));//执行查询List<Customer> list = criteria.list();System.out.println(list);	//-------------------------------------------tx.commit();session.close();}

运行JUnit测试输出:

Hibernate: selectthis_.cust_id as cust_id1_0_0_,this_.cust_name as cust_nam2_0_0_,this_.cust_source as cust_sou3_0_0_,this_.cust_industry as cust_ind4_0_0_,this_.cust_level as cust_lev5_0_0_,this_.cust_linkman as cust_lin6_0_0_,this_.cust_phone as cust_pho7_0_0_,this_.cust_mobile as cust_mob8_0_0_ fromcst_customer this_ order bythis_.cust_id desc
[Customer [cust_id=5, cust_name=腾讯], Customer [cust_id=4, cust_name=阿里巴巴], Customer [cust_id=3, cust_name=百度], Customer [cust_id=2, cust_name=联想], Customer [cust_id=1, cust_name=Google]]
统计查询

条件查询需要调用criteria.setProjection()方法,参数为Projection对象。
Projections类的常用方法:作为查询容器的参数:

方法名称描述使用
Projections.avg求平均值Projections.avg(String propertyName)
Projections.count统计某属性的数量Projections.count(String propertyName)
Projections.countDistinct统计某属性不同值的数量Projections.countDistinct(String propertyName)
Projections.groupProperty指定某个属性为分组属性Projections.groupProperty(String propertyName)
Projections.max求最大值Projections.max(String propertyName)
Projections.min求最小值Projections.min(String propertyName)
Projections.projectionList创建一个ProjectionList对象Projections.projectionList()
Projections.rowCount统计结果集中的记录条数Projections.rowCount()
Projections.sum求某属性的合计Projections.sum(String propertyName)
@Test//查询总记录数public void fun5(){Session session = HibernateUtils.openSession();Transaction tx = session.beginTransaction();//-------------------------------------------//创建criteria查询对象Criteria criteria = session.createCriteria(Customer.class);//设置查询的聚合函数 => 总行数criteria.setProjection(Projections.rowCount());Long count = (Long) criteria.uniqueResult();System.out.println(count);//-------------------------------------------tx.commit();session.close();}

运行JUnit测试输出:

Hibernate: selectcount(*) as y0_ fromcst_customer this_
5

http://chatgpt.dhexx.cn/article/dOyHWNNt.shtml

相关文章

统一软件开发过程(RUP)的概念和方法

统一软件开发过程&#xff08;Rational Unified Process,RUP)是一种面向对象且基于网络的程序开发方法论。 根据Rational(Rational Rose和统一建模语言的开发者)的说法&#xff0c;好像一个在线的指导者&#xff0c;它可以为所有方面和层次的程序开发提供指导方针&#xff0c;…

程序员技术开发委托合同模板 私活模板

---需要电子word版&#xff0c;请关注--------- 回复&#xff1a;私活 含税与不含税计算工具&#xff1a; 假设税率是6% 不含税金额&#xff1d;总金额/1.06 税额&#xff1d;不含税金额0.06 增值税在线计算器&#xff1a;http://www.ab126.com/goju/7332.html 大小写转换&…

软件开发模型

1.1大爆炸模型 大爆炸默认是相对简单的开发模型&#xff0c;在开发过程中&#xff0c;没有什么规范和计划&#xff0c;更没有测试 1.2 边做边改模型 相对于大爆炸模型&#xff0c;考虑到了项目需求&#xff0c;然后开发来回的编写&#xff0c;测试&#xff0c;修改缺陷的过程…

10种软件开发模型整理

准备整理一下软件模型&#xff0c;但是百度到都是零散信息&#xff0c;所以文章准备从概念、模型核心思想、优点、缺点、生命周期等体现各个软件模型 什么是软件模型 软件开发模型(Software Development Model)是指软件开发全部过程、活动和任务的结构框架。软件开发包括需求、…

系统软件开发基础知识

系统软件开发基础知识 最近最火的是孩子考了多少分&#xff0c;能上那个好大学&#xff0c;出现了我的大学的我的梦。 他们的大学他们的梦&#xff0c;我是啥&#xff0c;我的软件我的梦。 下面了解到的基础知识做一个归纳&#xff0c;本人了解这方面的知识点可能很片面&…

软件开发测试验收通知书,软件项目开发-客户告知书

为确保项目进度快速进行&#xff0c;在域名备案、开通相关第三方接口、确认功能文档、UI设计沟通和验收等阶段&#xff0c;恳请贵方百忙之中抽出时间积极配合&#xff0c;同时也为避免以后双方产生不必要的纠纷&#xff0c;特此告知贵方在项目进行过程中提供如下必要的配合条件…

利用Windows注册Dll或其他文件

注册Dll文件 前章 有时候&#xff0c;我要使用ActiveX控件&#xff0c;但是。如果别人给了一个Dll没注册进去怎么办。 注册 在电脑开始界面->运行->输入regsvr32“Dll文件所在路径”&#xff08;也可以用winR快捷键输出&#xff09;&#xff0c;如下图&#xff1a;

DLL注入——使用注册表

1.简介 整个系统的配置都保存在注册表中&#xff0c;我们可以通过调整其中的设置来改变系统的行为。 该方式依赖User32.dll&#xff0c;也就是说&#xff0c;需要可执行程序调用到这个系统动态库&#xff0c;我们注入的dll才会被执行到。基本上所有基于GUI的应用程序都使用了…

如何手动运行dll文件,非注册dll

遇到一个dll文件的恶意样本&#xff0c;cuckoo沙箱给出的运行命令如下&#xff1a; cd C:\Windows\System32\ rundll32.exe C:\coreshell.dll,DllMain 方法&#xff1a;rundll32.exe 文件位置,DllMain but会弹出警告 另外一种方法&#xff0c;在专家的文章中有提到 DllM…

怎样在计算机上注册dll文件,win10如何注册dll文件_win10系统dll文件怎样安装

在win10系统中&#xff0c;有着大量的dll文件&#xff0c;这是一种计算机上的一类文件&#xff0c;而且一个DLL文件也可能被不同的应用程序使用&#xff0c;但是许多用户可能都不知道win10注册dll文件的操作&#xff0c;那么win10如何注册dll文件呢&#xff1f;接下来就来告诉大…

Win10注册DLL办法

测试环境&#xff1a;win7 64位系统自动升级到win10 64位系统 经验提升&#xff1a;不要在开始-运行-CMD里面执行&#xff0c;不起作用。 注册步骤如下&#xff1a; 1、准备要注册的DLL文件到指定目录 2、新建一个BAT文件&#xff0c;写入注册命令 3、右键已管理员身份运行注…

怎样在计算机上注册dll文件,注册dll文件【搞定步骤】

喜欢使用电脑的小伙伴们一般都会遇到win7系统注册dll文件的问题&#xff0c;突然遇到win7系统注册dll文件的问题就不知道该怎么办了&#xff0c;其实win7系统注册dll文件的解决方法非常简单&#xff0c;按照 1&#xff1a;在电脑桌面上&#xff0c;依次选中菜单项开始--运行&am…

C#【必备技能篇】注册dll+批量注册dll

文章目录 问题描述&#xff1a;一、注册单个dll&#xff1a;1、把相应的dll放置到System32&#xff08;32位&#xff09;和SysWOW64中2、以管理员身份打开“命令提示符”3、输入cmd指令完成注册4、把相关的dll放置到项目输出路径下&#xff0c;即可成功引用 二、批量注册dll1、…

photoshop cc 2018破解补丁(pscc2018注册机) 附使用方法

1、下载破解程序 破解文件自行到 http://www.ddooo.com/softdown/109954.htm 下载 博主可以到本博客的文件--》pscc2018zcj_109954.rar下载 2、破解 打开Amtemu.exe 第一个下拉框选择 adobe photoshop cc 2015 没错&#xff0c;就是2015 。。2015可以破解2018的 然后点击…

安装ADOBE全系列软件的步骤

第一步&#xff1a;下载Adobe Creative Cloud 这款应用里几乎囊括了Adobe旗下的所有软件&#xff0c;也就是说有了这个应用&#xff0c;以后下载Adobe的任意一款软件&#xff0c;完全不用再去百度搜索下载地址了&#xff0c;更不用担心来源不正有病毒啥的&#xff0c;只要你的电…

Adobe Audition CS6

Adobe Audition CS6是一款功能强大、效果出色的多轨录音和音频处理软件&#xff0c;可在普通声卡上同时处理多达64轨的音频信号&#xff0c;并且最多支持混合128个声道&#xff0c;创建回路并使用45种以上的数字信号处理效果&#xff0c;具有极其丰富的音频处理效果。而且Audit…

Adobe Lightroom For Mac安装使用教程

软件介绍 Adobe Photoshop Lightroom是Adobe 研发的一款以后期制作为重点的图形工具软件&#xff0c;是当今数字拍摄工作流程中不可或缺的一部分。其增强的校正工具、强大的组织功能以及灵活的打印选项可以帮助您加快图片后期处理速度&#xff0c;将更多的时间投入拍摄。 安装…

Adobe Acrobat DC 2015 for Mac

作为世界上优秀的桌面版 PDF 解决方案的后继之作&#xff0c;焕然一新的 Acrobat DC 将彻底超乎您的想象。Adobe Acrobat DC 2015 for Mac 是一款值得信赖的 PDF 创建程序&#xff0c;可使您的工作变得更轻松。使用 Acrobat 对 PDF 文件进行转换、编辑和签名&#xff0c;无论您…

Adobe Acrobat Pro DC 2021下载及教程

资源地址&#xff1a;Adobe Acrobat Pro DC 2021 Adobe Acrobat Pro DC 2021是一款功能强大且专业性的PDF编辑处理工具&#xff0c;该软件也是一款知名度非常高的一款文件处理软件&#xff0c;该软件提高了PDF解决方案的新里程碑&#xff0c;还拥有全球最顶尖的PDF文件编辑处理…

lightroom classic破解版

lrc2022是一款图片后期处理软件&#xff0c;全称为Adobe Photoshop Lightroom Classic 2022&#xff0c;它用于摄影后期的图像管理、校正和处理&#xff0c;但不同的是&#xff0c;该软件支持多种相机和镜头配置文件&#xff0c;可以帮助用户轻松、快速地修复镜头像差问题。从技…