7.分页
思考:为什么要分页
- 减少数据的处理量
7.1使用Limit分页
语法:SELECT * form user limit startIndex,pageSize;
SELECT * from user limit 3; #[0,n]
使用Mybatis实现分页,核心SQL
- 接口
//分页查询
List<User> getUserByLimit(Map<String,Integer> map);
}
- Mapper.xml
<!--分页查询-->
<select id="getUserByLimit" parameterType="map" resultMap="UserMap">
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<String, Integer>();
//需要一个map,故new一个Hashmap
map.put("startIndex",0);
map.put("pageSize",2);
List<User> userList = mapper.getUserByLimit(map);
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
7.2RowBounds分页【不建议开发中使用】
不再使用SQL实现分页
1.接口
//使用RowBounds实现分页
List<User> getUserByRowBounds();
2.Mapper.xml
<!--分页查询RowBounds-->
<select id="getUserByRowBounds" resultMap="UserMap">
select * from mybatis.user
</select>
3.测试
@Test
public void getUserByRowBounds(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
//RowBounds实现
RowBounds rowBounds = new RowBounds(1,2);
//通过Java代码实现分页
List<User> userList = sqlSession.selectList("com.itxiaofei.dao.UserMapper.getUserByRowBounds", null, rowBounds);
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
7.3分页插件
了解即可,万一以后公司的架构师,要求使用,要知道这是什么东西!
标签:map,分页,List,sqlSession,user,RowBounds From: https://www.cnblogs.com/itxiaofei/p/16835135.html