分页:为了减少数据处理量

7.1 limit 分页

  1. select * from todo limit 0,2;

7. 分页 - 图1

7.2 mybatis分页

编写分页sql

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.lu.dao.TodoMapper">
  6. <select id="queryAll" resultType="Todo">
  7. select *
  8. from todo;
  9. </select>
  10. <select id="queryLimit" resultType="Todo" parameterType="map">
  11. select *
  12. from todo limit #{startIndex},#{pageSize};
  13. </select>
  14. </mapper>

调用

  1. Map<String, Integer> map = new HashMap<>();
  2. map.put("startIndex",1);
  3. map.put("pageSize",2);
  4. mapper.queryLimit(map).forEach(System.out::println);

7.3 RowBounds

不再使用SQL进行分页,在Java层进行分页

  1. RowBounds rowBounds = new RowBounds(1, 2);
  2. sqlSession.selectList("com.lu.dao.TodoMapper.queryAll", null, rowBounds).forEach(System.out::println);
  3. sqlSession.close();

7.4 分页插件

image.png