Hibernate的DetachedCriteria使用(含Criteria)
1、背景了解:Hibernate的三种查询方式
1.1 HQL(Hibernate Query Language)
@Override
public SysUser findUserByLoginName(String pLoginName) {
String hql = "from SysUser as u where u.loginName = ?";
List<SysUser> users = getHibernateTemplate().find(hql, pLoginName); //pLoginName对应?
return users.isEmpty() ? null : users.get(0);
}
@Override
public SysUser findUserByLoginName(String pLoginName) {
String hql = "from SysUser as u where u.loginName = ?";
List<SysUser> users = getHibernateTemplate().find(hql, pLoginName); //pLoginName对应?
return users.isEmpty() ? null : users.get(0);
}
1.2 SQL(Structured Query Language)
static List sql() {
Session s = HibernateUtil.getSession();
Query q = s.createSQLQuery("select * from user").addEntity(User.class);
List<User> rs = q.list();
s.close();
return rs;
}
static List sql() {
Session s = HibernateUtil.getSession();
Query q = s.createSQLQuery("select * from user").addEntity(User.class);
List<User> rs = q.list();
s.close();
return rs;
}
1.3 QBC(Query By Criteria)
1.3.1 QBC查询的基本步骤
- 使用Session实例的createCriteria()方法创建Criteria对象;
- 使用工具类Restrictions的方法为Criteria对象设置查询条件,Order工具类的方法设置排序方式,Projections工具类的方法进行统计和分组;
- 使用Criteria对象的list()方法进行查询并返回结果。
1.3.2 Restrictions、Order、Projections的常用方法
返回值类型 | 方法名称 | 描述 |
SimpleExpression | Restrictions.eq | 等于(equal) |
Criterion | Restrictions.allEq | 使用Map,Key/Valu进行多个等于的比对 |
SimpleExpression | Restrictions.gt | 大于(great than) |
SimpleExpression | Restrictions.ge | 大于等于(great than or equal) |
SimpleExpression | Restrictions.lt | 小于(less than) |
SimpleExpression | Restrictions.le | 小于等于(less than or equal) |
Criterion | Restrictions.between | 对应SQL的between |
SimpleExpression | Restrictions.like | 对应SQL的like |
Criterion | Restrictions.in | 对应SQL的in |
LogicalExpression | Restrictions.and | and关系 |
LogicalExpression | Restrictions.or | or关系 |
Criterion | Restrictions.isNull | 为空 |
Criterion | Restrictions.sqlRestriction | SQL限定查询 |
返回值类型 | 方法名称 | 描述 |
Order | Order.asc | 升序 |
Order | Order.desc | 降序 |
返回值类型 | 方法名称 | 描述 |
AggregateProjection | Projections.avg | 求平均值 |
CountProjection | Projections.count | 统计某属性的数量 |
CountProjection | Projections.countDistinct | 统计某属性不同值的数量 |
PropertyProjection | Projections.groupProperty | 指定某个属性为分组属性 |
AggregateProjection | Projections.max | 求最大值 |
AggregateProjection | Projections.min | 求最小值 |
ProjectionList | Projections.projectionList | 创建一个ProjectionList对象 |
Projection | Projections.rowCount | 查询结果集中的记录条数 |
AggregateProjection | Projections.sum | 求某属性的合计 |
1.3.3 QBC的查询示例和基本理解
//查询匹配的账户adminList
Criteria c=s.createCriteria(Admin.class);
c.add(Restrictions.eq("aname",name));//eq是等于,gt是大于,lt是小于,or是或
c.add(Restrictions.eq("apassword", password));
List<Admin> list=c.list();
//查询匹配的账户adminList
Criteria c=s.createCriteria(Admin.class);
c.add(Restrictions.eq("aname",name));//eq是等于,gt是大于,lt是小于,or是或
c.add(Restrictions.eq("apassword", password));
List<Admin> list=c.list();
//分页查询前10条
Criteria criteria = session.createCriteria(Customer.class);
criteria.addOrder( Order.asc("name") ); //排序方式
criteria.setFirstResult(0);
criteria.setMaxResults(10);
List result = criteria.list()
//分页查询前10条
Criteria criteria = session.createCriteria(Customer.class);
criteria.addOrder( Order.asc("name") ); //排序方式
criteria.setFirstResult(0);
criteria.setMaxResults(10);
List result = criteria.list()
//查找age等于(eq)20或(or)age为空(isNull)的User
Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.or( Restrictions.eq("age", new Integer(20)), Restrictions.isNull("age") ));
List users = criteria.list();
//实际上它产生的对应的sql如下
Hibernate:
select
this_.id as id0_0_, this_.name as name0_0_, this_.age as age0_0_
from
T_USER this_
where
(this_.age=? or this_.age is null)
//查找age等于(eq)20或(or)age为空(isNull)的User
Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.or( Restrictions.eq("age", new Integer(20)), Restrictions.isNull("age") ));
List users = criteria.list();
//实际上它产生的对应的sql如下
Hibernate:
select
this_.id as id0_0_, this_.name as name0_0_, this_.age as age0_0_
from
T_USER this_
where
(this_.age=? or this_.age is null)
1.3.4 复合查询
Criteria criteria = session.createCriteria(Clazz.class);
Criteria criteriaInner = criteria.createCriteria(Student.class);
criteriaInner.add(Restrictions.eq("name", "Bob"));
List clazzList = criteria.list();
Criteria criteria = session.createCriteria(Clazz.class);
Criteria criteriaInner = criteria.createCriteria(Student.class);
criteriaInner.add(Restrictions.eq("name", "Bob"));
List clazzList = criteria.list();
Member member = MemberHelper.getCurrentLoginMember();
DetachedCriteria criteria = DetachedCriteria.forClass(Picture.class);
DetachedCriteria collectCriteria = criteria.createCriteria("collectRecordList");
collectCriteria.add(Restrictions.eq("member", member));
List<Picture> pictureList = Picture.listByCriteria(criteria, page, Order.desc("updateDate"));
Member member = MemberHelper.getCurrentLoginMember();
DetachedCriteria criteria = DetachedCriteria.forClass(Picture.class);
DetachedCriteria collectCriteria = criteria.createCriteria("collectRecordList");
collectCriteria.add(Restrictions.eq("member", member));
List<Picture> pictureList = Picture.listByCriteria(criteria, page, Order.desc("updateDate"));
1.3.5 Hibernate中Criteria方式的基本使用流程
- Criteria创建 --> session.createCriteria(Class persistentClass) (更多重载参考)
- 条件添加 --> add(Criterion criterion) 限定条件、 addOrder(Order order) 限定顺序、 setProjection(Projection projection) 限定统计动作
- 结果返回 --> list()
2、DetachedCriteria是什么,和Criteria有什么区别
3、DetachedCriteria的基本使用
//查询id为1且在今天或今天之前出生的user的名单
//1、创建DetachedCriteria并设置条件
DetachedCriteria dc = DetachedCriteria.forClass(User.class);
int id = 1;
dc.add(Restrictions.eq("id", id));
Date age = new Date();
dc.add(Restrictions.le("birthday", age));
//2、执行查询(Criteria getExecutableCriteria(Session session))
Session session = HibernateUtil.getSession();
Criteria c = dc.getExecutableCriteria(session);
List users = c.list();
//查询id为1且在今天或今天之前出生的user的名单
//1、创建DetachedCriteria并设置条件
DetachedCriteria dc = DetachedCriteria.forClass(User.class);
int id = 1;
dc.add(Restrictions.eq("id", id));
Date age = new Date();
dc.add(Restrictions.le("birthday", age));
//2、执行查询(Criteria getExecutableCriteria(Session session))
Session session = HibernateUtil.getSession();
Criteria c = dc.getExecutableCriteria(session);
List users = c.list();
org.hibernate.criterion
Class DetachedCriteria
getExecutableCriteria
public Criteria getExecutableCriteria(Session session)
Get an executable instance of Criteria, to actually run the query.
org.hibernate.criterion
Class DetachedCriteria
getExecutableCriteria
public Criteria getExecutableCriteria(Session session)
Get an executable instance of Criteria, to actually run the query.
4、参考链接
Hibernate的DetachedCriteria使用(含Criteria)的更多相关文章
- Hibernate的DetachedCriteria使用(含Criteria)转载
https://www.cnblogs.com/deng-cc/p/6428599.html 1.背景了解:Hibernate的三种查询方式 Hibernate总的来说共有三种查询方式:HQL.QBC ...
- JavaWeb_(Hibernate框架)Hibernate中数据查询语句Criteria基本用法
Criteria进行数据查询与HQL和SQL的区别是Criteria完全是面向对象的方式在进行数据查询,将不再看到有sql语句的痕迹,使用Criteria 查询数据包括以下步骤: 1. 通过sessi ...
- 五 Hibernate的其他API,Query&Criteria&SQLQuery
Query Criteria SQLQuery Query接口:用于接收HQL,用于查询多个对象 HQL:Hibernate Query Language Query条件查询: Query分页查询: ...
- Hibernate 、Hql查询和Criteria查询
HQL查询: public Object query(String name){ Session s=null; try{ s=HibernateSessionFactory.getSession() ...
- hibernate的QBC查询之Criteria用法
//return (DeliverCost) super.getSession().createCriteria(getMyClass()).add(Restrictions.eq("isd ...
- 转:Hibernate中Criteria和DetachedCriteria的完整用法
原文地址:http://blog.sina.com.cn/s/blog_667528fd0100rkrf.html 设计上可以灵活的根据 Criteria 的特点来方便地进行查询条件的组装.现在对 H ...
- Hibernate中Criteria的完整用法
1,CriteriaHibernate 设计了 CriteriaSpecification 作为 Criteria 的父接口,下面提供了 Criteria和DetachedCriteria .2,De ...
- 【hibernate criteria】hibernate中criteria的完整用法 转
---恢复内容开始--- 转自:http://www.360doc.com/content/090313/10/26262_2794855.html 1.Criteria Hibernate 设计了 ...
- Hibernate框架之Criteria查询
首先给大家说说Hibernate检索方式 Hibernate提供了5种检索对象的方式 1.导航对象图检索方式:根据已经加载的对象导航到其他对象 2.OID检索方式:按照对象的OID来检索对象 3.HQ ...
随机推荐
- 【Ubuntu 16】安装ssh
ubuntu16默认使用ufw(Uncomplicated FireWall 继承自 iptables)管理防火墙,使用gufw图形化界面方便管理, 默认情况下openssh-server没有安装. ...
- hdu 6171---Admiral(双向搜索)
题目链接 Problem Description Suppose that you are an admiral of a famous naval troop. Our naval forces h ...
- 【Java学习笔记之二十九】Java中的"equals"和"=="的用法及区别
Java中的"equals"和"=="的用法及区别 在初学Java时,可能会经常碰到下面的代码: String str1 = new String(" ...
- poj2912 Rochambeau
Description N children are playing Rochambeau (scissors-rock-cloth) game with you. One of them is th ...
- TCP协议三次握手与四次挥手通俗解析
TCP/IP协议三次握手与四次握手流程解析 一.TCP报文格式 TCP/IP协议的详细信息参看<TCP/IP协议详解>三卷本.下面是TCP报文格式图: 图1 TCP报文格式 上图中有几个字 ...
- Spark Structured Streaming框架(2)之数据输入源详解
Spark Structured Streaming目前的2.1.0版本只支持输入源:File.kafka和socket. 1. Socket Socket方式是最简单的数据输入源,如Quick ex ...
- cacti监控系统
cacti 1. cacti安装 IP: 172.25.44.1 环境: Red Hat 6.5 镜像: rhel-server-6.5-x86_64-dvd.iso 火墙: /etc/init.d/ ...
- Java基础---IO(二)--File类、Properties类、打印流、序列流(合并流)
第一讲 File类 一.概述 1.File类:文件和目录路径名的抽象表现形式 2.特点: 1)用来将文件或文件夹封装成对象 2)方便于对文件与文件夹的属性信息进行操作 3)File类的实例是不 ...
- 在ubantu上安装hive
上次我们在ubantu上安装了hadoop,那我们现在再进一步,开始我们的学习之旅--hive板块吧! 第一步:准备! 软件: I.一个搭建好的hadoop环境 II.hive的bin文件(前往apa ...
- 深入剖析ConcurrentHashMap 一
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt201 ConcurrentHashMap是Java5中新增加的一个线程安全的 ...