Mybatis --> 6. 分页
6.1 使用Limit实现分页
SELECT * FROM user LIMIT startIndex, pageSize
- Mybatis实现分页核心:sql实现
-
接口
UserMapper.java
/** * 分页实现查询 * @param map * @return */ List<User> getUserByLimit(Map<String,Integer> map);
-
UserMapper.xml
<select id="getUserByLimit" parameterType="map" resultType="User"> select * from mybatis.user limit #{startIndex},#{pageSize} </select>
-
测试!
@Test public void getUserByLimit() { SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); Map<String,Integer> map = new HashMap<String, Integer>(); map.put("startIndex",1); map.put("pageSize",2); List<User> userList = mapper.getUserByLimit(map); for (User user: userList) { System.out.println(user); } sqlSession.close(); }
6.2 使用RowRounds分页
-
接口
UserMapper.java
/** * 分页实现查询 * @return */ List<User> getUserByRowRounds();
-
UserMapper.xml
<select id="getUserByRowRounds" resultType="User"> select * from mybatis.user </select>
-
测试
@Test public void getUserByRowRounds() { SqlSession sqlSession = MybatisUtils.getSqlSession(); RowBounds rowBounds = new RowBounds(1,2); List<User> userList = sqlSession.selectList("com.ano.dao.UserMapper.getUserByRowRounds", null, rowBounds); for (User user : userList) { System.out.println(user); } sqlSession.close(); }
6.3 分页插件
-
PageHelper