欢迎来到站长天空!

其它数据库

当前位置: 主页 > 数据库 > 其它数据库

Oracle使用MyBatis中RowBounds实现分页查询功能

时间:2025-12-23 21:00:02|栏目:其它数据库|点击:

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 queryUserList(String currentPage, String pageSize) {
    //查询数据总条数
    int total = userDao.queryCountUser();
    //返回结果集
    Map resultMap = new HashMap();
    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> userList = userDao.queryUserList(rowbounds);
    resultMap.put("userList", userList);
    return resultMap;
  }
}

dao层接口:

package com.xyfer.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.RowBounds;
public interface UserDao {
  public int queryCountUser();    //查询用户总数
  public List> queryUserList(RowBounds rowbounds);  //查询用户列表
}

对应的mapper.xml文件:




  
  
  
  

通过postman调用接口,传入对应的参数,即可实现分页查询数据。

总结

以上所述是小编给大家介绍的Oracle使用MyBatis中RowBounds实现分页查询功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

上一篇:linux服务器开机启动oracle的设置方法

栏    目:其它数据库

下一篇:oracle数据库导入.dmp脚本的sql 语句

本文标题:Oracle使用MyBatis中RowBounds实现分页查询功能

本文地址:https://zz.feitang.co/shujuku/32992.html

广告投放 | 联系我们 | 版权申明

申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:257218569 | 邮箱:257218569@qq.com

Copyright © 2018-2025 站长天空 版权所有 Powered by EyouCms冀ICP备14023439号