分页是为了减少数据的处理量
使用limit分页
语法
select * from user limit startIndex,pageSize
[0,n]
接口
//实现分页
List<User> getUserByLimit(Map<String,Integer> map);
Mapper.xml
<!--结果集映射-->
<resultMap id="UserMap" type="User">
<!--column是数据库中的字段,property实体中的属性
column是数据库中每个列的列名 后面的的property对应实体类里面的每个属性-->
<!-- <result column="id" property="id"/>
<result column="name" property="name"/>-->
<result column="pwd" property="password"/>
</resultMap>
<!--//实现分页-->
<select id="getUserByLimit" parameterType="map" resultMap="UserMap">
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",0);
map.put("pageSize",2);
List<User> list = mapper.getUserByLimit(map);
for (User user : list) {
System.out.println(user);
}
sqlSession.close();
}