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 @   安徒生敲代码  阅读(1627)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示