分页:为了减少数据处理量
7.1 limit 分页
select * from todo limit 0,2;
7.2 mybatis分页
编写分页sql
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lu.dao.TodoMapper">
<select id="queryAll" resultType="Todo">
select *
from todo;
</select>
<select id="queryLimit" resultType="Todo" parameterType="map">
select *
from todo limit #{startIndex},#{pageSize};
</select>
</mapper>
调用
Map<String, Integer> map = new HashMap<>();
map.put("startIndex",1);
map.put("pageSize",2);
mapper.queryLimit(map).forEach(System.out::println);
7.3 RowBounds
不再使用SQL进行分页,在Java层进行分页
RowBounds rowBounds = new RowBounds(1, 2);
sqlSession.selectList("com.lu.dao.TodoMapper.queryAll", null, rowBounds).forEach(System.out::println);
sqlSession.close();