• 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();
    	}
    }
    
  • 相关阅读:
    【安徽集训】fiend
    【安徽集训】Emerald
    【安徽集训】Entropy
    【安徽集训】字符串
    【福建集训】果树
    【CF335 E】Counting Skyscrapers
    【BZOJ 3514】Codechef MARCH14 GERALD07 加强版
    【未知来源】Randomized Binary Search Tree
    【ZJOI 2016】旅行者
    【AGC002 E】Candy Piles
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/7421378.html
Copyright © 2020-2023  润新知