1、简单查询(查询所有)

Session session=HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx=session.beginTransaction();
List<People> peoples=session.createQuery("from People").list();
for(People people:peoples){
System.out.println(people.getName());
}
tx.commit();

2、依照參数查询

Session session=HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx=session.beginTransaction();
//1、依照位置进行參数绑定
/*Query query=session.createQuery("from People Where name=?");
query.setString(0,"小明1");
*/
//2、依照參数名进行參数绑定
Query query=session.createQuery("from People Where name=:name");
query.setString("name","小明1");
List<People> peoples=query.list();
for(People people:peoples){
System.out.println(people.getName());
}
tx.commit();

3、分页查询

Session session=HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx=session.beginTransaction();
Query query=session.createQuery("from People");
query.setFirstResult(0);//设置查询開始的起始位置
query.setMaxResults(2);//设置查询的数目
List<People> peoples=query.list();
for(People people:peoples){
System.out.println(people.getName());
}
tx.commit();

4、聚集函数的使用

Session session=HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx=session.beginTransaction();
//count()
Query query=session.createQuery("select count(*) from People");
long count=(long) query.uniqueResult();
System.out.println(count);
tx.commit();
//min() max() avg() sum()的使用与count相似

5、in的使用

Session session=HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx=session.beginTransaction();
String names[]={"小明1","小明2","小明3"};
Query query=session.createQuery("from People where name in (:name)");
query.setParameterList("name",names);
List<People> peoples=query.list();
for(People people:peoples){
System.out.println(people.getId());
}
tx.commit();

Hibernate HQL的使用的更多相关文章

  1. weblogic10异常:org.hibernate.hql.ast.HqlToken

    转自:http://www.programgo.com/article/68682994452/ 在做查询的时候,报出  org.hibernate.QueryException: ClassNotF ...

  2. SSH整合报错:org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped[......]

    非常诡异的报错,信息如下:org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [select count(* ...

  3. Hibernate HQL查询:

    Hibernate HQL查询:Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查 ...

  4. Hibernate学习笔记-Hibernate HQL查询

    Session是持久层操作的基础,相当于JDBC中的Connection,通过Session会话来保存.更新.查找数据.session是Hibernate运作的中心,对象的生命周期.事务的管理.数据库 ...

  5. 解决Eclipse编译器报错ClassNotFoundException:Org.hibernate.hql.ast.HqlToken

    最近开发遇到Eclipse编译器老是报出ClassNotFoundException:Org.hibernate.hql.ast.HqlToken [from User Where id=1] 的错误 ...

  6. java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.MethodNode(尼玛,蛋疼的错误)

    java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.MethodNode   \-[M ...

  7. Hibernate HQL详细说明

    1.  Hibernate HQL详细说明 1.1.  hql一个简短的引论 Hibernate它配备了一种非常强大的查询语言.这种语言看起来非常像SQL.但是不要 要对相位的语法结构似,HQL是很有 ...

  8. WebLogic使用SSH架构部署遇到org.hibernate.hql.internal.ast.HqlTok

    其实这个问题在以前就遇到过,当时解决了,但今天在部署一个测试轻应用的时候一直没有想起来,特此记录一下. 这个问题出现在使用WebLogic(我使用的是10.3.5版本)发布SSH架构的应用.在操作数据 ...

  9. Hibernate HQL查询语句总结

    Hibernate HQL查询语句总结 1. 实体查询:有关实体查询技术,其实我们在先前已经有多次涉及,比如下面的例子:String hql="from User user ";L ...

  10. hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken

    环境:weblogic10.3.5,hibernate3,GGTS(groovy/grails tools suite):出现这问题是因为该项目是从weblogic8.1.6下移植到weblogic1 ...

随机推荐

  1. 潭州课堂25班:Ph201805201 WEB 之 页面编写 第二课 (课堂笔记)

    index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  2. 使用time模块,转化时间格式

    import time ''' 时间戳:表示1970年开始计算的偏移量.我们运用type(时间戳)是float类型 结构化时间:9个元素组成的数组 格式化时间字符串 ''' '''获取当前时间戳''' ...

  3. Leaflet_扩展Leaflet:类(2017-10-26)

    扩展教程:http://leafletjs.com/examples/extending/extending-1-classes.html 翻译 该教程介绍扩展Leaflet最常用的方式. 注意:本文 ...

  4. 树莓派.Qt.Creator安装方法

    树莓派硬件: Raspberry Pi 3 B 树莓派系统: Linux version 4.9.59-v7+ (32位) Qt版本(x86版本--32位): 安装过程 可以查看软件仓库支持的版本: ...

  5. C# Dictionary<TKey,TValue>如何添加键重复的内容

    这里以Dictionary<string,string>为例 当我们实例化Dictionary<string,string>集合时,其中有一个重载构造方法如下: // // 摘 ...

  6. 【GPU编解码】GPU硬编码 (转)

    一.OpenCV中的硬编码 OpenCV2.4.6中,已实现利用GPU进行写视频,编码过程由cv::gpu::VideoWriter_GPU完成,其示例程序如下. 1 int main(int arg ...

  7. 树莓派(RespberryPi)安装手记

    购买了两台树莓派,显示器接口是HDMI的,所以需要HDMI高清线连接到显示器,再加上SD卡做硬盘以及无线USB-WIFI,就可以玩一玩树莓派这个小东西了.以下是安装手记. 首先是制作“启动光盘”,其实 ...

  8. 【工具】使用markdown写ppt

    见识到一个新工具,markdown写ppt,支持多平台:https://yhatt.github.io/marp/ 看起来是一个不错的小工具,有兴趣可以尝试一下.

  9. 使用log4net将C#日志发送到Elasticsearch

    一.安装Elasticsearch 参考前面写的文章:https://www.cnblogs.com/songxingzhu/p/7909486.html 安装完Elasticsearch后,修改/e ...

  10. Android官方开发文档Training系列课程中文版:性能优化建议

    原文地址:http://android.xsoftlab.net/training/articles/perf-tips.html 本篇文章主要介绍那些能够提升总体性能的微小优化点.它与那些能突然改观 ...