limit分页
limit分页基于sql语句:
#语法
SELECT * FROM table LIMIT stratIndex,pageSize
SELECT * FROM table LIMIT 5,10; // 检索记录行 6-15
#为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1:
SELECT * FROM table LIMIT 95,-1; // 检索记录行 96-last.
#如果只给定一个参数,它表示返回最大的记录行数目:
SELECT * FROM table LIMIT 5; //检索前 5 个记录行
#换句话说,LIMIT n 等价于 LIMIT 0,n。
步骤1:添加getUserByPage方法
//分页查询
public List<User> getUserByPage(Map<String,Integer>map);
步骤2:修改Mapper文件
<select id="getUserByPage" parameterType="map" resultMap="userResultMap">
select * from mybatis_db.user_t limit #{
startIndex},#{
pageSize}
</select>
步骤3:测试
@Test
public void getUserByPage(){
SqlSession sqlSession = MyBatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("startIndex",0);
map.put("pageSize",3);
List<User> users=mapper.getUserByPage(map);
for(User user:users){
System.out.println(user);
}
sqlSession.close();
}
- 结果