Hibernate分页查询小结.docx

上传人:b****6 文档编号:6303894 上传时间:2023-01-05 格式:DOCX 页数:14 大小:23.68KB
下载 相关 举报
Hibernate分页查询小结.docx_第1页
第1页 / 共14页
Hibernate分页查询小结.docx_第2页
第2页 / 共14页
Hibernate分页查询小结.docx_第3页
第3页 / 共14页
Hibernate分页查询小结.docx_第4页
第4页 / 共14页
Hibernate分页查询小结.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

Hibernate分页查询小结.docx

《Hibernate分页查询小结.docx》由会员分享,可在线阅读,更多相关《Hibernate分页查询小结.docx(14页珍藏版)》请在冰豆网上搜索。

Hibernate分页查询小结.docx

Hibernate分页查询小结

通常使用的Hibernate通常是三种:

hql查询,QBC查询和QBE查询:

 

1、QBE(QureyByExample)检索方式 

QBE是最简单的,但是功能也是最弱的,QBE的功能不是特别强大,仅在某些场合下有用。

一个典型的使用场合就是在查询窗口中让用户输入一系列的查询条件,然后返回匹配的对象。

QBE只支持=和like比较运算符,无法不大区间值,及其或的匹配。

在这种情况下,还是采用HQL检索方式或QBC检索方式。

 

Java代码  

1./** 

2. * @function 根据传递过来的Object,分页显示在数据库中与其匹配的记录 

3. * @param pageNo 

4. *            当前页数 

5. * @param pageSize 

6. *            每页显示的记录数 

7. * @param object 

8. *            将查询条件封装为Object 

9. * @return 将查询结果封装为Pager返回 

10. */  

11.public Pager findPageByExample(int pageNo, int pageSize, Object object)  

12.{  

13.    Pager pager = null;  

14.    try  

15.    {  

16.        Criteria criteria = this.getSession().createCriteria(  

17.                Class.forName(this.getEntity()));  

18.  

19.        if (object !

= null)  

20.        {  

21.            criteria.add(Example.create(object).enableLike());  

22.        }  

23.  

24.        // 获取根据条件分页查询的总行数  

25.        int rowCount = (Integer) criteria.setProjection(  

26.                Projections.rowCount()).uniqueResult();  

27.        criteria.setProjection(null);  

28.  

29.        criteria.setFirstResult((pageNo - 1) * pageSize);  

30.        criteria.setMaxResults(pageSize);  

31.  

32.        List result = criteria.list();  

33.  

34.        pager = new Pager(pageSize, pageNo, rowCount, result);  

35.  

36.    } catch (RuntimeException re)  

37.    {  

38.        throw re;  

39.    } finally  

40.    {  

41.        return pager;  

42.    }  

43.  

44.}  

注意代码的第20行,即criteria.add(Example.create(object).enableLike());这一行,需将Example.create(object)调用.enableLike()方法,不然不能模糊查询。

 

在BO层将需要模糊查询的列用"%%"串起来,不然仍然和"="一样。

 

BO层代码:

 

Java代码  

1./** 

2. * @function 将传递过来的参数封装成抢修人员Bean,分页查询符合条件的记录 

3. * @param pageNo 

4. *            当前的页码 

5. * @param pageSize 

6. *            每页显示的记录数 

7. * @param mendName 

8. *            抢修人员的名称 

9. * @param specialty 

10. *            抢修人员的工种 

11. * @param post 

12. *            抢修人员的职称 

13. * @return 将符合条件的记录数以及页码信息封装成PagerBean返回 

14. */  

15.public Pager getInfoByQuery(int pageNo, int pageSize, String mendName,  

16.        String specialty, String post)  

17.{  

18.  

19.    EicMend eicMend = new EicMend();  

20.    if (mendName !

= null && mendName.length() > 0)  

21.    {  

22.        eicMend.setMendname("%" + mendName + "%");  

23.    }  

24.    if (specialty !

= null && specialty.length() > 0)  

25.    {  

26.        eicMend.setSpecialty(specialty);  

27.    }  

28.    if (post !

= null && post.length() > 0)  

29.    {  

30.        eicMend.setPost(post);  

31.    }  

32.  

33.    Pager pager = erpManagerDao  

34.            .findPageByExample(pageNo, pageSize, eicMend);  

35.    return pager;  

36.}  

