pagehelper 使用步骤全解析
在进行 web 应用开发时,经常会涉及到数据库数据的分页展示。pagehelper 是一个非常实用的 mybatis 分页插件,它能够方便地实现数据库查询结果的分页功能,极大地提高了开发效率。以下将简单介绍 pagehelper 的使用步骤。
一、引入依赖
maven 项目
如果你的项目是基于 maven 构建的,需要在项目的pom.xml文件中添加 pagehelper 的依赖项。
<!--pagehelper分页插件-->
<dependency>
<groupid>com.github.pagehelper</groupid>
<artifactid>pagehelper-spring-boot-starter</artifactid>
<version>1.4.0</version>
</dependency>gradle 项目
对于 gradle 项目,在build.gradle文件中添加以下依赖:
implementation 'com.github.pagehelper:pagehelper-spring-boot-starter:1.4.0'
二、配置 pagehelper
- 如果是 spring boot 项目,在
application.properties或application.yml文件中进行配置。以application.yml为例:
pagehelper: helper-dialect: mysql # 指定数据库方言,这里以mysql为例 reasonable: true # 分页合理化,如果pagenum<1会查询第一页,如果pagenum>pages会查询最后一页 support-methods-arguments: true # 支持通过mapper接口参数传递分页参数 params: count=countsql # 用于从对象中根据属性名取值,这里配置count的sql
这些参数的详细解释如下:
helper-dialect: 指定分页插件的数据库方言。pagehelper会自动检测当前的数据库链接,自动选择合适的分页方式。如果你使用的是mysql,可以明确指定为mysql。reasonable: 是否启用分页合理化。如果启用,当pagenum<1时,会自动查询第一页的数据,当pagenum>pages时,自动查询最后一页数据;不启用的情况下,以上两种情况都会返回空数据。support-methods-arguments: 是否支持通过mapper接口参数来传递分页参数,默认值false。设置为true时,pagehelper会从查询方法的参数值中自动根据配置的字段取值,进行分页。params: 用于从对象中根据属性名取值,可以配置pagenum,pagesize,count,pagesizezero,reasonable等参数。这里的count=countsql表示在执行分页查询时,会使用countsql作为计算总数的sql。
三、在代码中使用 pagehelper
1.简单分页查询
假设我们有一个用户表(user),要查询并分页展示用户信息。首先,在对应的 mapper 接口中定义查询方法:
import com.example.entity.user;
import java.util.list;
public interface usermapper {
@select("select * from user")
list<user> selectallusers();
}然后,在 service 层或调用 mapper 的地方使用 pagehelper 进行分页查询,示例代码如下:
import com.github.pagehelper.pagehelper;
import com.github.pagehelper.pageinfo;
import com.example.mapper.usermapper;
import com.example.entity.user;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
import java.util.list;
@service
public class userservice {
@autowired
private usermapper usermapper;
public pageinfo<user> getuserlist(int pagenum, int pagesize) {
// 设置分页参数,pagenum为当前页码,pagesize为每页显示的记录数
pagehelper.startpage(pagenum, pagesize);
// 执行查询
list<user> userlist = usermapper.selectallusers();
// 使用pageinfo对查询结果进行包装,返回包含分页信息的对象
return new pageinfo<>(userlist);
}
}在上述代码中,pagehelper.startpage(pagenum, pagesize)方法用于设置分页参数,告诉 pagehelper 要查询第几页以及每页显示多少条记录。然后执行查询操作,pagehelper 会自动对查询语句进行分页处理,最后将查询结果包装成pageinfo对象返回。pageinfo对象包含了分页相关的各种信息,如总记录数、总页数、当前页码、每页记录数等,方便在前端进行分页展示和相关逻辑处理。
2.带条件的分页查询
如果查询需要带条件,例如根据用户姓名进行模糊查询并分页。首先在 mapper 接口中定义带条件的查询方法:
import com.example.entity.user;
import org.apache.ibatis.annotations.mapper;
import org.apache.ibatis.annotations.param;
import org.apache.ibatis.annotations.select;
import java.util.list;
@mapper
public interface usermapper {
@select("select * from user where user_name like #{name}")
list<user> selectusersbyname(@param("name") string name);
}在 service 层实现带条件的分页查询,代码如下:
import com.github.pagehelper.pagehelper;
import com.github.pagehelper.pageinfo;
import com.example.mapper.usermapper;
import com.example.entity.user;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
import java.util.list;
@service
public class userservice {
@autowired
private usermapper usermapper;
public pageinfo<user> getuserlistbyname(string name, int pagenum, int pagesize) {
// 设置分页参数
pagehelper.startpage(pagenum, pagesize);
// 执行带条件的查询
list<user> userlist = usermapper.selectusersbyname("%" + name + "%");
// 返回分页信息对象
return new pageinfo<>(userlist);
}
}这样我们就能简单的使用pagehelper的分页功能 ,详细的使用可以参考文档如何使用分页插件
到此这篇关于springboot整合mybatis-plus使用pagehelper进行分页的文章就介绍到这了,更多相关springboot整合mybatis-plus分页内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论