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