1.分页的目的
减少数据量
2.分页的方法有几种
- 2.1 第一种:limit分页 sqly语句:select * from user limit(起始下标,每页显示个数)
与Mybatis结合步骤:
创建接口
//分页 List<User> getUserByLimit(Map<String,Integer> map);
创建mapper.xml
<mapper namespace="com.neusoft.dao.UserMapper"> <resultMap id="UserMap" type="User"> <result column="name" property="username"/> </resultMap> <select id="getUserByLimit" parameterType="map" resultMap="UserMap"> select id,name,password,email from users limit #{startIndex},#{pageSize} </select> </mapper>
测试
@Test public void test1(){ 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> list= mapper.getUserByLimit(map); for (User u:list) { System.out.println(u); } sqlSession.close(); }
- 2.2 第二种:RowBounds类
创建接口
//RowBounds分页 List<User> getUserByRowBounds();
创建mapper.xml
<select id="getUserByRowBounds" resultMap="UserMap"> select id,name,password,email from users </select>
测试
@Test public void test2(){ SqlSession sqlSession=MybatisUtils.getSqlSession(); RowBounds rowBounds=new RowBounds(1,2); List<User> userList= sqlSession.selectList("com.neusoft.dao.UserMapper.getUserByRowBounds",null,rowBounds); for (User user : userList) { System.out.println(user); } sqlSession.close(); }
2.3 第三种Mybatis分页插件 pageHelper