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

  1、获取Hibernate Session对象

  2、编写HQL语句

  3、以HQL语句作为参数,调用Session的createQuery方法创建查询对象。

  4、如果HQL语句包含参数,则调用Query的setXxx方法为参数赋值。

  5、调用Query对象的list()或uniqueResult()方法返回查询结果列表(持久化实体集)

  本文展示三种基本的HQL查询用法

  1.1、基本查询 Query query = session.createQuery(hql);
  2、条件查询
    2.1、基本条件查询 : String hql = "from com.Gary.domain.User where id = 1";
    2.2、?占位符:String hql = "from com.Gary.domain.User where id = ?0";
            query.setParameter(0, id);
    2.3、命名占位符 :String hql = "from com.Gary.domain.User where id = :id ";
            query.setParameter("id", id);
  3.1、分页查询
        limit ?, ?
        String hql = "from com.Gary.domain.User";
        query.setFirstResult(1);
        query.setMaxResults(1);
package com.Gary.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query; import com.Gary.domain.User;
import com.Gary.utils.HibernateUtils; public class HQLDao { //1.基本查询
public void search()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名
String hql = "from com.Gary.domain.User"; Query query = session.createQuery(hql);
List<User> list = query.list(); //查询多条数据
//query.uniqueResult(); 查询唯一的数据 System.out.println(list);
//模板:完成操作
beginTransaction.commit();
session.close(); } //2.1基本条件查询
public void search1()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名 //操作 select * from user where id = 1;
String hql = "from com.Gary.domain.User where id = 1";
Query query = session.createQuery(hql);
User user = (User)query.uniqueResult(); System.out.println(user.getName());
//模板:完成操作
beginTransaction.commit();
session.close(); } //2.2?占位符条件查询 id = 1
public void search2(String id)
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名 //操作 select * from user where id = 1;
String hql = "from com.Gary.domain.User where id = ?0";
Query query = session.createQuery(hql); query.setParameter(0, id);
User user = (User)query.uniqueResult(); System.out.println(user.getUsername());
//模板:完成操作
beginTransaction.commit();
session.close(); } //2.3命名占位符条件查询 id = 1
public void search3(String id)
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名 //操作 select * from user where id = 1;
String hql = "from com.Gary.domain.User where id = :id";
Query query = session.createQuery(hql);
query.setParameter("id", id);
User user = (User)query.uniqueResult(); System.out.println(user.getUsername());
//模板:完成操作
beginTransaction.commit();
session.close(); } //3.1分页查询
public void search4()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名 //操作 select * from user limit 0,1;
String hql = "from com.Gary.domain.User"; Query query = session.createQuery(hql);
//设置一条数据为一页 访问第二页的数据
query.setFirstResult(1);
query.setMaxResults(1); List<User> list = query.list(); System.out.println(list.get(0).getUsername());
//模板:完成操作
beginTransaction.commit();
session.close(); } }

HQLDao.java

  数据库user表  

  向数据库中添加假数据

  

1.HQL基本查询语句

  String hql = "from com.Gary.domain.User";
  Query query = session.createQuery(hql);

  HQLDao.java向数据库发起查询请求

//基本查询
public void search()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名
String hql = "from com.Gary.domain.User"; Query query = session.createQuery(hql);
List<User> list = query.list(); //查询多条数据
//query.uniqueResult(); 查询唯一的数据 System.out.println(list);
//模板:完成操作
beginTransaction.commit();
session.close(); }

  

package com.Gary.domain;

public class User {

    private String id;
private String username;
private String password;
private String name;
private String email;
private String telephone; public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
} }

User.java

package com.Gary.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query; import com.Gary.domain.User;
import com.Gary.utils.HibernateUtils; public class HQLDao { //基本查询
public void search()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名
String hql = "from com.Gary.domain.User"; Query query = session.createQuery(hql);
List<User> list = query.list(); //查询多条数据
//query.uniqueResult(); 查询唯一的数据 System.out.println(list);
//模板:完成操作
beginTransaction.commit();
session.close(); } }

HQLDao.java

2.1基本条件查询

  String hql = "from com.Gary.domain.User where id = 1";
  Query query = session.createQuery(hql);

  HQLDao.java向数据库发起查询请求

