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方法,可以多次調用來添加多個查詢條件;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM