分页
1 使用Limit分页
语法:SELECT * from user limit startIndex,pageSize
-
使用Mybatis实现分页
-
接口
//分页实现查询 List<User> getUserByLimit(Map<String,Integer> map);
-
接口实现
<select id="getUserByLimit" resultType="user" parameterType="map">
select * from demo01.user limit #{startIndex},#{pageSize}
</select>
- 测试
@Test
public void getUserByLimitTest(){
SqlSession sqlsession = MybatisUtils.getSqlsession();
UserMapper mapper = sqlsession.getMapper(UserMapper.class);
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("startIndex",0);
map.put("pageSize",2);
List<User> userByLimit = mapper.getUserByLimit(map);
for (User user : userByLimit) {
System.out.println(user);
}
}
2 RowBounds分页
不再使用SQL实现分页
- 接口
//分页2
List<User> getUserByRowBounds();
2.Mapper.xml
<!-- 分页2-->
<select id="getUserByRowBounds" resultMap="UserMap">
select * from mybatis.user
</select>
3.测试
@Test
public void getUserByRowBounds(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
//RowBounds实现
RowBounds rowBounds = new RowBounds(0, 2);
//通过java代码层面实现分页
List<User> userList = sqlSession.selectList("com.kuang.dao.UserMapper.getUserByRowBounds",null,rowBounds);
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
3 分页插件
- PageHelper
了解即可,使用时,需要知道是什么东西!