Mybatis分页
一、为什么要分页?
二、SQL分页语句limit
1 2 3
| select * from mybatis.user limit 0,2; select * from mybatis.user limit 3; #[0,3]
|
三、使用Mybatis实现分页
1. 接口
1 2 3
| public interface UserMapper { List<User> getUserListBylimit(Map<String ,Integer> map); }
|
2. Mapper.xml
1 2 3
| <select id="getUserListBylimit" parameterType="map" resultType="user"> select * from mybatis.user limit #{startIndex},#{pageSize} </select>
|
3. 测试
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Test public void Test2(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map<String, Integer> map = new HashMap<>(); map.put("startIndex",1); map.put("pageSize",2);
List<User> userListBylimit = mapper.getUserListBylimit(map); System.out.println(userListBylimit); sqlSession.close(); }
|
四、通过RowBounds实现分页
不再使用SQL实现分页
1. 接口
1 2 3
| public interface UserMapper { List<User> getUserListByRowBounds(); }
|
2. Mapper.xml
1 2 3
| <select id="getUserListByRowBounds" resultType="user"> select * from mybatis.user </select>
|
3. 测试
1 2 3 4 5 6 7 8 9 10
| @Test public void Test3(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class);
RowBounds rowBounds = new RowBounds(1, 2); List<User> userList = sqlSession.selectList("com.nichu.dao.UserMapper.getUserListByRowBounds", null, rowBounds); System.out.println(userList); sqlSession.close(); }
|
-------------本文结束感谢您的阅读-------------