Oracle使用MyBatis中RowBounds实现分页查询功能
Oracle中分页查询因为存在伪列rownum,sql语句写起来较为复杂,现在介绍一种通过使用MyBatis中的RowBounds进行分页查询,非常方便。
使用MyBatis中的RowBounds进行分页查询时,不需要在sql语句中写offset,limit,mybatis会自动拼接分页sql,添加offset,limit,实现自动分页。
需要前台传递参数currentPage和pageSize两个参数,分别是当前页和每页数量,controller层把参数传递给service层即可,下面是service实现的代码:
packagecom.xyfer.service.impl;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
importorg.apache.ibatis.session.RowBounds;
importcom.xyfer.dao.UserDao;
importcom.xyfer.service.UserService;
publicclassUserServiceImplimplementsUserService{
privateUserDaouserDao;
@Override
publicMapqueryUserList(StringcurrentPage,StringpageSize){
//查询数据总条数
inttotal=userDao.queryCountUser();
//返回结果集
MapresultMap=newHashMap();
resultMap.put("total",total);
//总页数
inttotalpage=(total+Integer.parseInt(pageSize)-1)/Integer.parseInt(pageSize);
resultMap.put("totalpage",totalpage);
//数据的起始行
intoffset=(Integer.parseInt(currentPage)-1)*Integer.parseInt(pageSize);
RowBoundsrowbounds=newRowBounds(offset,Integer.parseInt(pageSize));
//用户数据集合
List
dao层接口:
packagecom.xyfer.dao;
importjava.util.List;
importjava.util.Map;
importorg.apache.ibatis.session.RowBounds;
publicinterfaceUserDao{
publicintqueryCountUser();//查询用户总数
publicList
对应的mapper.xml文件:
selectcount(1)fromuser select*fromuser
通过postman调用接口,传入对应的参数,即可实现分页查询数据。
总结
以上所述是小编给大家介绍的Oracle使用MyBatis中RowBounds实现分页查询功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!