执行SQL语句如下:

 

Sql代码  

1.Hibernate:

 select count(*) as y0_ from YJZX.EIC_MEND this_ where   

2.(this_.MENDNAME like ?

 and this_.POST like ?

)  

3.  

4.Hibernate:

 select * from ( select this_.MENDID as MENDID23_0_, ……  

5.this_.EXPERTREMARK as EXPERTR28_23_0_ from YJZX.EIC_MEND this_ where   

6.(this_.MENDNAME like ?

 and this_.POST like ?

) ) where rownum <= ?

  

所以只需将需模糊查询的列用“%%”链接即可。

 

2、QBC(QureyByCriteria)检索方式 

      采用HQL检索方式时,在应用程序中需要定义基于字符串形式的HQL查询语句。

QBCAPI提供了检索对象的另一种方式,它主要由Criteria接口、Criterion接口和Restrictions接口组成,它支持在运行时动态生成查询语句。

比较常见的是两种传参方式:

一种是用map传参,另一种是用Criterion…不定参数传参。

 

Map传参方式范例如下:

 

DAO层:

 

Java代码  

1./** 

2. * @function 分页显示符合所有的记录数,将查询结果封装为Pager 

3. * @param pageNo 

4. *            当前页数 

5. * @param pageSize 

6. *            每页显示的条数 

7. * @param map 

8. *            将查询条件封装为map 

9. * @return 查询结果Pager 

10. */  

11.public Pager findPageByCriteria(int pageNo, int pageSize, Map map)  

12.{  

13.    Pager pager = null;  

14.    try  

15.    {  

16.        Criteria criteria = this.getSession().createCriteria(  

17.                Class.forName(this.getEntity()));  

18.  

19.        if (map !

= null)  

20.        {  

21.            Set keys = map.keySet();  

22.            for (String key :

 keys)  

23.            {  

24.                criteria.add(Restrictions.like(key, map.get(key)));  

25.            }  

26.        }  

27.  

28.        // 获取根据条件分页查询的总行数  

29.        int rowCount = (Integer) criteria.setProjection(  

30.                Projections.rowCount()).uniqueResult();  

31.        criteria.setProjection(null);  

32.  

33.        criteria.setFirstResult((pageNo - 1) * pageSize);  

34.        criteria.setMaxResults(pageSize);  

35.  

36.        List result = criteria.list();  

37.  

38.        pager = new Pager(pageSize, pageNo, rowCount, result);  

39.  

40.    } catch (RuntimeException re)  

41.    {  

42.        throw re;  

43.    } finally  

44.    {  

45.        return pager;  

46.    }  

47.  

48.}  

Map传参方式对应BO层代码:

 

Java代码  

1./** 

2. * @function 将传递过来的参数封装成抢修人员Bean,分页查询符合条件的记录 

3. * @param pageNo 

4. *            当前的页码 

5. * @param pageSize 

6. *            每页显示的记录数 

7. * @param mendName 

8. *            抢修人员的名称 

9. * @param specialty 

10. *            抢修人员的工种 

11. * @param post 

12. *            抢修人员的职称 

13. * @return 将符合条件的记录数以及页码信息封装成PagerBean返回 

14. */  

15.public Pager getInfoByQuery2(int pageNo, int pageSize, String mendName,  

16.        String specialty, String post)  

17.{  

18.  

19.    Map map = new HashMap();  

20.  

21.    if (mendName !

= null && mendName.length() > 0)  

22.    {  

23.        map.put("mendname", "%" + mendName + "%");  

24.    }  

25.    if (specialty !

= null && specialty.length() > 0)  

26.    {  

27.        map.put("specialty", specialty);  

28.    }  

29.    if (post !

= null && post.length() > 0)  

30.    {  

31.        map.put("post", post);  

32.    }  

33.  

34.    Pager pager = erpManagerDao.findPageByCriteria(pageNo, pageSize, map);  

35.    return pager;  

36.}  

第二种方式:

Criterion…不定参数传参方式。

其代码如下所示:

 

DAO层代码:

 

Java代码  

