成都创新互联网站制作重庆分公司

Oracle使用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 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;
  }
}

当前标题:Oracle使用MyBatis中RowBounds实现分页查询功能-创新互联
网站地址:http://cxhlcq.com/article/piphi.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部