进行项目开发时,我们有时会查询数据列表,可是数据库不会显示所有的数据,因此这时就会使用分页功能,那mybatis怎么实现分页?下面来我们就来给大家讲解一下mybatis实现分页功能方法。
1.数组分页
查询出全部数据,然后再list中截取需要的部分;
mybatis接口
List < Student > queryStudentsByArray();
xml配置文件
<select id="queryStudentsByArray" resultMap="studentmapper"> select * from student </select>
service
接口 List < Student > queryStudentsByArray(int currPage, int pageSize); 实现接口 @Override public List < Student > queryStudentsByArray(int currPage, int pageSize) { //查询全部数据 List < Student > students = studentMapper.queryStudentsByArray(); //从第几条数据开始 int firstIndex = (currPage - 1) * pageSize; //到第几条数据结束 int lastIndex = currPage * pageSize; return students.subList(firstIndex, lastIndex); //直接在list中截取 }
controller
@ResponseBody @RequestMapping("/student/array/{currPage}/{pageSize}") public List < Student > getStudentByArray(@PathVariable("currPage") int currPage, @PathVariable("pageSize") int pageSize) { List < Student > student = StuServiceIml.queryStudentsByArray(currPage, pageSize); return student; }
2.Sql分页
使用limit关键字分页,limit start,pageSize
mybatis接口
List < Student > queryStudentsBySql(Map < String, Object > data);
<select id="queryStudentsBySql" parameterType="map" resultMap="studentmapper"> select * from student limit #{currIndex} , #{pageSize} </select>
service
接口 List < Student > queryStudentsBySql(int currPage, int pageSize); 实现类 public List < Student > queryStudentsBySql(int currPage, int pageSize) { Map < String, Object > data = new HashedMap(); data.put("currIndex", (currPage - 1) * pageSize); data.put("pageSize", pageSize); return studentMapper.queryStudentsBySql(data); }
controller
@ResponseBody @RequestMapping("/student/array/{currPage}/{pageSize}") public ListqueryStudentsBySql(@PathVariable("currPage") int currPage, @PathVariable("pageSize") int pageSize) { Liststudent = StuServiceIml.queryStudentsBySql(currPage, pageSize); return student; }
3.RowRounds分页
在 mybatis 中,使用 RowBounds 进行分页,非常方便,不需要在 sql 语句中写 limit,即可完成分页功能。但是由于它是在 sql 查询出所有结果的基础上截取数据的,所以在数据量大的sql中并不适用,它更适合在返回数据结果较少的查询中使用
最核心的是在 mapper 接口层,传参时传入 RowBounds(int offset, int limit) 对象,即可完成分页
mapper接口
@Mapper public interface BookMapper { //添加数据 int insert(Book book); //模糊查询 List < Book > selectBookByName(Map < String, Object > map, RowBounds rowBounds); }
xml文件
<select id="selectBookByName" resultMap="BaseResultMap"> <bind name="pattern_bookName" value="'%' + bookName + '%'" /> <bind name="pattern_bookAuthor" value="'%' + bookAuthor + '%'" /> select * from book where 1 = 1 <if test="bookName != null and bookName !=''"> and book_name LIKE #{pattern_bookName} </if> <if test="bookAuthor != null and bookAuthor !=''"> and book_author LIKE #{pattern_bookAuthor} </if> </select>
service
public List < Book > selectBookByName(String pageNo, String pageSize) { List < Book > list = bookMapper.selectBookByName(map, new RowBounds((pageNo - 1) * pageSize, pageSize)); return list; }
controller
@RequestMapping("/selectBookByName") @ResponseBody public Page selectBookByName(String pageNo, String pageSize, HttpServletRequest request, HttpServletResponse response) { pageNo = pageNo == null ? "1" : pageNo; //当前页码 pageSize = pageSize == null ? "5" : pageSize; //页面大小 //获取当前页数据 List < Book > list = bookService.selectBookByName(pageNo, pageSize); //获取总数据大小 //int totals = bookService.getAllBook(); //封装返回结果 Page page = new Page(); page.setTotal(totals + ""); page.setRows(list); return page; }
这就是mybatis实现分页的三种方法,简单的说,数组分页就是在list中截部分数据,Sql分页使用sql语句中limit关键字,RowBounds分页就是针对ResultSet结果集执行的内存分页。最后大家如果想要了解更多java架构师知识,敬请关注奇Q工具网。
推荐阅读:
java怎么操作excel?java操作excel的几种方式