Java笔记之hibernate(四):Criteria

0.说在前面

  基于Hibernate(三):HQL项目

1.新建CriteriaTest类

package com.hibernate.demo.test;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.hibernate.demo.bean.Employee;

public class CriteriaTest {

    public static void main(String[] args) {
        //加载配置文件,创建会话工厂对象
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        //创建会话对象
        Session session = sessionFactory.openSession();
        
        //调用方法
        getById(session,2);

        //关闭会话对象
        session.close();
        //关闭会话工厂对象
        sessionFactory.close();
        
    }
    
    /**
     * 根据id查询信息
     * @param session
     * @param id
     */
    private static void getById(Session session,int id){
        //根据Employee.class创建Criteria对象
        Criteria criteria = session.createCriteria(Employee.class);
        //添加条件,empId=id值
        criteria.add(Restrictions.eq("empId", id));
        //获取结果集
        List<Employee> list = criteria.list();
        //遍历结果集
        for (Employee employee : list) {
            System.out.println(employee);
        }
    }
    
    /**
     * 根据name值进行模糊查询
     * @param session
     * @param name
     */
    private static void getByName(Session session,String name){
        //根据Employee.class创建Criteria对象
        Criteria criteria = session.createCriteria(Employee.class);
        //添加条件,empName like "%"+name+"%"
        criteria.add(Restrictions.like("empName", "%"+name+"%"));
        //获取结果集
        List<Employee> list = criteria.list();
        //遍历结果集
        for (Employee employee : list) {
            System.out.println(employee);
        }
    }
    
    /**
     * 降序查询所有数据
     * @param session
     */
    private static void getByOrderDesc(Session session){
        //根据Employee.class创建Criteria对象
        Criteria criteria = session.createCriteria(Employee.class);
        //添加排序,按照empId字段降序排列
        criteria.addOrder(Order.desc("empId"));
        //获取结果集
        List<Employee> list = criteria.list();
        //遍历结果集
        for (Employee employee : list) {
            System.out.println(employee);
        }
    }
    
    /**
     * 升序查询所有数据
     * @param session
     */
    private static void getByOrderAsc(Session session){
        //根据Employee.class创建Criteria对象
        Criteria criteria = session.createCriteria(Employee.class);
        //添加排序,按照empId字段升序排列
        criteria.addOrder(Order.asc("empId"));
        //获取结果集
        List<Employee> 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方法,可以多次调用来添加多个查询条件;

 

posted @ 2020-04-22 10:28  安徒生敲代码  阅读(1623)  评论(0编辑  收藏  举报