欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

hibernate框架学习笔记11:Criteria查询详解

程序员文章站 2022-07-01 08:44:21
创建实体类对象: package domain; import java.util.HashSet; import java.util.Set; //客户实体 public class Customer { private Long cust_id; private String cust_name ......

创建实体类对象:

hibernate框架学习笔记11:Criteria查询详解
package domain;

import java.util.HashSet;
import java.util.Set;

//客户实体
public class Customer {
    
    private Long cust_id;
    private String cust_name;
    private String cust_source;
    private String cust_industry;
    private String cust_level;
    private String cust_linkman;
    private String cust_phone;
    private String cust_mobile;
    //使用set集合,表达一对多关系
    private Set<LinkMan> linkMens = new HashSet<LinkMan>();

    public Set<LinkMan> getLinkMens() {
        return linkMens;
    }
    public void setLinkMens(Set<LinkMan> linkMens) {
        this.linkMens = linkMens;
    }
    public Long getCust_id() {
        return cust_id;
    }
    public void setCust_id(Long cust_id) {
        this.cust_id = cust_id;
    }
    public String getCust_name() {
        return cust_name;
    }
    public void setCust_name(String cust_name) {
        this.cust_name = cust_name;
    }
    public String getCust_source() {
        return cust_source;
    }
    public void setCust_source(String cust_source) {
        this.cust_source = cust_source;
    }
    public String getCust_industry() {
        return cust_industry;
    }
    public void setCust_industry(String cust_industry) {
        this.cust_industry = cust_industry;
    }
    public String getCust_level() {
        return cust_level;
    }
    public void setCust_level(String cust_level) {
        this.cust_level = cust_level;
    }
    public String getCust_linkman() {
        return cust_linkman;
    }
    public void setCust_linkman(String cust_linkman) {
        this.cust_linkman = cust_linkman;
    }
    public String getCust_phone() {
        return cust_phone;
    }
    public void setCust_phone(String cust_phone) {
        this.cust_phone = cust_phone;
    }
    public String getCust_mobile() {
        return cust_mobile;
    }
    public void setCust_mobile(String cust_mobile) {
        this.cust_mobile = cust_mobile;
    }
    @Override
    public String toString() {
        return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]";
    }
}
View Code

 

自定义工具类:

hibernate框架学习笔记11:Criteria查询详解
package utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {
    private static SessionFactory sf;
    
    static{
        //1 创建,调用空参构造
        Configuration conf = new Configuration().configure();
        //2 根据配置信息,创建 SessionFactory对象
         sf = conf.buildSessionFactory();
    }
    
    //获得session => 获得全新session
    public static Session openSession(){
                //3 获得session
                Session session = sf.openSession();
                
                return session;
        
    }
    //获得session => 获得与线程绑定的session
    public static Session getCurrentSession(){
        //3 获得session
        Session session = sf.getCurrentSession();
        
        return session;
    }    
}
View Code

 

测试类示例:

基本语法:

package criteria;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;

import domain.Customer;
import utils.HibernateUtils;

//学习Criteria语法
public class Demo {
    
    @Test
    //基本语法
    public void fun1(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        
        Criteria c = session.createCriteria(Customer.class);
        
        List<Customer> list = c.list();
        
        System.out.println(list);
        
        //----------------------------------------------------
        tx.commit();
        session.close();
        
    }
    
    @Test
    //条件语法
    // >                 gt
    // >=                ge
    // <                lt
    // <=                le
    // ==                eq
    // !=                ne
    // in                in
    // between and        between
    // like             like
    // is not null         isNotNull
    // is null            isNull
    // or                or
    // and                and
    public void fun2(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        
        Criteria c = session.createCriteria(Customer.class);
        
//        c.add(Restrictions.idEq(2l));
        c.add(Restrictions.eq("cust_id",2l));
        
        List<Customer> list = c.list();
        
        System.out.println(list);
        
        //----------------------------------------------------
        tx.commit();
        session.close();
        
    }
    
    @Test
    //分页语法 - 与HQL一样
    public void fun3(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        
        Criteria c = session.createCriteria(Customer.class);
        //limit ?,? 
        c.setFirstResult(0);
        c.setMaxResults(2);
        
        List<Customer> list = c.list();
        
        System.out.println(list);
        
        //----------------------------------------------------
        tx.commit();
        session.close();
        
    }
    
    @Test
    //排序语法 
    public void fun4(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        
        Criteria c = session.createCriteria(Customer.class);
        
        c.addOrder(Order.asc("cust_id"));//升序
        //c.addOrder(Order.desc("cust_id"));降序
        
        List<Customer> list = c.list();
        
        System.out.println(list);
        
        //----------------------------------------------------
        tx.commit();
        session.close();
        
    }
    
    @Test
    //统计语法 
    public void fun5(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        
        Criteria c = session.createCriteria(Customer.class);
        
        //设置查询目标(聚合函数)
        c.setProjection(Projections.rowCount());
        
        List list = c.list();
        
        System.out.println(list);
        
        //----------------------------------------------------
        tx.commit();
        session.close();
        
    }
    
    
}

 

 

离线查询:

 

传统criteria对象创建依赖于session,前端传递查询条件依次经过WEB、SERVICE层到DAO层,

在DAO层组装查询条件到criteria后执行查询

 

离线的criteria对象,于session无关,凭空创建,可以在WEB层创建criteria对象并组装查询条件,

这时候直接传递criteria对象到DAO层查询即可,这时候就做到了一个DAO方法适用于所有查询

 

package criteria;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;

import domain.Customer;
import utils.HibernateUtils;

//学习离线Criteria
public class Demo2 {
    
    @Test
    public void fun1(){
        //Service/web层
        DetachedCriteria dc  = DetachedCriteria.forClass(Customer.class);
        
        dc.add(Restrictions.idEq(5l));//拼装条件(全部与普通Criteria一致)
        
        //----------------------------------------------------
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        //Dao层
        Criteria c = dc.getExecutableCriteria(session);
        
        List list = c.list();
        
        System.out.println(list);
        //----------------------------------------------------
        tx.commit();
        session.close();
        
    }
    
}