QBC检索和本地SQL检索
QBC查询就是通过使用Hibernate提供的Query By Criteria API来查询对象,这种API封装了SQL语句的动态拼装,对查询提供了更加面向对象的功能接口
本地SQL查询来完善HQL不能涵盖所有的查询特性
关于QBC列出了4部分内容:
第一个:是一个QBC的helloworld程序
如何创建一个criteria对象,如何添加查询条件
@Test
public void testQBC(){
//1.创建一个Criteria对象
Criteria criteria = session.createCriteria(Employee.class);
//2.添加查询条件:在QBC中查询条件使用Criterion来表示 标准
//Criterion可以通过Restrictions的静态方法得到 限制
criteria.add(Restrictions.eq("email", "SKUMAR"));
criteria.add(Restrictions.gt("salary", 5000F));
//3.执行查询
Employee employee = (Employee) criteria.uniqueResult();
System.out.println(employee);
// 控制台打印:
// Hibernate:
// select
// this_.id as id1_1_0_,
// this_.NAME as NAME2_1_0_,
// this_.SALARY as SALARY3_1_0_,
// this_.EMAIL as EMAIL4_1_0_,
// this_.DEPT_ID as DEPT_ID5_1_0_
// from
// GG_EMPLOYEE this_
// where
// this_.EMAIL=?
// and this_.SALARY>?
}
第二个:添加复杂的查询条件
添加AND 和 OR还有 AND和OR如何表示 Conjunction Disjunction
@Test
public void testQBC2(){
//1.创建一个Criteria对象
Criteria criteria = session.createCriteria(Employee.class);
//1.AND :使用 Conjunction表示 关联 Restrictions 限制
//Conjunction 本身就是一个Cirterion对象:关联
//且其中还可以添加Criterion对象
Conjunction conjunction = Restrictions.conjunction();
conjunction.add(Restrictions.like("name", "a", MatchMode.ANYWHERE));
Department dept = new Department();
dept.setId(80);
conjunction.add(Restrictions.eq("dept", dept));
System.out.println(conjunction);
//2.OR Disjunction:析取
Disjunction disjunction = Restrictions.disjunction();
disjunction.add(Restrictions.ge("salary", 6000F));
disjunction.add(Restrictions.isNull("email"));
criteria.add(disjunction);
criteria.add(conjunction);
criteria.list();
// 控制台打印语句:
// Hibernate:
// select
// this_.id as id1_1_0_,
// this_.NAME as NAME2_1_0_,
// this_.SALARY as SALARY3_1_0_,
// this_.EMAIL as EMAIL4_1_0_,
// this_.DEPT_ID as DEPT_ID5_1_0_
// from
// GG_EMPLOYEE this_
// where
// (
// this_.SALARY>=?
// or this_.EMAIL is null
// )
// and (
// this_.NAME like ?
// and this_.DEPT_ID=?
// )
}
第三个,统计查询 ,排序和分页
@Test
//统计查询: Projection 投影
public void testQBC3(){
Criteria criteria = session.createCriteria(Employee.class);
//统计查询:使用Projection 来表示: 可以由Projections的静态方法得到
criteria.setProjection(Projections.max("salary"));
System.out.println(criteria.uniqueResult());
// Hibernate:
// select
// max(this_.SALARY) as y0_
// from
// GG_EMPLOYEE this_
}
@Test
public void testQBC4(){
Criteria criteria = session.createCriteria(Employee.class);
//1.添加排序
criteria.addOrder(Order.asc("salary"));
criteria.addOrder(Order.desc("email"));
//2.添加翻页方法
int pageSize=5;
int pageNo=3;
criteria.setFirstResult((pageNo-1)*pageSize)
.setMaxResults(pageSize).list();
// Hibernate:
// select
// this_.id as id1_1_0_,
// this_.NAME as NAME2_1_0_,
// this_.SALARY as SALARY3_1_0_,
// this_.EMAIL as EMAIL4_1_0_,
// this_.DEPT_ID as DEPT_ID5_1_0_
// from
// GG_EMPLOYEE this_
// order by
// this_.SALARY asc,
// this_.EMAIL desc limit ?,
// ?
}
更加复杂的QBC查询参看技术文档E:\jar包\hibernate-release-4.2.16.Final\documentation\manual\en-US\html_single\index.html
QBC检索和本地SQL检索
QBC查询是通过使用Hibernate提供的Query By Criteria APi来查询对象,这种API封装了SQL语句的动态拼装,对查询提供了更加面向对象的功能接口
本地SQL查询来完善HQL不能涵盖所有的查询特性
@Test
public void testNativeSQL(){
String sql = "INSERT INTO gg_department VALUES(?,?)";
Query query = session.createSQLQuery(sql);
query.setInteger(0, 280)
.setString(1, "ATGUIGU")
.executeUpdate();
// Hibernate:
// INSERT
// INTO
// gg_department
//
// VALUES
// (?,?)
}
//HQL也支持删除和修改操作
@Test
public void testHQLUpdate(){
String hql = "delete from Department d where d.id = :id";
session.createQuery(hql).setInteger("id",280)
.executeUpdate();
// Hibernate:
// delete
// from
// GG_DEPARTMENT
// where
// ID=?
}