JavaWeb_(Hibernate框架)Hibernate中数据查询语句HQL基本用法
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基本用法的更多相关文章
- JavaWeb_(Hibernate框架)Hibernate中数据查询语句Criteria基本用法
Criteria进行数据查询与HQL和SQL的区别是Criteria完全是面向对象的方式在进行数据查询,将不再看到有sql语句的痕迹,使用Criteria 查询数据包括以下步骤: 1. 通过sessi ...
- hibernate框架学习之数据查询(HQL)
lHibernate共提供5种查询方式 •OID数据查询方式 •HQL数据查询方式 •QBC数据查询方式 •本地SQL查询方式 •OGN数据查询方式 OID数据查询方式 l前提:已经获取到了对象的OI ...
- JavaWeb_(Hibernate框架)Hibernate中数据查询语句SQL基本用法
本文展示三种在Hibernate中使用SQL语句进行数据查询基本用法 1.基本查询 2.条件查询 3.分页查询 package com.Gary.dao; import java.util.List; ...
- hibernate框架学习之数据查询(HQL)helloworld
package cn.itcast.h3.hql; import java.util.List; import org.hibernate.Query; import org.hibernate.Se ...
- hibernate框架学习之数据查询(本地SQL)
本地SQL查询方式 lHibernate框架支持使用原生态SQL语句进行操作数据库 l查询对象SQLQuery由Session获取 •SQLQuery sq = s.createSQLQuery(&q ...
- hibernate框架学习之数据查询(QBC)
lQBC(Query By Criteria)是一种Hibernate中使用面向对象的格式进行查询的计数 lQBC查询方式步骤 •获取Session对象 •初始化Criteria对象(使用Sessio ...
- hibernate框架学习之数据查询(QBC)helloworld
package cn.itcast.h3.query.hql; import java.util.List; import org.hibernate.Criteria; import org.hib ...
- (数据科学学习手册28)SQL server 2012中的查询语句汇总
一.简介 数据库管理系统(DBMS)最重要的功能就是提供数据查询,即用户根据实际需求对数据进行筛选,并以特定形式进行显示.在Microsoft SQL Serve 2012 中,可以使用通用的SELE ...
- JavaWeb_(Struts2框架)Action中struts-default下result的各种转发类型
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
随机推荐
- C++ STL 之 queue
queue 是一种先进先出(first in first out, FIFO)的数据类型,他有两个口,数据元素只能从一个口进,从另一个口出.队列只允许从队尾加入元素,队头删除元素,必须符合先进先出的原 ...
- C# webserver实现短信发送(移动)
近端时间接了个需求在原来的OA办公系统中添加一个发送短信功能.(既然需要发送短信那肯定要申请一个发送短信的账号,我这里是以移动mas为列子) c#的weserver需要选协议WS.其他的基本不用怎么填 ...
- ASE19团队项目alpha阶段model组 scrum3 记录
本次会议于11月5日,19时整在微软北京西二号楼sky garden召开,持续25分钟. 与会人员:Jiyan He, Kun Yan, Lei Chai, Linfeng Qi, Xueqing W ...
- 第三章、前端之JavaScript
目录 第三章.前端之JavaScript 一.javaScript的引入方式 二.JavaScript语言的规范 三.语言基础 变量声明 四.数据类型 五.流程控制 六.函数 函数的argument ...
- Image Processing and Analysis_8_Edge Detection:Multiresolution edge detection techniques ——1995
此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...
- PhpStorm添加PHP代码规范检查CodeSniffer(phpcs)和PHP代码静态分析工具Mess Detector(phpmd)
一.安装 添加镜像,加速下载 ./composer.phar config -g repo.packagist composer https://packagist.phpcomposer.com ...
- vim编辑命令
vi命令 命令模式: yy:复制 光标所在的这一行 4yy:复制 光标所在行开始向下的4行 p: 粘贴 dd:剪切 光标所在的这一行 2dd:剪切 光标所在行 向下 2行 D:从当前的光标开始剪切,一 ...
- Mac下用命令行压缩和解压rar文件的方法(转)
废话不多说,直接进入主题 第一步:下载RAR工具包或https://www.rarlab.com/download.htm,根据自己需要下载相对应的版本 第二步:解压对应的压在的压缩包rarosx-5 ...
- linux下动态库中的soname
soname( Short for shared object name) 其是应用程序加载dll 时候,其寻找共享库用的文件名.其格式为 lib + math+.so + ( major versi ...
- VMware厚置备延迟置零,厚置备置零,精简置备详解
1.厚置备延迟置零(zeroed thick) 以默认的厚格式创建虚拟磁盘.创建过程中为虚拟磁盘分配所需空间.创建时不会擦除物理设备上保留的任何数据,但是以后从虚拟机首次执行写操作时会按需要将其置零. ...