`
hz_chenwenbiao
  • 浏览: 1007997 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

hibernate分页转换为sql命令

阅读更多

使用hibernate进行分页,设置起始记录数和记录条数:

 

if (limit > 0) {
				c.setFirstResult(start);
				c.setMaxResults(limit);
			}

执行查询后转换为sql语句分为两种情况,第一种是当起始位置为0时,那么会使用top方式:

 

select top ? this_.POSTID as POSTID23_3_, this_.cityPK as cityPK23_3_, this_.content as content23_3_, this_.countryPK as countryPK23_3_, this_.createtime as createtime23_3_, this_.ip as ip23_3_, this_.lastmodtime as lastmodt7_23_3_, this_.OBJECTPK as OBJECTPK23_3_, this_.parentid as parentid23_3_, this_.provincePK as provincePK23_3_, this_.recommended as recomme10_23_3_, this_.replyCount as replyCount23_3_, this_.SERVERNODEID as SERVERN12_23_3_, this_.title as title23_3_, this_.townPK as townPK23_3_, this_.USERPK as USERPK23_3_, this_.viliagePK as viliagePK23_3_, obj2_.OBJECTPK as OBJECTPK0_0_, obj2_.addDate as addDate0_0_, obj2_.currVisitCount as currVisi3_0_0_, obj2_.depth as depth0_0_, obj2_.description as descript5_0_0_, obj2_.historyVisitCount as historyV6_0_0_, obj2_.objectID as objectID0_0_, obj2_.objectType as objectType0_0_, obj2_.PARENTPK as PARENTPK0_0_, obj2_.password as password0_0_, obj2_.popular as popular0_0_, obj2_.postID as postID0_0_, obj2_.recommended as recomme12_0_0_, obj2_.ROOTPK as ROOTPK0_0_, obj2_.serverNodeID as serverN13_0_0_, obj2_.status as status0_0_, obj2_.updateDate as updateDate0_0_, obj2_.USERPK as USERPK0_0_, user3_.USERPK as USERPK6_1_, user3_.addDate as addDate6_1_, user3_.address as address6_1_, user3_.OBJECTPK as OBJECTPK6_1_, user3_.answer as answer6_1_, user3_.CONFIG as CONFIG6_1_, user3_.description as descript6_6_1_, user3_.EMAIL as EMAIL6_1_, user3_.OCCUPATION as OCCUPATION6_1_, user3_.PASSWORD as PASSWORD6_1_, user3_.PHOTO as PHOTO6_1_, user3_.plainpasswd as plainpa11_6_1_, user3_.question as question6_1_, user3_.SERVERNODEID as SERVERN13_6_1_, user3_.SEX as SEX6_1_, user3_.STATUS as STATUS6_1_, user3_.tel as tel6_1_, user3_.USERID as USERID6_1_, user3_.USERNAME as USERNAME6_1_, user3_.userType as userType6_1_, user4_.USERPK as USERPK6_2_, user4_.addDate as addDate6_2_, user4_.address as address6_2_, user4_.OBJECTPK as OBJECTPK6_2_, user4_.answer as answer6_2_, user4_.CONFIG as CONFIG6_2_, user4_.description as descript6_6_2_, user4_.EMAIL as EMAIL6_2_, user4_.OCCUPATION as OCCUPATION6_2_, user4_.PASSWORD as PASSWORD6_2_, user4_.PHOTO as PHOTO6_2_, user4_.plainpasswd as plainpa11_6_2_, user4_.question as question6_2_, user4_.SERVERNODEID as SERVERN13_6_2_, user4_.SEX as SEX6_2_, user4_.STATUS as STATUS6_2_, user4_.tel as tel6_2_, user4_.USERID as USERID6_2_, user4_.USERNAME as USERNAME6_2_, user4_.userType as userType6_2_ 
from POSTS this_ 
inner join OBJECTS obj2_ on this_.OBJECTPK=obj2_.OBJECTPK 
left outer join USERS user3_ on obj2_.USERPK=user3_.USERPK 
inner join USERS user4_ on this_.USERPK=user4_.USERPK 
where this_.parentid=? and this_.recommended<>? and (this_.OBJECTPK=? or this_.OBJECTPK=?) 
order by this_.POSTID desc

上面显示一堆信息,其它它们的关系,我们可以不看,就看它实现的方式,这里是使用了top方式来进行查询的。

 

第二种情况是起始位置不为0的时候,那么会使用limit方式进行查询:

select limit ? ? this_.POSTID as POSTID23_3_, this_.cityPK as cityPK23_3_, this_.content as content23_3_, this_.countryPK as countryPK23_3_, this_.createtime as createtime23_3_, this_.ip as ip23_3_, this_.lastmodtime as lastmodt7_23_3_, this_.OBJECTPK as OBJECTPK23_3_, this_.parentid as parentid23_3_, this_.provincePK as provincePK23_3_, this_.recommended as recomme10_23_3_, this_.replyCount as replyCount23_3_, this_.SERVERNODEID as SERVERN12_23_3_, this_.title as title23_3_, this_.townPK as townPK23_3_, this_.USERPK as USERPK23_3_, this_.viliagePK as viliagePK23_3_, obj2_.OBJECTPK as OBJECTPK0_0_, obj2_.addDate as addDate0_0_, obj2_.currVisitCount as currVisi3_0_0_, obj2_.depth as depth0_0_, obj2_.description as descript5_0_0_, obj2_.historyVisitCount as historyV6_0_0_, obj2_.objectID as objectID0_0_, obj2_.objectType as objectType0_0_, obj2_.PARENTPK as PARENTPK0_0_, obj2_.password as password0_0_, obj2_.popular as popular0_0_, obj2_.postID as postID0_0_, obj2_.recommended as recomme12_0_0_, obj2_.ROOTPK as ROOTPK0_0_, obj2_.serverNodeID as serverN13_0_0_, obj2_.status as status0_0_, obj2_.updateDate as updateDate0_0_, obj2_.USERPK as USERPK0_0_, user3_.USERPK as USERPK6_1_, user3_.addDate as addDate6_1_, user3_.address as address6_1_, user3_.OBJECTPK as OBJECTPK6_1_, user3_.answer as answer6_1_, user3_.CONFIG as CONFIG6_1_, user3_.description as descript6_6_1_, user3_.EMAIL as EMAIL6_1_, user3_.OCCUPATION as OCCUPATION6_1_, user3_.PASSWORD as PASSWORD6_1_, user3_.PHOTO as PHOTO6_1_, user3_.plainpasswd as plainpa11_6_1_, user3_.question as question6_1_, user3_.SERVERNODEID as SERVERN13_6_1_, user3_.SEX as SEX6_1_, user3_.STATUS as STATUS6_1_, user3_.tel as tel6_1_, user3_.USERID as USERID6_1_, user3_.USERNAME as USERNAME6_1_, user3_.userType as userType6_1_, user4_.USERPK as USERPK6_2_, user4_.addDate as addDate6_2_, user4_.address as address6_2_, user4_.OBJECTPK as OBJECTPK6_2_, user4_.answer as answer6_2_, user4_.CONFIG as CONFIG6_2_, user4_.description as descript6_6_2_, user4_.EMAIL as EMAIL6_2_, user4_.OCCUPATION as OCCUPATION6_2_, user4_.PASSWORD as PASSWORD6_2_, user4_.PHOTO as PHOTO6_2_, user4_.plainpasswd as plainpa11_6_2_, user4_.question as question6_2_, user4_.SERVERNODEID as SERVERN13_6_2_, user4_.SEX as SEX6_2_, user4_.STATUS as STATUS6_2_, user4_.tel as tel6_2_, user4_.USERID as USERID6_2_, user4_.USERNAME as USERNAME6_2_, user4_.userType as userType6_2_ 
from POSTS this_ 
inner join OBJECTS obj2_ on this_.OBJECTPK=obj2_.OBJECTPK 
left outer join USERS user3_ on obj2_.USERPK=user3_.USERPK 
inner join USERS user4_ on this_.USERPK=user4_.USERPK 
where this_.parentid=? and this_.recommended<>? and (this_.OBJECTPK=? or this_.OBJECTPK=?) 
order by this_.POSTID desc
 

 

limit和top应该不是标准的sql命令,再转:

 

 

做分页效果时发现一直不能成功,最后发现问题在top * N from tablename这条语句上。在mysql中select top用法和ms sqlserver有所区别。

若在mysql中实现select top功能得用:select * from tablename limit M,N

这里M表示从(M+1)条记录开始,N表示返回的记录条数。

 

分享到:
评论

相关推荐

    hibernate分页Hibernate 分页的设计和编码

    标题与描述均提到了“Hibernate分页的设计和编码”,这表明文章主要聚焦于如何在Hibernate框架中实现数据分页功能。下面将详细解析这一主题的关键知识点。 ### Hibernate分页概念 Hibernate是Java环境下一个开放源...

    spring+hibernate 分页 +mysql

    Hibernate是一个强大的ORM(Object-Relational Mapping)框架,它允许开发者将Java对象映射到关系数据库表,消除了Java与SQL之间的直接交互,降低了数据访问层的复杂性。在Spring框架中,我们可以利用Spring Data ...

    Struts和Hibernate分页及查询

    **Hibernate框架** 是一个流行的ORM框架,它可以将数据库操作转换为面向对象的Java代码,简化了数据持久化的过程。在分页查询中,Hibernate提供了一种称为HQL(Hibernate Query Language)的SQL方言,可以方便地实现...

    Hibernate分页

    `Dialect` 类负责将 Hibernate 的标准分页逻辑转换为特定数据库的 SQL 语法。这意味着,无论是在 MySQL、Oracle 还是其他任何数据库上,开发者都可以使用相同的 Hibernate 分页 API,而具体的 SQL 语法转换则由 ...

    struts+hibernate分页

    Hibernate在执行Criteria查询时,会自动转化为包含这些关键字的SQL语句。 5. **DAO(Data Access Object)模式**:DAO模式是一种设计模式,用于封装数据库访问逻辑。在分页场景下,DAO类将处理所有与数据库相关的...

    hibernate实现分页查询

    执行查询并将结果转换为List类型: ```java List result = query.list(); ``` 完整的分页查询方法实现如下: ```java public List getPageList(int thisNumber, int sumCount, String sql) { Session session = ...

    struts2+spring+hibernate分页显示

    通过配置文件或注解,Hibernate可以自动将Java对象转换为SQL语句进行数据库操作。在分页查询时,Hibernate提供Criteria、HQL(Hibernate Query Language)等方式来实现,可以根据需求灵活选择。 实现分页显示的步骤...

    struts+hibernate分页源码

    3. **结果集转换**: 分页查询得到的结果集需要转化为前端可以理解的格式。这通常涉及到创建一个Page对象,包含当前页的数据列表以及总页数、总记录数等信息。 4. **视图层处理**: Struts的JSP视图层可以通过标签库...

    Struts+Hibernate分页

    通过以上步骤,我们可以构建一个功能完备且易于维护的Struts+Hibernate分页系统。在实际开发中,还可以结合Spring框架进行整合,提高代码的可测试性和解耦性。同时,随着技术的发展,现在的Web应用更多地采用Spring ...

    Hibernate中的query 分页.doc

    Hibernate会根据设定的数据库方言(dialect)自动将这些调用转换为对应的SQL语句。例如,对于MySQL,它会生成`LIMIT`子句;而对于Oracle和SQL Server,可能会生成`TOP`子句。数据库层面的分页通常更快且更节省内存,...

    hibernate商品分页展示

    **hibernate商品分页展示**是Web应用中常见的需求,尤其是在电商系统中,用户通常需要浏览大量的商品信息,分页能有效地管理这些数据并提高用户体验。本篇将深入探讨如何利用Hibernate框架来实现商品的分页显示。 ...

    hibernate jsp 分页Demo

    在本项目中,Hibernate被用来执行数据库查询,并将结果集转换为Java对象。 2. **JSP(JavaServer Pages)**: JSP是Java EE平台的一部分,用于创建动态网页。在分页Demo中,JSP页面负责接收用户请求,展示分页结果,...

    spring+struts2+hibernate整合实现分页

    在分页场景下,Hibernate可以方便地执行SQL查询,获取指定范围的数据,并进行转换以供显示。 4. **分页原理**:分页是为了解决大量数据一次性加载导致性能下降和用户体验不佳的问题。通常包括两个参数:当前页码和...

    hibernate分页

    本示例是通过SQL、Hibernate和Struts2.1这三个技术实现的分页功能。下面将详细介绍这三个组件以及它们在分页中的作用。 首先,我们来了解一下**Hibernate**,它是一个强大的Java持久化框架,能够将Java对象与关系...

    Hibernate分页总结

    一、Hibernate分页基本概念 分页查询是指在检索数据时,不是一次性获取所有记录,而是分批地获取,每批数据称为一页。分页查询有助于减少内存消耗,避免一次性加载大量数据导致的性能问题。 二、Query与Criteria...

    Hibernate通用分页.docx

    Hibernate自动将HQL转换为数据库兼容的SQL,包括处理分页相关的部分。在执行分页查询时,`setFirstResult`和`setMaxResults`方法会添加到生成的SQL中,对应LIMIT和OFFSET子句。 4. **性能优化** 为了提高效率,...

    STRUTS+ HIBERNATE 简单分页

    4. **结果集转换**:查询到的数据需要转换为Java对象,Hibernate的`list()`方法可以完成这个任务。然后将这些对象封装到一个自定义的列表类中,这个类通常包含数据列表和一些分页信息,如总记录数、总页数等。 5. *...

    李兴华\struts\45-使用Struts _ DAO _ Hibernate完成分页

    - 构造查询条件:在DAO层,根据当前页和每页记录数构造分页SQL或HQL(Hibernate查询语言)。 - 执行查询:使用Hibernate Session执行分页查询。 - 返回结果:将查询结果包装成业务对象或列表,传回给控制器和视图...

    dwr+hibernate实现的分页技术

    ### dwr+hibernate实现的分页技术详解 在当今快速发展的互联网环境中,网站和应用程序需要处理大量的数据。为了提高用户体验并优化服务器资源利用,分页技术成为了必不可少的一部分。本文将详细介绍如何通过结合dwr...

    Hibernate通用分页

    HQL是Hibernate提供的面向对象的查询语言,它可以方便地将SQL语句转换为面向对象的形式,使得代码更加简洁、易读。而对象持久化则是Hibernate的核心功能,能够将Java对象与数据库中的记录进行映射,实现数据的增删改...

Global site tag (gtag.js) - Google Analytics