Hibernate之QBC检索(查询)方式

QBC(Query By Criteria)由Criteria接口、Criterion接口和Expresson类组成,它支持在运行时动态生成查询语句。

实例演示:

package com.lanhuigu.hibernate.test;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Expression;

import com.lanhuigu.hibernate.entity.Customer;

public class TestHibernateJianSuo {
	public static void main(String[] args){
		Configuration cfg = new Configuration().configure();
		SessionFactory sessionFactory = cfg.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		//1.创建Criteria
		Criteria criteria = session.createCriteria(Customer.class);
		//2.设定查询条件,把条件放入Criteria中
		Criterion criterion = Expression.eq("name", "test");
		criteria.add(criterion);
		//3.执行SQL返回查询结果
		List list = criteria.list();
		//4.输出结果
		for (int i=0;i<list.size();i++) {
			Customer customer = (Customer) list.get(i);
			System.out.println(customer.getName());
		}
		//5.事务提交
		tr.commit();
		//6.关闭session
		session.close();
	}
}
执行结果,控制台输出:

Hibernate: select this_.ID as ID1_0_0_, this_.NAME as NAME2_0_0_, this_.EMAIL as EMAIL3_0_0_, this_.PASSWORD as PASSWORD4_0_0_, this_.PHONE as PHONE5_0_0_, this_.ADDRESS as ADDRESS6_0_0_, this_.SEX as SEX7_0_0_, this_.IS_MARRIED as IS8_0_0_, this_.DESCRIPTION as DESCRIPT9_0_0_, this_.IMAGE as IMAGE10_0_0_, this_.BIRTHDAY as BIRTHDA11_0_0_, this_.REGISTERED_TIME as REGISTE12_0_0_, this_.HOME_PROVINCE as HOME13_0_0_, this_.HOME_CITY as HOME14_0_0_, this_.HOME_STREET as HOME15_0_0_, this_.HOME_ZIPCODE as HOME16_0_0_, this_.COMP_PROVINCE as COMP17_0_0_, this_.COMP_CITY as COMP18_0_0_, this_.COMP_STREET as COMP19_0_0_, this_.COMP_ZIPCODE as COMP20_0_0_ from CUSTOMERS this_ where this_.NAME=?
test
实例分析,QBC检索步骤:

(1)调用Session的createCriteria()方法,创建Criteria对象

(2)设定查询条件。调用Expression的静态方法设置查询条件,每一个Criterion实例代表一个条件,通过Criteria的add()方法加入查询条件。

(3)执行SQL。在list中放入符合查询条件的持久化对象。

(4)支持链式风格编程。如,

List list = session.createCriteria(Customer.class).add(Expression.eq("name", "test")).list();
-------------------------------------------------

在QBC中有个子功能叫做QBE(Query By Example)检索方式,定义查询条件模板,根据模板检索数据。

实例:

package com.lanhuigu.hibernate.test;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;

import com.lanhuigu.hibernate.entity.Customer;

public class TestHibernateJianSuo {
	public static void main(String[] args){
		Configuration cfg = new Configuration().configure();
		SessionFactory sessionFactory = cfg.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		//1.创建样板对象
		Customer exampleCustomer = new Customer();
		exampleCustomer.setName("test");
		//2.执行SQL
		List list = session.createCriteria(Customer.class)
				.add(Example.create(exampleCustomer))
				.list();
		//3.输出结果
		for (int i=0;i<list.size();i++) {
			Customer customer = (Customer) list.get(i);
			System.out.println(customer.getName());
		}
		//4.事务提交
		tr.commit();
		//5.关闭session
		session.close();
	}
}
对比QBC,QBE通过create()方法,创建Criterion实例对象,在通过Criteria的add方法加上条件,最后在执行sql.

执行结果,控制台输出:

Hibernate: select this_.ID as ID1_0_0_, this_.NAME as NAME2_0_0_, this_.EMAIL as EMAIL3_0_0_, this_.PASSWORD as PASSWORD4_0_0_, this_.PHONE as PHONE5_0_0_, this_.ADDRESS as ADDRESS6_0_0_, this_.SEX as SEX7_0_0_, this_.IS_MARRIED as IS8_0_0_, this_.DESCRIPTION as DESCRIPT9_0_0_, this_.IMAGE as IMAGE10_0_0_, this_.BIRTHDAY as BIRTHDA11_0_0_, this_.REGISTERED_TIME as REGISTE12_0_0_, this_.HOME_PROVINCE as HOME13_0_0_, this_.HOME_CITY as HOME14_0_0_, this_.HOME_STREET as HOME15_0_0_, this_.HOME_ZIPCODE as HOME16_0_0_, this_.COMP_PROVINCE as COMP17_0_0_, this_.COMP_CITY as COMP18_0_0_, this_.COMP_STREET as COMP19_0_0_, this_.COMP_ZIPCODE as COMP20_0_0_ from CUSTOMERS this_ where (this_.NAME=? and this_.PHONE=? and this_.SEX=? and this_.IS_MARRIED=?)
test
QBE能包含对象中不为NULL的条件,只支持'=','like'运算符。

你可能感兴趣的:(criterion,example,qbc,Criteria,QBE)