- 在pom.xml中添加pageHelper依赖
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
- 在mybatis配置文件中添加插件依赖
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 配置数据库的类型 -->
<property name="helperDialect" value="mysql"/>
<property name="reasonable" value="true"/>
</plugin>
- DAO层Mapper写法和无分页写法相同,在Service层调用插件
public class UserService {
/**
*
* @param pageNum
* @param map
* @return
*/
public PageInfo<User> selectPage(int pageNum, Map<String, Object> map) {
SqlSession sqlSession = SqlSessionUtil.openSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//开启分页功能
PageHelper.startPage(pageNum, 5);
List<User> users = userMapper.select(map);
//PageInfo对象获得非常全面的分页信息
PageInfo<User> page = new PageInfo<User>(users);
return page;
} finally {
SqlSessionUtil.close(sqlSession);
}
}
}
Debug模式下查看到PageInfo中的属性,常用的pageNum当前页,pages总页数,list分页查询的数据等…
来源:CSDN
作者:Chill_Lyn_
链接:https://blog.csdn.net/Chill_Lyn/article/details/103752106