课程咨询 :186 8716 1620      qq:2066486918

昆明Java培训 > 达内新闻 > Hibernate:QBC检索方式
  • Hibernate:QBC检索方式

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

  • 昆明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的更新

最新开班日期  |  更多

Java--零基础全日制班

Java--零基础全日制班

开班日期:12/29

Java--零基础业余班

Java--零基础业余班

开班日期:12/29

Java--周末提升班

Java--周末提升班

开班日期:12/29

Java--零基础周末班

Java--零基础周末班

开班日期:12/29

  • 网址:http://km .java.tedu.cn      地址:昆明市官渡区春城路62号证券大厦附楼6楼
  • 课程培训电话:186 8716 1620      qq:2066486918    全国服务监督电话:400-827-0010
  • 服务邮箱 ts@tedu.cn
  • 2001-2016 达内国际公司(TARENA INTERNATIONAL,INC.) 版权所有 京ICP证08000853号-56