Hibernate- 条件查询

01.搭建开发环境

02.条件查询

package com.gordon.test;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import com.gordon.domain.Book;
import com.gordon.utils.HibernateUtil;
import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 * 条件查询
 * @author Administrator
 */
public class TestDemo4 {
	/**
	 * 条件查询
	 * 查询结果1:
		Hibernate: 
		    select
		        book0_.id as id1_0_,
		        book0_.name as name2_0_,
		        book0_.price as price3_0_,
		        book0_.publisher_id as publishe4_0_ 
		    from
		        t_book book0_ 
		    where
		        book0_.price>?
		云计算技术及性能优化
		中国冰雪梦
		架构探险:轻量级微服务架构(下册)
	 */
	@Test
	public void run1() {
		Session session = HibernateUtil.getCurrentSession();
		Transaction transaction = session.beginTransaction();
	
		// 两种方式查询结果相同
		
		//1
//		String hql = "from Book b where b.price > ?";
//		Query query = session.createQuery(hql);
//		query.setParameter(0, new Double(44.00));
		
		//2
		String hql = "from Book b where b.price > :price";
		Query query = session.createQuery(hql);
		query.setParameter("price", new Double(44.00));

		
		
		List<Book> list = query.list();
		for (Book book : list) {
			System.err.println(book.getName());
		}
		
		transaction.commit();
	}
}
原文地址:https://www.cnblogs.com/hfultrastrong/p/7421378.html