Hibernate 5.2版本以前的Criteria查询

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Create_Myths/article/details/78692919

Hibernate 5.2版本以前的Criteria查询


Query By Criteria
特点:使用面向对象的方式查询

package com.qfedu.hibernate.test;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;
import com.qfedu.hibernate.pojo.Customer;
import com.qfedu.hibernate.pojo.Order;
import com.qfedu.hibernate.utils.HibernateUtil;
/**
 * 
 * @author helen
 * 1、全表查询
 * 2、条件查询
 * 3、分页查询
 * 4、查询排序
 * 5、聚合查询
 * 6、投影查询
 */
public class OBCTest {
    /**
     * 全表查询
     */
    @Test
    public void test1() {
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();

        Criteria ce = session.createCriteria(Customer.class);
        List<Customer> list = ce.list();
        for(Customer customer : list) {
            System.out.println(customer);
        }

        tx.commit();
        session.close();
    }

    /**
     * 条件查询
     */
    @Test
    public void test21() {
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();

        Criteria ce = session.createCriteria(Order.class);
        ce.add(Restrictions.eq("orderno", "20170928001"));
        List<Order> list = ce.list();
        for(Order order : list) {
            System.out.println(order);
        }

        tx.commit();
        session.close();
    }

    /**
     * 多条件查询
     */
    @Test
    public void test22() {
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();

        Criteria ce = session.createCriteria(Order.class);
        //ce.add(Restrictions.eq("orderno", "20170928001"));
        ce.add(Restrictions.and(
                  Restrictions.like("orderno", "%2017%"), Restrictions.like("productName", "%手机%")
              ));
        List<Order> list = ce.list();
        for(Order order : list) {
            System.out.println(order);
        }

        tx.commit();
        session.close();
    }

    /**
     * 分页查询
     */
    @Test
    public void test3() {
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();

        Criteria ce = session.createCriteria(Order.class);

        //分页
        ce.setFirstResult(0);
        ce.setMaxResults(5);

        List<Order> list = ce.list();
        for(Order order : list) {
            System.out.println(order);
        }

        tx.commit();
        session.close();
    }

    /**
     * 查询排序
     */
    @Test
    public void test4() {
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();

        Criteria ce = session.createCriteria(Order.class);

        ce.addOrder(org.hibernate.criterion.Order.desc("id"));

        List<Order> list = ce.list();
        for(Order order : list) {
            System.out.println(order);
        }

        tx.commit();
        session.close();
    }

    /**
     * 聚合排序
     */
    @Test
    public void test5() {
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();

        Criteria ce = session.createCriteria(Order.class);

        /*ce.setProjection(Projections.rowCount());
        Long count = (Long)ce.uniqueResult();*/

        ce.setProjection(Projections.max("id"));
        Integer count = (Integer)ce.uniqueResult();

        System.out.println(count);

        tx.commit();
        session.close();
    }


    /**
     * 投影查询
     */
    @Test
    public void test6() {
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();

        Criteria ce = session.createCriteria(Order.class);

        ProjectionList pList = Projections.projectionList();
        pList.add(Property.forName("orderno"));
        pList.add(Property.forName("productName"));
        ce.setProjection(pList);
        List<Object[]> list = ce.list();

        for(Object[] order : list) {
            for(Object column : order) {
                System.out.print(column);
                System.out.print(" ");
            }
            System.out.println();
        }

        tx.commit();
        session.close();
    }
}
展开阅读全文

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