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. 在Windows系统配置Jekyll

    Jekyll 是一个简单的网站静态页面生成工具.由于是用Ruby语音编写的,所以在Windows系统上配置起来还是稍微有点繁琐的.具体过程如下: 安装Ruby:在Windows系统上当然使用rubyi ...

  2. 喵哈哈村的魔法考试 Round #18 (Div.2) 题解

    喵哈哈村的古怪石碑(一) 题解:暴力check一下是等比数列还是等差数列,然后输出答案即可.注意如果数据范围是1e9的话,就要快速幂了. 代码: #include <cstdio> #in ...

  3. Spring 3.1 M1: Unified Property Management(转)

    In the first two posts of this series, I described the bean definition profiles feature, and how it ...

  4. Ubuntu GNOME 13.04将关闭窗口的按钮放在最右边

    转载请注明:转自http://blog.csdn.net/u010811449/article/details/9426187 先上图: 首先打开dconf系统配置编译器. 找到 rog -> ...

  5. Linux C Socket TCP编程介绍及实例

    转自:https://blog.csdn.net/lell3538/article/details/53335231 { printf("向服务器发送数据:%s\n",sendbu ...

  6. 用.Net打造一个移动客户端(Android/IOS)的服务端框架NHM(四)——Android端Http访问类(转)

    本章目的 在上一章中,我们利用Hibernate Tools完成了Android Model层的建立,依赖Hibernate Tools的强大功能,自动生成了Model层.在本章,我们将继续我们的项目 ...

  7. Android之官方导航栏之Toolbar(Toolbar+DrawerLayout+ViewPager+PagerSlidingTabStrip)

    通过前几篇文章,我们对Android的导航栏有了一定的了解认识,本次文章将对Toolbar进行综合应用,主要结合DrawerLayout.ViewPager.PagerSlidingTabStrip一 ...

  8. CentOS7中ELK6.2.3安装

      一.配置主机名 hostnamectl set-hostname elk vim /etc/sysconfig/network修改HOSTNAME=elk 安装Java环境:yum install ...

  9. ArrayList vs LinkedList 空间占用

    空间占用上,ArrayList完胜 看下两者的内存占用图   这三个图,横轴是list长度,纵轴是内存占用值.两条蓝线是LinkedList,两条红线是ArrayList,可以看到,LinkedLis ...

  10. gcc/g++ disable warnings in particular include files

    当在编译一个大项目的时候,你想打开所有的Warning,但是打开后发现一堆公共库文件都出现了warning报错.此时如果你想忽略公共库头文件中的warning报错,只需要在编译的时候,将公共库头文件的 ...