分页查询
回顾MySQL的分页查询
select * from user limit startindex,pagesize;
//startindex 是从第几条数据开始查
//pagesize 这个是显示几条数据
// 如果只写一个参数 则默认表示(0,参数值)
在mybatis的分页查询 xxxMapper.xml文件下sql语句
<!-- 分页查询-->
<select id="getindex" parameterType="map" resultMap="user">
select * from mybatis.user limit #{startIndex}, #{pagesize}
</select>
接口
//分页查询
List<Mybatis> getindex(Map<String,Object> map);
测试
@Test
public void testindex() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String,Object> map=new HashMap<String,Object> ();
map.put("startIndex",0);
map.put("pagesize",2);
List<Mybatis> getindex = mapper.getindex(map);
for (Mybatis user:getindex){
System.out.println(user);
}
sqlSession.close();
}
或者使用PagHelper
1 加入PagHelper依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.6</version>
</dependency>
在这里插入代码片
2 在mybatis主配置文件中加入plugin声明
—<plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins>
3 在select语句之前调用PagHelper依赖
@Test
public void test06(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
List<Blog> getbolg = mapper.getbolg();
PageHelper.startPage(1,1);
for (Blog user:getbolg){
System.out.println(user);
}
}
可能会因为版本问题 无法使用 建议还是用其它方式