Mybatis07:分页
程序员文章站
2022-04-22 22:21:10
...
7、分页
思考:为啥要分页??
- 减少数据的处理量,查询快速
7.1、使用limit分页
SELECT * FROM USER LIMIT 0, 2; -- 每页显示两个从第0个开始查,步长为2
SELECT * FROM USER LIMIT 4; -- 从第0个开始查,一直到第n个[0, n]
使用Mybatis实现分页,核心就是SQL
-
接口
//分页查询 public List<User> getUserByLimit(Map<String, Integer> map);
-
Mapper.xml
<select id="getUserByLimit" resultMap="User" parameterType="map"> select * from mybatisdb.user limit #{startIndex}, #{pageSize}; </select>
-
测试
@Test public void getUserByLimit() { SqlSession sqlSession = MybatisUtils.getSqlSession(); UserDao mapper = sqlSession.getMapper(UserDao.class); Map<String, Integer> map = new HashMap<String, Integer>(); map.put("startIndex", 0); map.put("pageSize", 2); List<User> user = mapper.getUserByLimit(map); for (User user1 : user) { System.out.println(user1); } sqlSession.close(); }