应用 limit 分页
外围 sql
1.select * from user limit startIndex,pageSize;
2.select * from user limit 3; #[0,3)
- 接口
// 分页
List<User> getUserByLimit(Map<String,Integer> map);
- Mapper.xml
<!-- 分页 -->
<select id="getUserByLimit" parameterType="map" resultType="helloUser">
select * from mybatis.user limit #{startIndex},#{pageSize}
</select>
- 测试
@Test
public void getUserByLimit(){SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Integer> map = new HashMap<>();
map.put("startIndex",2);
map.put("pageSize",2);
List<User> userList = mapper.getUserByLimit(map);
for (User user : userList) {System.out.println(user);
}
sqlSession.close();}
应用 RowBounds 分页
- 接口
List<User> getUserByRowBounds();
- Mapper.xml
<select id="getUserByRowBounds" resultType="helloUser">
select * from mybatis.user
</select>
- 测试
@Test
public void getUserByRowBounds(){SqlSession sqlSession = MybatisUtils.getSqlSession();
//RowBounds 实现
RowBounds rowBounds = new RowBounds(1, 2);
// 通过 Java 代码层面实现分页
List<User> userList = sqlSession.selectList("com.sunfl.dao.UserMapper.getUserByRowBounds",null,rowBounds);
for (User user : userList) {System.out.println(user);
}
sqlSession.close();}
分页插件
pageHelper