如何用mybatis做分页?
我目前正在开发一个电子商务应用程序,我必须使用搜索功能显示可用产品列表.
I am currently working on a ecommerce application where I have to show a list of available products using search functionality.
和每次搜索一样,我必须在这里实现分页.
As with every search, I have to implement Pagination here.
我使用 mybatis 作为我的 ORM 工具,使用 mysql 作为底层数据库.
I am using mybatis as my ORM tool and mysql as an underlying database.
通过谷歌搜索,我发现以下方法可以完成此任务:
Googling around I found following ways to accomplish this task :
客户端分页:在这里,我将不得不一次性从数据库中获取与搜索条件匹配的所有结果,并在我的代码级别(可能是前端代码)处理分页.
Client Side paging : Here I will have to fetch all the results from the database matching the search criteria in one stroke and handle the pagination at my code level (Possibly frond end code ).
服务器端分页:使用 mysql,我可以使用结果集的限制和偏移量来构建如下查询:SELECT * FROM sampletable WHERE condition1>1 AND condition2>2 LIMIT 0,20
Server Side Paging :
With mysql I can use the Limit and the offset of the resultset to construct a query like :
SELECT * FROM sampletable WHERE condition1>1 AND condition2>2 LIMIT 0,20
在这里,每次用户在搜索结果中导航时选择新页面时,我都必须传递偏移量和限制计数.
Here, I have to pass the offset and limit count everytime the user selects a new page while navigating in search results.
谁能告诉我,
- 哪种方式更好地实现分页?
- mybatis 是否支持比仅依赖上述 SQL 查询(如休眠条件 API)更好的实现分页的方法.
任何输入都受到高度赞赏.谢谢.
Any inputs is highly appreaciated. Thanks .
推荐答案
我自己在 sql 查询中使用你的第二个选项和 LIMIT.
I myself use your second opion with LIMIT in sql query.
但是有很多方法支持使用 RowBounds 类进行分页.这在 mybatis 文档这里
But there is range of methods that support pagination using RowBounds class. This is well described in mybatis documentation here
注意使用正确的结果集类型.
Pay attention to correct result set type to use.
相关文章