HQL是Hibernate Query Language的缩写,提供更加丰富灵活、更为强大的查询能力;HQL更接近SQL语句查询语法。

HQL基础查询 

1.获取部分列 多列

 /**
* 获取部分列 多列 Object[]
*/
@Test
public void testgetMultipelColumns(){
String hql="select d.dname,d.loc from Dept d";
Query query=session.createQuery(hql);
List<Object[]> list=query.list();
for (Object[] item:list){
for (Object items:item) {
System.out.println(items+"--");
}
System.out.println();
}
}

2.获取部分列 多列 list<强类型>

   /**
* 获取部分列 多列 list<强类型>
*/
@Test
public void testgetMultipelColumns(){
String hql="select new Dept(d.deptno,d.dname,d.loc) from Dept d";
Query query=session.createQuery(hql);
List<Dept> list=query.list();
for (Dept dept:list){
System.out.println(dept.getDname());
}
}

HQL参数查询 

使用字符串拼接查询条件存在各种弊端"from User where name = '" + name + "'"
性能低
不安全

使用占位符
按参数位置绑定
from User where name = ?
按参数名称绑定
from User where name = :name

   /**
* 参数查询: 方案三::dname 参数名称绑定+对象属性
*/
@Test
public void selectByConditionParameternameAndObjectAttribute(){
//部门名称为SALES的部门信息
String hql="from Dept d where d.dname=:dname and d.loc=:loc";
Query query = session.createQuery(hql);
DeptModel model=new DeptModel();
model.setDname("SALES");
model.setLoc("CHICAGO");
query.setProperties(model);
List<Dept> list = query.list();
for (Dept dept:list) {
System.out.println(dept.getDname());
}
}
/**
* 参数查询: 方案二::dname 参数名称绑定
*/
@Test
public void selectByConditionParametername(){
//部门名称为SALES的部门信息
String hql="from Dept d where d.dname=:dname and d.loc=:loc";
Query query = session.createQuery(hql);
query.setParameter("dname","SALES");
query.setParameter("loc","CHICAGO");
List<Dept> list = query.list();
for (Dept dept:list) {
System.out.println(dept.getDname());
}
}
/**
* 参数查询: 方案一:? 匿名占位符
*/
@Test
public void selectByConditionNiming(){
//部门名称为SALES的部门信息
String hql="from Dept d where d.dname=? and d.loc=?";
Query query = session.createQuery(hql);
query.setParameter(0,"SALES");
query.setParameter(1,"CHICAGO");
List<Dept> list = query.list();
for (Dept dept:list
) {
System.out.println(dept.getDname());
}
}

动态查询

   /**
* 动态sql
*/
@Test
public void selectByDynamic() throws ParseException {
EmpCondition emp=new EmpCondition();
emp.setJob("CLERK");
emp.setSal(1000.0);
//入职时间
emp.setFromDate(Tool.strDate("1891-05-01"));
//离职时间
emp.setEndDate(new Date());
//根据条件拼接sql
StringBuilder sb=new StringBuilder("from Emp e where 1=1 ");
if(emp.getJob()!=null){
sb.append("and e.job =:job ");
}
if(emp.getSal()!=null){
sb.append("and e.sal >:sal ");
}
if (emp.getFromDate()!=null){
sb.append("and e.hiredate >=:fromDate ");
}
if (emp.getEndDate()!=null){
sb.append("and e.hiredate <=:endDate ");
}
Query query=session.createQuery(sb.toString());
query.setProperties(emp);
List<Emp> list = query.list();
for (Emp item:list) {
System.out.println(item.getEname());
}
}

分页查询

Query接口的相关方法
uniqueResult()  :获取唯一对象
setFirstResult() :设置从第几条开始
setMaxResults():设置读取最大记录数

  /**
* 分页
*/
@Test
public void selectPage(){
String hql="from Emp order by empno";
Query query = session.createQuery(hql);
int pageIndex=1;
int pageSize=3;
query.setFirstResult((pageIndex-1)*pageSize);
query.setMaxResults(pageSize);
List<Emp> empList=query.list();
for (Emp emp:empList){
System.out.println(emp.getEname());
}
}

提取工具类:HibernateUtil.java

   //线程变量
static ThreadLocal<Session> tlSession=new ThreadLocal<Session>();
public static SessionFactory factory;
static Configuration cfg=null;
static {
cfg=new Configuration().configure("hibernate.cfgscott.xml");
factory=cfg.buildSessionFactory();
}
//1.获取连接
public static Session getSession(){
//01 从线程中尝试获取
Session session=tlSession.get();
if (session==null){
session=factory.openSession();
tlSession.set(session);
}
return session;
}
//2.释放连接
public static void closeSession(){
Session session=tlSession.get();
if (session!=null){
tlSession.set(null);
session.close();
}
}

