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. PS文字生成头像

    链接: 教你用PS把文字拼成肖像.... 28个文字组成的肖像画欣赏

  2. windows 64位环境下php执行环境部署配置

    1.下载安装包 地址可以网上找,我下载的是php-5.6.27-Win32-VC11-x64.zip 2.解压安装包,我的解压到D:\tools\php5.6 3.配置php.ini 在解压的目录中, ...

  3. centos7 设置tomcat自启动

    1 .vi  /etc/init.d/tomcat8 #!/bin/bash # # tomcat startup script for the Tomcat server # # chkconfig ...

  4. WebService服务发布与使用(JDK自带WebService)

    简单粗暴,直接上步骤 一.先建立一个web项目,名字叫MyService 名字为MyService 新建Java类 package com.webService; import javax.jws.W ...

  5. 理解TCP之Keepalive

    理解HTTP之keep-alive 在前面一篇文章中讲了TCP的keepalive,这篇文章再讲讲HTTP层面keep-alive.两种keepalive在拼写上面就是不一样的,只是发音一样,于是乎大 ...

  6. maven 引入外部jar包的几种方式(转)

    原文链接: maven 引入外部jar包的几种方式 方式1:dependency 本地jar包 <dependency> <groupId>com.hope.cloud< ...

  7. Java 8 学习资料汇总【转载】

    原文地址 2014年3月18日,Java SE 8 发布,而 Java 9 预期2016年发布: 2011年7月7日,Java 7 发布,是2009年4月20日被Oracle 以74亿美元收购首次推出 ...

  8. (98)Address already in use: make_sock: could not bind to address 80 [resolved] (2012-10-11 09:04)

    以前遇到一个问题: sudo /etc/init.d/apache2 start * Starting web server apache2 apache2: Could not reliably d ...

  9. A SimpleDataStore

    import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; impor ...

  10. 利用useragent判断移动设备

    用 User Agent 判断移动设备 WebApp除了做成响应式设计以外,还可以给移动设备做一套UI,PC端再做一套UI,然后在前台进行判断进行相应的跳转.判断是否是移动设备一般根据浏览器的user ...