//基本条件查询
public void search1()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名 //操作 select * from user where id = 1;
String hql = "from com.Gary.domain.User where id = 1";
Query query = session.createQuery(hql);
User user = (User)query.uniqueResult(); System.out.println(user.getName());
//模板:完成操作
beginTransaction.commit();
session.close(); }

  

package com.Gary.domain;

public class User {

    private String id;
private String username;
private String password;
private String name;
private String email;
private String telephone; public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
} }

User.java

package com.Gary.domain;

public class User {

    private String id;
private String username;
private String password;
private String name;
private String email;
private String telephone; public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
} }

HQLDao.java

2.1?占位符条件查询

  String hql = "from com.Gary.domain.User where id = ?0";
  Query query = session.createQuery(hql);   query.setParameter(0, id);

  HQLDao.java向数据库发起查询请求

//2.2?占位符条件查询            id = 1
public void search2(String id)
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名 //操作 select * from user where id = 1;
String hql = "from com.Gary.domain.User where id = ?0";
Query query = session.createQuery(hql); query.setParameter(0, id);
User user = (User)query.uniqueResult(); System.out.println(user.getUsername());
//模板:完成操作
beginTransaction.commit();
session.close(); }

  

package com.Gary.domain;

public class User {

    private String id;
private String username;
private String password;
private String name;
private String email;
private String telephone; public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
} }

User.java

package com.Gary.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query; import com.Gary.domain.User;
import com.Gary.utils.HibernateUtils; public class HQLDao { //1.基本查询
public void search()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名
String hql = "from com.Gary.domain.User"; Query query = session.createQuery(hql);
List<User> list = query.list(); //查询多条数据
//query.uniqueResult(); 查询唯一的数据 System.out.println(list);
//模板:完成操作
beginTransaction.commit();
session.close(); } //2.1基本条件查询
public void search1()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名 //操作 select * from user where id = 1;
String hql = "from com.Gary.domain.User where id = 1";
Query query = session.createQuery(hql);
User user = (User)query.uniqueResult(); System.out.println(user.getName());
//模板:完成操作
beginTransaction.commit();
session.close(); } //2.2?占位符条件查询 id = 1
public void search2(String id)
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名 //操作 select * from user where id = 1;
String hql = "from com.Gary.domain.User where id = ?0";
Query query = session.createQuery(hql); query.setParameter(0, id);
User user = (User)query.uniqueResult(); System.out.println(user.getUsername());
//模板:完成操作
beginTransaction.commit();
session.close(); } }

UserDao.java

2.3命名占位符

  String hql = "from com.Gary.domain.User where id = :id";
  Query query = session.createQuery(hql);
  query.setParameter("id", id);

  HQLDao.java向数据库发起查询请求

//2.3命名占位符条件查询            id = 1
public void search3(String id)
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名 //操作 select * from user where id = 1;
String hql = "from com.Gary.domain.User where id = :id";
Query query = session.createQuery(hql);
query.setParameter("id", id);
User user = (User)query.uniqueResult(); System.out.println(user.getUsername());
//模板:完成操作
beginTransaction.commit();
session.close(); }

  

package com.Gary.domain;

public class User {

    private String id;
private String username;
private String password;
private String name;
private String email;
private String telephone; public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
} }

User.java

package com.Gary.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query; import com.Gary.domain.User;
import com.Gary.utils.HibernateUtils; public class HQLDao { //1.基本查询
public void search()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名
String hql = "from com.Gary.domain.User"; Query query = session.createQuery(hql);
List<User> list = query.list(); //查询多条数据
//query.uniqueResult(); 查询唯一的数据 System.out.println(list);
//模板:完成操作
beginTransaction.commit();
session.close(); } //2.1基本条件查询
public void search1()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名 //操作 select * from user where id = 1;
String hql = "from com.Gary.domain.User where id = 1";
Query query = session.createQuery(hql);
User user = (User)query.uniqueResult(); System.out.println(user.getName());
//模板:完成操作
beginTransaction.commit();
session.close(); } //2.2?占位符条件查询 id = 1
public void search2(String id)
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名 //操作 select * from user where id = 1;
String hql = "from com.Gary.domain.User where id = ?0";
Query query = session.createQuery(hql); query.setParameter(0, id);
User user = (User)query.uniqueResult(); System.out.println(user.getUsername());
//模板:完成操作
beginTransaction.commit();
session.close(); } //2.3命名占位符条件查询 id = 1
public void search3(String id)
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名 //操作 select * from user where id = 1;
String hql = "from com.Gary.domain.User where id = :id";
Query query = session.createQuery(hql);
query.setParameter("id", id);
User user = (User)query.uniqueResult(); System.out.println(user.getUsername());
//模板:完成操作
beginTransaction.commit();
session.close(); } }