1./** 

2. * @function 分页显示符合所有的记录数,将查询结果封装为Pager 

3. * @param pageNo 

4. *            当前页数 

5. * @param pageSize 

6. *            每页显示的条数 

7. * @param criterions 

8. *            不定参数Criterion 

9. * @return 查询结果Pager 

10. */  

11.public Pager findPageByCriteria(int pageNo, int pageSize,  

12.        Criterion... criterions)  

13.{  

14.    Pager pager = null;  

15.    try  

16.    {  

17.        Criteria criteria = this.getSession().createCriteria(  

18.                Class.forName(this.getEntity()));  

19.        if (criterions !

= null)  

20.        {  

21.            for (Criterion criterion :

 criterions)  

22.            {  

23.                if (criterion !

= null)  

24.                {  

25.                    criteria.add(criterion);  

26.                }  

27.  

28.            }  

29.        }  

30.  

31.        // 获取根据条件分页查询的总行数  

32.        int rowCount = (Integer) criteria.setProjection(  

33.                Projections.rowCount()).uniqueResult();  

34.        criteria.setProjection(null);  

35.  

36.        criteria.setFirstResult((pageNo - 1) * pageSize);  

37.        criteria.setMaxResults(pageSize);  

38.  

39.        List result = criteria.list();  

40.  

41.        pager = new Pager(pageSize, pageNo, rowCount, result);  

42.  

43.    } catch (RuntimeException re)  

44.    {  

45.        throw re;  

46.    } finally  

47.    {  

48.        return pager;  

49.    }  

50.  

51.}  

Criterion…不定参数传参方式对应BO层代码:

 

Java代码  

1./** 

2. * @function 将传递过来的参数封装成抢修人员Bean,分页查询符合条件的记录 

3. * @param pageNo 

4. *            当前的页码 

5. * @param pageSize 

6. *            每页显示的记录数 

7. * @param mendName 

8. *            抢修人员的名称 

9. * @param specialty 

10. *            抢修人员的工种 

11. * @param post 

12. *            抢修人员的职称 

13. * @return 将符合条件的记录数以及页码信息封装成PagerBean返回 

14. */  

15.public Pager getInfoByQuery3(int pageNo, int pageSize, String mendName,  

16.        String specialty, String post)  

17.{  

18.    Criterion criterion1 = null, criterion2 = null, criterion3 = null;  

19.    if (mendName !

= null && mendName.length() > 0)  

20.    {  

21.        criterion1 = Restrictions.ilike("mendname", mendName,  

22.                MatchMode.ANYWHERE);  

23.    }  

24.  

25.    if (specialty !

= null && specialty.length() > 0)  

26.    {  

27.        criterion2 = Restrictions.ilike("specialty", specialty,  

28.                MatchMode.EXACT);  

29.    }  

30.  

31.    if (post !

= null && post.length() > 0)  

32.    {  

33.        criterion3 = Restrictions.ilike("post", post, MatchMode.EXACT);  

34.    }  

35.  

36.    Pager pager = erpManagerDao.findPageByCriteria(pageNo, pageSize,  

37.            criterion1, criterion2, criterion3);  

38.  

39.    return pager;  

40.}  

3、HQL检索方式 

HQL(HibernateQueryLanguage)是面向对象的查询语言,它和SQL查询语言有些相识。

在Hibernate提供的各种检索方式中,HQL是使用最广的一种检索方式。

 

使用Query接口分页查询DAO代码:

 

Java代码  

1./** 

2. * @function 分页显示符合所有的记录数,将查询结果封装为Pager 

3. * @param pageNo 

4. *            当前页数 

5. * @param pageSize 

6. *            每页显示的条数 

7. * @param instance 

8. *            将查询条件封装为专家Bean 

9. * @return 查询结果Pager 

10. */  

11.public List findPageByQuery(int pageNo, int pageSize, String hql,  

12.        Map map)  

13.{  

14.    List result = null;  

15.    try  

16.    {  

17.        Query query = this.getSession().createQuery(hql);  

18.  

19.        Iterator it = map.keySet().iterator();  

20.        while (it.hasNext())  

21.        {  

22.            Object key = it.next();  

23.            query.setParameter(key.toString(), map.get(key));  

24.        }  

25.  

26.        query.setFirstResult((pageNo - 1) * pageSize);  

27.        query.setMaxResults(pageSize);  

28.  

29.        result = query.list();  

展开阅读全文
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1