HQL实用技术的更多相关文章

  1. 第五章 HQL实用技术

    第五章   HQL实用技术5.1  使用HQL查询语句(面向对象查询语句)    5.1.1 编写HQL语句        5.1.1.1 from子句                    例:fr ...

  2. Hibernate=====HQL实用技术

    Hibernate支持三种查询语言:HQL查询.Criteria查询和原生SQL查询 HQL(hibernate Query Language,hibernate查询语言)是一种面向对象查询语言,其中 ...

  3. Hibernate-02 HQL实用技术

    学习任务 Query接口的使用 HQL基本用法 动态参数绑定查询 HQL的使用 Hibernate支持三种查询方式:HQL查询.Criateria查询.Native SQL查询. HQL是Hibern ...

  4. 深入HQL学习以及HQL和SQL的区别

    HQL(Hibernate Query Language) 是面向对象的查询语言, 它和 SQL 查询语言有些相似. 在 Hibernate 提供的各种检索方式中, HQL 是使用最广的一种检索方式. ...

  5. hibernate -- HQL语句总结

    1. 查询整个映射对象所有字段 //直接from查询出来的是一个映射对象,即:查询整个映射对象所有字段 String hql = "from Users"; Query query ...

  6. Hibernate 查询方式(HQL/QBC/QBE)汇总

    作为老牌的 ORM 框架,Hibernate 在推动数据库持久化层所做出的贡献有目共睹. 它所提供的数据查询方式也越来越丰富,从 SQL 到自创的 HQL,再到面向对象的标准化查询. 虽然查询方式有点 ...

  7. hql中in关键字的用法

    hql: from " + FoodComment.class.getName() + " f where f.id in :groupIds" 封装的方法: publi ...

  8. [转]HQL中的子查询

    原文地址:http://blog.csdn.net/xb12369/article/details/8638683 子查询:   子查询是SQL语句中非常重要的功能特性,它可以在SQL语句中利用另外一 ...

  9. Hibernate--------八大类HQL查询集合

    Hibernate的   八大类HQL查询集合 Hibernate的八大类HQL查询集合: 一:属性查询(SimplePropertyQuery) 1,单一属性查询 *返回结果集属性列表,元素类型和实 ...

随机推荐

  1. iOS 测试之非代码获取 iPhone 型号及其他信息

    首先 安装libimobiledevice和ideviceinstaller $ brew uninstall ideviceinstaller $ brew uninstall libimobile ...

  2. AI-URL注册器

    官方文档地址:https://www.django-rest-framework.org/tutorial/quickstart/#serializers #url生成器生成四个url,就可以访问关于 ...

  3. 使用asp.net MVC的 HtmlHelper 时遇到的小问题,报错:Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

    异常信息:Templates can be used only with field access, property access, single-dimension array index, or ...

  4. P1036 选数 题解

    题目链接https://www.luogu.org/problemnew/show/P1036 题目描述 已知 nnn 个整数 x1,x2,-,xnx_1,x_2,-,x_nx1​,x2​,-,xn​ ...

  5. 报错ERR_CONNECTION_REFUSED,如何解决(原创)

    当我访问我的一个后天地址的时候,突然出现了ERR_CONNECTION_REFUSED,但是之前是可以访问的. 我先ping了下这个网址,发现是OK的 然后我想可能是80端口有问题,也就是说可能是WE ...

  6. WCF寄宿IIS

    1.创建一个简单的wcf项目 创建完成后直接运行,结果 然后进行发布 在IIS上新建一个网站,直接进行发布即可 遇到的问题 请求与通配符 mime 映射相匹配.请求映射到静态文件处理程序. 需要注意的 ...

  7. VS2017远程调试C#或 Visual Studio 中的 Visual Basic 项目

    来源:远程调试C#或 Visual Studio 中的 Visual Basic 项目 若要调试已部署在另一台计算机的 Visual Studio 应用程序,安装和在其中部署您的应用程序的计算机上运行 ...

  8. IPMI无法执行命令

    IPMI无法执行命令 https://www.cnblogs.com/EricDing/p/8995263.html http://www.cnblogs.com/heidsoft/p/4014301 ...

  9. eclipse中文乱码修改新方法

    方法背景:想看别人的JAVA项目,导入eclipse后出现中文乱码,在设置了所有的工作空间都为UTF-8以后都没有用,并且项目Resource选项没有GBK选项,或统一选择GBK后会使其他项目出现中文 ...

  10. Python 高级面向对象

    一.字段 1.字段包括:普通字段和静态字段,他们在定义和使用中有所区别,而最本质的区别是内存中保存的位置不同. a.普通字段属于对象(实例变量) b.静态字段属于类(类变量) 二.属性 对于属性,有以 ...