HQLDao.java

3.1分页查询

        //操作 select * from user limit 0,1;
String hql = "from com.Gary.domain.User"; Query query = session.createQuery(hql);

  HQLDao.java向数据库发起查询请求

public void search4()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名 //操作 select * from user limit 0,1;
String hql = "from com.Gary.domain.User"; Query query = session.createQuery(hql);
//设置一条数据为一页 访问第二页的数据
query.setFirstResult(1);
query.setMaxResults(1); List<User> list = query.list(); System.out.println(list.get(0).getUsername());
//模板:完成操作
beginTransaction.commit();
session.close(); }

  

package com.Gary.domain;

public class User {

    private String id;
private String username;
private String password;
private String name;
private String email;
private String telephone; public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
} }

User.java

package com.Gary.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query; import com.Gary.domain.User;
import com.Gary.utils.HibernateUtils; public class HQLDao { //1.基本查询
public void search()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名
String hql = "from com.Gary.domain.User"; Query query = session.createQuery(hql);
List<User> list = query.list(); //查询多条数据
//query.uniqueResult(); 查询唯一的数据 System.out.println(list);
//模板:完成操作
beginTransaction.commit();
session.close(); } //2.1基本条件查询
public void search1()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名 //操作 select * from user where id = 1;
String hql = "from com.Gary.domain.User where id = 1";
Query query = session.createQuery(hql);
User user = (User)query.uniqueResult(); System.out.println(user.getName());
//模板:完成操作
beginTransaction.commit();
session.close(); } //2.2?占位符条件查询 id = 1
public void search2(String id)
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名 //操作 select * from user where id = 1;
String hql = "from com.Gary.domain.User where id = ?0";
Query query = session.createQuery(hql); query.setParameter(0, id);
User user = (User)query.uniqueResult(); System.out.println(user.getUsername());
//模板:完成操作
beginTransaction.commit();
session.close(); } //2.3命名占位符条件查询 id = 1
public void search3(String id)
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名 //操作 select * from user where id = 1;
String hql = "from com.Gary.domain.User where id = :id";
Query query = session.createQuery(hql);
query.setParameter("id", id);
User user = (User)query.uniqueResult(); System.out.println(user.getUsername());
//模板:完成操作
beginTransaction.commit();
session.close(); } //3.1分页查询
public void search4()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //模板:开始HQL操作
//HQL完全面对类 from类的全包名 //操作 select * from user limit 0,1;
String hql = "from com.Gary.domain.User"; Query query = session.createQuery(hql);
//设置一条数据为一页 访问第二页的数据
query.setFirstResult(1);
query.setMaxResults(1); List<User> list = query.list(); System.out.println(list.get(0).getUsername());
//模板:完成操作
beginTransaction.commit();
session.close(); } }

HQLDao.java

