zoukankan      html  css  js  c++  java
  • 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();
    	}
    }
    
  • 相关阅读:
    Cookie练习
    JS写九九乘法表
    对GridView实现分页
    对GridView的行加颜色并弹出Kindeditor
    对Dictionary的理解
    一、android 开发环境大搭建
    main方法的测试
    main 方法的书写(1)
    由InvocationTargetException引发的思考
    汇编学习笔记之处理器体系结构
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/7421378.html
Copyright © 2011-2022 走看看