criterion java_Java笔记之hibernate(四):Criteria

该篇博客演示了如何利用Hibernate的Criteria API进行不同类型的查询操作,包括按ID查询、按名称模糊查询、以及按ID字段进行升序和降序排序。Criteria API提供了方便的接口来构建查询条件和排序,适用于简单的单表查询。对于更复杂的查询,如多表关联和分组,可能需要使用SQL查询。
摘要由CSDN通过智能技术生成

0.说在前面

1.新建CriteriaTest类

packagecom.hibernate.demo.test;importjava.util.List;importorg.hibernate.Criteria;importorg.hibernate.SessionFactory;importorg.hibernate.cfg.Configuration;importorg.hibernate.classic.Session;importorg.hibernate.criterion.Order;importorg.hibernate.criterion.Restrictions;importcom.hibernate.demo.bean.Employee;public classCriteriaTest {public static voidmain(String[] args) {//加载配置文件,创建会话工厂对象

SessionFactory sessionFactory = newConfiguration().configure().buildSessionFactory();//创建会话对象

Session session =sessionFactory.openSession();//调用方法

getById(session,2);//关闭会话对象

session.close();//关闭会话工厂对象

sessionFactory.close();

}/*** 根据id查询信息

*@paramsession

*@paramid*/

private static void getById(Session session,intid){//根据Employee.class创建Criteria对象

Criteria criteria = session.createCriteria(Employee.class);//添加条件,empId=id值

criteria.add(Restrictions.eq("empId", id));//获取结果集

List list =criteria.list();//遍历结果集

for(Employee employee : list) {

System.out.println(employee);

}

}/*** 根据name值进行模糊查询

*@paramsession

*@paramname*/

private static voidgetByName(Session session,String name){//根据Employee.class创建Criteria对象

Criteria criteria = session.createCriteria(Employee.class);//添加条件,empName like "%"+name+"%"

criteria.add(Restrictions.like("empName", "%"+name+"%"));//获取结果集

List list =criteria.list();//遍历结果集

for(Employee employee : list) {

System.out.println(employee);

}

}/*** 降序查询所有数据

*@paramsession*/

private static voidgetByOrderDesc(Session session){//根据Employee.class创建Criteria对象

Criteria criteria = session.createCriteria(Employee.class);//添加排序,按照empId字段降序排列

criteria.addOrder(Order.desc("empId"));//获取结果集

List list =criteria.list();//遍历结果集

for(Employee employee : list) {

System.out.println(employee);

}

}/*** 升序查询所有数据

*@paramsession*/

private static voidgetByOrderAsc(Session session){//根据Employee.class创建Criteria对象

Criteria criteria = session.createCriteria(Employee.class);//添加排序,按照empId字段升序排列

criteria.addOrder(Order.asc("empId"));//获取结果集

List list =criteria.list();//遍历结果集

for(Employee employee : list) {

System.out.println(employee);

}

}

}

2.其中首先调用的是getById方法并传参---getById(session,2),运行CriteriaTest类

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

SLF4J: Defaulting to no-operation (NOP) logger implementation

SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

Hibernate: select this_.emp_id as emp1_0_0_, this_.emp_name as emp2_0_0_ from t_employee this_ where this_.emp_id=?Employee [empId=2, empName=李四]

3.调用getByName方法并传参---getByName(session,"小"),运行CriteriaTest类

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

SLF4J: Defaulting to no-operation (NOP) logger implementation

SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

Hibernate: select this_.emp_id as emp1_0_0_, this_.emp_name as emp2_0_0_ from t_employee this_ where this_.emp_name like ?Employee [empId=6, empName=小明]

Employee [empId=7, empName=小红]

Employee [empId=8, empName=小兰]

Employee [empId=9, empName=小绿]

4.调用getByOrderDesc方法并传参---getByOrderDesc(session),运行CriteriaTest类

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

SLF4J: Defaulting to no-operation (NOP) logger implementation

SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

Hibernate: select this_.emp_id as emp1_0_0_, this_.emp_name as emp2_0_0_ from t_employee this_ order by this_.emp_id desc

Employee [empId=12, empName=简]

Employee [empId=11, empName=韩梅梅]

Employee [empId=10, empName=李磊]

Employee [empId=9, empName=小绿]

Employee [empId=8, empName=小兰]

Employee [empId=7, empName=小红]

Employee [empId=6, empName=小明]

Employee [empId=5, empName=韩七]

Employee [empId=4, empName=赵六]

Employee [empId=3, empName=王五]

Employee [empId=2, empName=李四]

5.调用getByOrderAsc并传参---getByOrderAsc(session),运行CriteriaTest类

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

SLF4J: Defaulting to no-operation (NOP) logger implementation

SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

Hibernate: select this_.emp_id as emp1_0_0_, this_.emp_name as emp2_0_0_ from t_employee this_ order by this_.emp_id asc

Employee [empId=2, empName=李四]

Employee [empId=3, empName=王五]

Employee [empId=4, empName=赵六]

Employee [empId=5, empName=韩七]

Employee [empId=6, empName=小明]

Employee [empId=7, empName=小红]

Employee [empId=8, empName=小兰]

Employee [empId=9, empName=小绿]

Employee [empId=10, empName=李磊]

Employee [empId=11, empName=韩梅梅]

Employee [empId=12, empName=简]

6.说明

Criteria适合单表查询,较复杂的查询还是需要使用标准的SQL查询,例如多表关联查询,分组等;

Criteria的add方法,可以多次调用来添加多个查询条件;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值