JavaWeb_(Hibernate框架)Hibernate中数据查询语句HQL基本用法的更多相关文章

  1. JavaWeb_(Hibernate框架)Hibernate中数据查询语句Criteria基本用法

    Criteria进行数据查询与HQL和SQL的区别是Criteria完全是面向对象的方式在进行数据查询,将不再看到有sql语句的痕迹,使用Criteria 查询数据包括以下步骤: 1. 通过sessi ...

  2. hibernate框架学习之数据查询(HQL)

    lHibernate共提供5种查询方式 •OID数据查询方式 •HQL数据查询方式 •QBC数据查询方式 •本地SQL查询方式 •OGN数据查询方式 OID数据查询方式 l前提:已经获取到了对象的OI ...

  3. JavaWeb_(Hibernate框架)Hibernate中数据查询语句SQL基本用法

    本文展示三种在Hibernate中使用SQL语句进行数据查询基本用法 1.基本查询 2.条件查询 3.分页查询 package com.Gary.dao; import java.util.List; ...

  4. hibernate框架学习之数据查询(HQL)helloworld

    package cn.itcast.h3.hql; import java.util.List; import org.hibernate.Query; import org.hibernate.Se ...

  5. hibernate框架学习之数据查询(本地SQL)

    本地SQL查询方式 lHibernate框架支持使用原生态SQL语句进行操作数据库 l查询对象SQLQuery由Session获取 •SQLQuery sq = s.createSQLQuery(&q ...

  6. hibernate框架学习之数据查询(QBC)

    lQBC(Query By Criteria)是一种Hibernate中使用面向对象的格式进行查询的计数 lQBC查询方式步骤 •获取Session对象 •初始化Criteria对象(使用Sessio ...

  7. hibernate框架学习之数据查询(QBC)helloworld

    package cn.itcast.h3.query.hql; import java.util.List; import org.hibernate.Criteria; import org.hib ...

  8. (数据科学学习手册28)SQL server 2012中的查询语句汇总

    一.简介 数据库管理系统(DBMS)最重要的功能就是提供数据查询,即用户根据实际需求对数据进行筛选,并以特定形式进行显示.在Microsoft SQL Serve 2012 中,可以使用通用的SELE ...

  9. JavaWeb_(Struts2框架)Action中struts-default下result的各种转发类型

    此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...

随机推荐

  1. Spring MVC <context:annotation-config> 与 <context:component-scan>

    在MVC的配置文件中,二者常出现,功能相似.简单做个比较 <context:annotation-config> 用于激活应用上下文中已经注册的bean的注解,无论你的bean是通过什么方 ...

  2. 正则表达式(Regular Expression)分组(Group)

    基本语法 (exp)匹配exp, 并捕获文本到自动命名的组里 (?<name>exp)  自己命名分组 static void Main(string[] args)        {   ...

  3. python之random库的使用以及程序的异常处理

    1.random库的使用: random库是使用随机数的Python标准库从概率论角度来说,随机数是随机产生的数据(比如抛硬币),但时计算机是不可能产生随机值,真正的随机数也是在特定条件下产生的确定值 ...

  4. 关于Mybatis的几件小事(二)

    一.MyBatis缓存机制 1.简介 Mybatis包含了一个非常强大的查询缓存的特性,它可以非常方便地配置和定制. 缓存key极大提高查询效率 MyBatis系统中默认定义了两次缓存 默认情况下,只 ...

  5. 检查一个string是否包含List<string>中的任意一个

    bool b = listOfStrings.Any(s=>myString.Contains(s)); 应用在where子句中的示例: //获取路径 var groupPaths = grou ...

  6. 说说css hack,说真的,我也是才去了解这个东西

    之前在很多地方看到css hack,今天狠下心,看看到底是什么鬼,所有我去百度了,然后看了一篇文章,然后写个小总结. css hack就是通过加一些特定的符号,不同的浏览器可以识别特定符号的样式,以此 ...

  7. 前端基础(一):HTML内容

    HTML介绍 Web服务本质 import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk.listen(5 ...

  8. 前端基础(六):Bootstrap框架

    Bootstrap介绍 Bootstrap是Twitter开源的基于HTML.CSS.JavaScript的前端框架. 它是为实现快速开发Web应用程序而设计的一套前端工具包. 它支持响应式布局,并且 ...

  9. 牛客练习赛48 E 小w的矩阵前k大元素

    E 思路: 优先队列,将迭代器变量作为结构体的变量. 迭代器走的时候只能像一个方向走,另外一个方向只有最开始才走.如下图所示: 如果两个方向同时走,同一个值会被遍历多次,像上图那样就能保证每个位置都走 ...

  10. Java&Python&Appium 驱动APP及Desired Capabilities配置

    一.摘要 本篇博文,将阐述如何将环境&代码&APP联系起来工作 二.Java代码 我们先看一段真实的java代码,我将他放在了TestNG框架的@BeforeTest里,执行这段即可启 ...