分页
分页可以减少数据的处理量,实际应用非常广泛
1.使用limit分页【重点】
sql语句
select * from user limit startIndex,pageSize
使用mybatis实现分页,核心sql
-
接口
//分页查询 List<User> getUserByLimit(Map<String, Integer> map);
-
配置文件UserMapper.xml
<select id="getUserByLimit" parameterType="map" resultType="user"> select * from user limit #{startIndex},#{pageSize} </select>
-
测试方法
@Test public void getUserByLimitTest() { 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", 4); List<User> userByLimit = mapper.getUserByLimit(map); for (User user : userByLimit) { System.out.println(user); } sqlSession.close(); }
-
结果