hibernate入门(四)——QBC查询(CriteriaQuery )

标准化对象查询(Query By Criteria):以对象的方式进行查询,将查询语句封装为对象操作。可读性好,符合java程序员的编程习惯。

hibernate5.X 版本中 session.createCriteria() 已经过时。
javax.persistence.criteria.CriteriaQuery 取代。

环境配置等详细内容请看:hibernate入门(一)——基本环境搭建

在包orm.test下创建类 CriteriaTest.java

1.导入的包:

package orm.test;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Subquery;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Test;

import orm.entity.User;
import orm.util.HibernateUtil;

2.详细代码:

public class CriteriaTest {
    @Test // 简单查询
    public void criteriaTest() {
        SessionFactory sessionFactory = null;
        Session session = null;
        Transaction transaction = null;

        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.getCurrentSession();
            transaction = session.beginTransaction();
            // 获取  CriteriaBuilder
            CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
            // 创建  CriteriaQuery
            CriteriaQuery<User> criteria = criteriaBuilder.createQuery(User.class);
            criteria.from(User.class);
            List<User> list = session.createQuery(criteria).getResultList();
            for (User user : list) {
                System.out.println(user);
            }
            transaction.commit();
        } catch (HibernateException e) {
            if (transaction != null) {
                transaction.rollback();
            }
            e.printStackTrace();
        }
    }

    @Test // 属性查询
    public void queryByTest() {
        SessionFactory sessionFactory = null;
        Session session = null;
        Transaction transaction = null;

        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
            CriteriaQuery<User> criteria = criteriaBuilder.createQuery(User.class);
            Root<User> from = criteria.from(User.class);
            //user表中age为26的记录
            criteria.select(from).where(from.get("age").in("26"));
            List<User> list = session.createQuery(criteria).getResultList();
            for (User user : list) {
                System.out.println(user);
            }
            transaction.commit();
        } catch (HibernateException e) {
            if (transaction != null) {
                transaction.rollback();
            }
            e.printStackTrace();
        }
    }

    @Test // orderBy查询
    public void orderByTest() {
        SessionFactory sessionFactory = null;
        Session session = null;
        Transaction transaction = null;

        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
            CriteriaQuery<User> criteria = criteriaBuilder.createQuery(User.class);
            Root<User> from = criteria.from(User.class);
            //查询结果按id降序排列
            Order order = criteriaBuilder.desc(from.get("id"));
            criteria.orderBy(order);
            List<User> list = session.createQuery(criteria).getResultList();
            for (User user : list) {
                System.out.println(user);
            }
            transaction.commit();
        } catch (HibernateException e) {
            if (transaction != null) {
                transaction.rollback();
            }
            e.printStackTrace();
        }
    }

    @Test // 统计查询
    public void countTest() {
        SessionFactory sessionFactory = null;
        Session session = null;
        Transaction transaction = null;

        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
            CriteriaQuery<Long> criteria = criteriaBuilder.createQuery(Long.class);
            Root<User> from = criteria.from(User.class);
            //user表中记录总数
            criteria.select(criteriaBuilder.count(from));
            Long count = session.createQuery(criteria).uniqueResult();
            System.out.println("总数为:"+count);
            transaction.commit();
        } catch (HibernateException e) {
            if (transaction != null) {
                transaction.rollback();
            }
            e.printStackTrace();
        }
    }

    @Test // 子查询
    public void subQueryTest() {
        SessionFactory sessionFactory = null;
        Session session = null;
        Transaction transaction = null;

        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
            // 创建criteria
            CriteriaQuery<User> criteria = criteriaBuilder.createQuery(User.class);
            Root<User> from = criteria.from(User.class);
            // 创建子查询
            Subquery<Double> subQuery = criteria.subquery(Double.class);
            Root<User> subFrom = subQuery.from(User.class);
            // user表中age的平均值
            subQuery.select(criteriaBuilder.avg(subFrom.get("age")));
            // user表中age大于平均值的记录
            criteria.select(from).where(criteriaBuilder.gt(from.get("age"), subQuery));
            List<User> list = session.createQuery(criteria).getResultList();
            for (User user : list) {
                System.out.println(user);
            }
            transaction.commit();
        } catch (HibernateException e) {
            if (transaction != null) {
                transaction.rollback();
            }
            e.printStackTrace();
        }
    }
}
展开阅读全文

没有更多推荐了,返回首页