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;
    
    /**
     * 分页查询
     * @author Administrator
     */
    public class TestDemo3 {
    	/**
    	 * 分页查查询
    	 * 查询结果:
    		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_ 
    		    order by
    		        book0_.price desc limit ?
    		云计算技术及性能优化
    		架构探险:轻量级微服务架构(下册)
    		中国冰雪梦
    	 */
    	@Test
    	public void run1() {
    		Session session = HibernateUtil.getCurrentSession();
    		Transaction transaction = session.beginTransaction();
    		
    		String hql = "from Book b order by b.price desc";
    		Query query = session.createQuery(hql);
    		
    		query.setFirstResult(0);// 从哪一个对象开始查询,起始位置为0
    		query.setMaxResults(3);// 查询出几个对象
    		
    		List<Book> list = query.list();
    		for (Book book : list) {
    			System.err.println(book.getName());
    		}
    		
    		transaction.commit();
    	}
    }
    
  • 相关阅读:
    zabbix实现mysql数据库的监控(四)
    Redis高级进阶(一)
    Redis高级进阶(二)
    Redis的管理
    9.动态SQL
    8.Mapper动态代理
    7.属性名与查询字段名不相同
    6.单表的CRUD操作
    5.API详解
    4.主配置文件详解
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/7421343.html
Copyright © 2011-2022 走看看