分页
为什么要分页?
- 减少数据的处理量
使用Limit分页
语法SELECT * from user limit startIndex,pageSize;
使用Mybatis实现分页,核心SQL
1.接口
//分页
List<User> getUserByLimit(Map<String,Integer>map);
2.Mapper.xml
<!--分页-->
<select id="getUserByLimit" parameterType="map" resultType="user">
select * from mybatis.user limit #{startIndex},#{pageSize}
</select>
3.测试
@Test
public void getUserByLimit(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserMapper mapper=sqlSession.getMapper(UserMapper.class);
HashMap<String,Integer> map = new HashMap<String, Integer>();
map.put("startIndex",2);
map.put("pageSize",2);
List<User> userList = mapper.getUserByLimit(map);
for(User user:userList){
System.out.println(user);
}
sqlSession.close();
}
4.出现Bug
报错:
org.apache.ibatis.builder.IncompleteElementException: Could not find result map ‘com.kuang.dao.UserMapper.user’ referenced from ‘com.kuang.dao.UserMapper.getUserByLimit’
问题:

解决:
将resultMap改成resultType
参考链接: link.
本文探讨了为何需要进行数据分页,主要介绍了如何使用Mybatis的Limit来实现分页功能。通过详细步骤,包括定义接口、编写Mapper.xml、编写测试用例,展示了分页查询的过程。在实践中遇到的‘找不到resultMap’的错误,通过将resultMap替换为resultType的方式得到了解决。
330

被折叠的 条评论
为什么被折叠?



