文章目录
pom.xml 文件引入框架pagehelper
<!--⚠️1. 分页插件【非常重要】 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.4</version>
</dependency>
config.xml配置信息:
<!-- 自定义别名 -->
<typeAliases>
<!--typeAlias用于配置别名。type属性指定的是实体类全限定类名。alias属性指定别名,当指定了别名就再区分大小写
<typeAlias type="com.itheima.domain.User" alias="user"></typeAlias>-->
<!-- 用于指定要配置别名的包,当指定之后,该包下的实体类都会注册别名,并且类名就是别名,不再区分大小写-->
<package name="com.accp.pojo"/>
</typeAliases>
<!-- ⚠️2.分页插件设置【非常重要】 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 -->
<property name="dialect" value="mysql" />
<!-- 设置为true时,使用RowBounds分页会进行count查询 -->
<property name="rowBoundsWithCount" value="true" />
</plugin>
</plugins>
PersonBiz类
/**
* 分页
* @param pageNum
* @param pageSize
* @return
*/
public PageInfo<Person> findAllByPage(Integer pageNum,Integer pageSize){
SqlSession session=null;
try {
session=ssf.openSession();
IPersonDao dao=session.getMapper(IPersonDao.class);
PageHelper.startPage(pageNum, pageSize);// ⚠️1.将获取到的页面数量和一页的行数 配置到 pageHalper 中
PageInfo<Person> pageInfo=new PageInfo<Person>(dao.queryAllPersons());// ⚠️2.将查询的全数据的返回值分装到PageInfo 对象中即可,
return pageInfo;
}catch(Exception ex) {
throw new RuntimeException(ex);
}finally {
session.close();
}
}
这样简单的分页就完成了
主要节点:
1.引入依赖
2.配置porperty 信息
3. 在查询数据的时候,将接收到的行数和页数分装到pageHelper 对象中
4. 然后将查询的数据分装到pageinfo 对象即可