oracle中分页查询因为存在伪列rownum,sql语句写起来较为复杂,现在介绍一种通过使用mybatis中的RowBounds进行分页查询,非常方便。 使用MyBatis中的RowBounds进行分页查
oracle中分页查询因为存在伪列rownum,sql语句写起来较为复杂,现在介绍一种通过使用mybatis中的RowBounds进行分页查询,非常方便。
使用MyBatis中的RowBounds进行分页查询时,不需要在 sql 语句中写 offset,limit,mybatis 会自动拼接 分页sql ,添加 offset,limit,实现自动分页。
需要前台传递参数currentPage和pageSize两个参数,分别是当前页和每页数量,controller层把参数传递给service层即可,下面是service实现的代码:
package com.xyfer.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.RowBounds;
import com.xyfer.dao.UserDao;
import com.xyfer.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
@Override
public Map<String, Object> queryUserList(String currentPage, String pageSize) {
//查询数据总条数
int total = userDao.queryCountUser();
//返回结果集
Map<String,Object> resultMap = new HashMap<String,Object>();
resultMap.put("total", total);
//总页数
int totalpage = (total + Integer.parseInt(pageSize) - 1) / Integer.parseInt(pageSize);
resultMap.put("totalpage", totalpage);
//数据的起始行
int offset = (Integer.parseInt(currentPage)-1)*Integer.parseInt(pageSize);
RowBounds rowbounds = new RowBounds(offset, Integer.parseInt(pageSize));
//用户数据集合
List<Map<String, Object>> userList = userDao.queryUserList(rowbounds);
resultMap.put("userList", userList);
return resultMap;
}
}
--结束END--
本文标题: Oracle使用MyBatis中RowBounds实现分页查询功能
本文链接: https://www.lsjlt.com/news/52882.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-02
2024-05-02
2024-05-02
2024-05-02
2024-05-02
2024-05-02
2024-05-02
2024-05-02
2024-05-02
2024-05-02
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0