昆明java培训
达内昆明广州春城路

18487146383

热门课程

Hibernate:QBC检索方式

  • 时间:2016-10-24
  • 发布:昆明Java培训
  • 来源:达内新闻

昆明Java培训机构的老师今天给大家讲Hibernate:QBC检索方式

1.设定各种查询条件

(1)like、gt、排序

@Test

public void testQBC() {

Criteria criteria = session.createCriteria(Employee.class);

criteria.add(Restrictions.like("empName", "a", MatchMode.ANYWHERE));

criteria.add(Restrictions.gt("salary", 1000F));

//排序

criteria.addOrder(Order.desc("salary"));

List list = criteria.list();

System.out.println(list);

}

Hibernate:

select

this_.emp_id as emp1_1_0_,

this_.emp_name as emp2_1_0_,

this_.salary as salary3_1_0_,

this_.dept_id_fk as dept4_1_0_

from

hibernate.employee this_

where

this_.emp_name like ?

and this_.salary>?

order by

this_.salary desc

(2)and、or

public void testQbc2() {

Criteria criteria = session.createCriteria(Employee.class);

Conjunction conjunction = Restrictions.conjunction();

conjunction.add(Restrictions.like("empName", "a", MatchMode.ANYWHERE));

Department department = new Department();

department.setDeptId(6);

conjunction.add(Restrictions.eq("dept", department));

Disjunction disjunction = Restrictions.disjunction();

disjunction.add(Restrictions.gt("salary", 1000F));

disjunction.add(Restrictions.lt("salary", 20000F));

criteria.add(conjunction).add(disjunction);

criteria.list();

}

Hibernate:

select

this_.emp_id as emp1_1_0_,

this_.emp_name as emp2_1_0_,

this_.salary as salary3_1_0_,

this_.dept_id_fk as dept4_1_0_

from

hibernate.employee this_

where

(

this_.emp_name like ?

and this_.dept_id_fk=?

)

and (

this_.salary>?

or this_.salary<?

)

2.分页查询

@Test

public void testQbc4() {

Criteria criteria = session.createCriteria(Employee.class);

//分页

int pageNo = 2;

int pageSize = 4;

List<Employee> list = criteria.setFirstResult((pageNo - 1) * pageSize).setMaxResults(pageSize).list();

}

Hibernate:

select

this_.emp_id as emp1_1_0_,

this_.emp_name as emp2_1_0_,

this_.salary as salary3_1_0_,

this_.dept_id_fk as dept4_1_0_

from

hibernate.employee this_ limit ?,

?

3.聚集函数查询

@Test

public void testQbc3() {

Criteria criteria = session.createCriteria(Employee.class);

criteria.setProjection(Projections.max("salary"));

String maxSalary = criteria.uniqueResult().toString();

System.out.println(maxSalary);

}

Hibernate:

select

max(this_.salary) as y0_

from

hibernate.employee this

昆明Java培训机构的老师小结一下这一期。

(1)创建QBC查询:session.createCriteria()

(2)like关键字:Restrictions.like(),MatchMode.ANYWHERE

(3)排序:criteria.addOrder(),Order.desc、Order.asc

(4)AND:Conjunction conjunction = Restrictions.conjunction()

(5)Or:Disjunction disjunction = Restrictions.disjunction()

(6)聚集函数:criteria.setProjection(Projections.max("salary"))

(7)查询单个对象:criteria.uniqueResult()

(8)查询对象列表:criteria.list()

上一篇:Hibernate:HQL检索方式
下一篇:【昆明Java培训机构】Hibernate的本地SQL和HQL的更新

iPhone摄像头发展史一步一个脚印

腾讯游戏Switch独立销量领先——昆明达内

达内java语言编程学以致用

苹果技术:A11芯片上新菜【达内培训】

选择城市和中心
贵州省

广西省

海南省