Hibernate检索方式和Criteria查询的讲解

 Hibernate框架5种检索方式

     1.导航对象图检索方式:根据已经加载的对象导航到其他对象

     2.OID检索方式:按照对象的OID来检索对象

     3.HQL检索方式:使用面向对象的HQL查询语言

     4.QBC检索方式:使用QBC(Query By Criteria)API来检索对象,这种API封装了基于字符串形式的查询语句,提供了更加面向对象的查询接口

     5.本地SQL检索方式:使用本地数据库的SQL查询语句 这次重点讲解Criteria查询,首先我们先了解一下它。  Criteria 查询采用面向对象方式封装查询条件,又称为对象查询,对SQL语句进行封装
  采用对象的方式来组合各种查询条件,由Hibernate 自动产生SQL 查询语句Criteria由Hibernate Session进行创建。
   0. HibernateUtil辅助类的代码如下

 

package entity;

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

public class HibernateUtil {
private static final ThreadLocal sessionTL=new ThreadLocal();
private static Configuration conf;
private final static SessionFactory sf;
static{
    try {
        conf=new Configuration().configure();
        sf=conf.buildSessionFactory();
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new ExceptionInInitializerError(ex);
    }
    
}
public static Session currentSession(){
    Session session=(Session)sessionTL.get();
    if (session==null) {
        session=sf.openSession();
        sessionTL.set(session);
    }
    return session;
    
    
}
public static void closeSession(){
    Session session=(Session)sessionTL.get();
    sessionTL.set(null);
    session.close();
    
}
}

 

 

 

 

1.无条件查询

 

    /*
     * 无条件查询
     */
@Test
public void criTest(){
    Session session = HibernateUtil.currentSession();
    Transaction tx = session.beginTransaction();
    Criteria criteria = session.createCriteria(Dept.class);
    List<Dept> list=criteria.list();
    for (Dept dept : list) {
        System.out.println(dept.getDeptName());
    }
    tx.commit();
     HibernateUtil.closeSession();
    
}

 

2.比较运算

/*
     * 比较运算
     * 条件查询
     */
    @Test
public void criTestByname(){
    Session session = HibernateUtil.currentSession();
    Transaction tx = session.beginTransaction();
    List<Dept> list = session.createCriteria(Dept.class).add(Restrictions.eq("deptName", "测试部")).list();
    for (Dept dept : list) {
        System.out.println(dept.getDeptName());
    }
    tx.commit();
     HibernateUtil.closeSession();
    
}
   

3.别名查询

/*
     * 别名查询
     */
    @Test
    public void criNamedTest(){
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(Emp.class);
        criteria.createAlias("dept", "d");//把emp中dept属性命名为d
        List<Emp> list=criteria.add(Restrictions.eq("d.deptName", "测试部")).list();
        for (Emp emp : list) {
            System.out.println(emp.getEmpName());
        }
        tx.commit();
         HibernateUtil.closeSession();
        
    }

4.范围运算

/*
     * 范围运算
     * Restrictions.in查询
     */
    @Test
    public void criInTest(){
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(Emp.class);
        List<String> loclist=new ArrayList<>();
        loclist.add("bj");
        List<Emp> list=criteria.add(Restrictions.in("loc", loclist)).list();
        for (Emp emp : list) {
            System.out.println(emp.getEmpName());
        }
        tx.commit();
         HibernateUtil.closeSession();
        
    }

5.模糊查询

/*
     * 字符串模式匹配(模糊查询)
     */
    @Test
    public void criLikeTest(){
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(Emp.class);
    List<Emp> list=    criteria.add(Restrictions.like("loc", "%b%")).list();
    //下面和上面实现功能一样,只不过忽略大小写
    //List<Emp> list=    criteria.add(Restrictions.ilike("loc", "b",MatchMode.ANYWHERE)).list();
        for (Emp emp : list) {
            System.out.println(emp.getEmpName());
        }
        tx.commit();
         HibernateUtil.closeSession();
        
    }

6.逻辑运算

/*
     * 逻辑运算 and or not
     */
    @Test
    public void criAndTest(){
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(Emp.class).add(Restrictions.and(Restrictions.eq("empName", "小乐"),Restrictions.like("loc", "%b%")));    
        List<Emp> list = criteria.list();    
        for (Emp emp : list) {
            System.out.println(emp.getEmpNo());
        }
        tx.commit();
         HibernateUtil.closeSession();
    }

7.动态查询

/*
     * 动态查询
     */
    @Test
    public void cridynamicTest(){
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        Emp empcri=new Emp();
        empcri.setEmpName("小乐");
        empcri.setEmpNo(0);
        Criteria criteria = session.createCriteria(Emp.class);    
        if (empcri.getEmpName()!=null) {
            //用户填写姓名作为条件
            criteria.add(Restrictions.eq("empName", empcri.getEmpName()));
        }
        if (empcri.getLoc()!=null) {
            criteria.add(Restrictions.gt("empNo", empcri.getEmpNo()));
        }
        List<Emp> list = criteria.list();    
        for (Emp emp : list) {
            System.out.println(emp.getEmpNo());
        }
        tx.commit();
         HibernateUtil.closeSession();
    }

8.排序

/*
     * 排序
     */
    @Test
    public void criOrderTest(){
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(Emp.class);    
        List<Emp> list = criteria.addOrder(Order.asc("empNo")).list();
        for (Emp emp : list) {
            System.out.println(emp.getEmpName());
        }
        tx.commit();
         HibernateUtil.closeSession();
    }

9.分页

/*
     * 分页
     */
    @Test
    public void  criPageTest(){
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(Emp.class);    
        int pageIndex=2;//当前页码
        int pageSize=2;//每页显示数据量
        criteria.setFirstResult((pageIndex-1)*pageSize);
        criteria.setMaxResults(pageSize);
        List<Emp> list =criteria.list();
        for (Emp emp : list) {
            System.out.println(emp.getEmpName());
        }
        tx.commit();
         HibernateUtil.closeSession();
        
    }

 

10.detachedCriteria查询

/*
     * detachedCriteria查询
     */
    @Test
    public void detachedCriTest(){
        Session session = HibernateUtil.currentSession();
        DetachedCriteria dc=DetachedCriteria.forClass(Emp.class,"e");
        dc.createAlias("e.dept", "d").add(Restrictions.eq("d.deptName", "测试部"));
    List<Emp> list=dc.getExecutableCriteria(session).list();
        for (Emp emp : list) {
            System.out.println(emp.getEmpName());
        }
         HibernateUtil.closeSession();
         
    }

 

posted @ 2016-08-22 17:06  柒陌枫  阅读(1168)  评论(0编辑  收藏  举报