ssh学习:hibernate中QBC查询

Hibernate中QBC查询

QBC:Query by Criteria 标准化查询.使用到的接口: Criteria、Critertion(条件)、Order(排序)、Projection(映射) 以及DetachedCriteria(离线)

获取Criteria对象

 

        //从session中获取Criteria接口对象
	Criteria criteria = session.createCriteria(User.class);
 设置查询的条件

 

 

//Critertion 条件查询 利用Restrictions实现
	Criterion ilike = Restrictions.ilike("name", "%");
	Criterion between = Restrictions.between("id", 1, 2);
	//添加查询的条件:下面二选一
	Criterion and = Restrictions.and(ilike,between);//可以一次添加多个
	Criteria criteria2 = criteria.add(and);
	//Criteria criteria3 = criteria.add(between).add(ilike);
 对查询进行排序:
	// order
	criteria.addOrder(org.hibernate.criterion.Order.desc("name"));
 映射查询:
// 映射 projection 常用的函数sun,avg,max,min,count等
	Criteria criteria4 = criteria.setProjection(Projections.count("id"));
 离线查询:

 

 

// 离线查询 可以随意添加到任意一个session当中
	DetachedCriteria detachedCriteria = DetachedCriteria.forClass(User.class);
	// 将离线查询加载到需要加载的session上
	Criteria criteria1 = detachedCriteria.getExecutableCriteria(session);
 

你可能感兴趣的:(ssh学习:hibernate中QBC查询)