PageHelper在SpringBoot中的应用

PageHelper在SpringBoot中的应用

1.POM文件

<dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>5.1.2</version>
</dependency>

2. pageHelper拦截器的了解

PageHelper就是实现了一个MyBatis的自定义拦截器

//@Intercepts:标识该类是一个自定义拦截器
//@Signature:指明我们需要拦截的接口和方法
/** type :mybatis 拦截器默认可拦截的类型只有四种,即四种接口类型 Executor、StatementHandler、ParameterHandler 和 ResultSetHandler
 *  对于我们的自定义拦截器必须使用 mybatis 提供的注解来指明我们要拦截的是四类中的哪一个类接口
 *  method : 对应接口中的哪类方法
    args   :指明参数类型,从而确定是哪一个方法
 */
@Intercepts({@Signature(
    type = Executor.class,
    method = "query",
    args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
), @Signature(
    type = Executor.class,
    method = "query",
    args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}
)})
public class PageInterceptor implements Interceptor {
    protected Cache<String, MappedStatement> msCountMap = null;
    private Dialect dialect;
    private String default_dialect_class = "com.github.pagehelper.PageHelper";
    private Field additionalParametersField;
    private String countSuffix = "_COUNT";

3.配置文件中添加拦截器

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <plugins>
        <!-- com.github.pagehelper为PageHelper类所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!-- 使用下面的方式配置参数,参数介绍参考官方文档 -->
            <!--            <property name="param1" value="value1"/>-->
        </plugin>
    </plugins>
</configuration>

4.pageHelper使用

/*doSelectPageInfo方法解释 : 是PageHelper.startPage()函数返回的默认Page实例内置的函数,该函数可以用以Lambda的形式通过额外的Function来进行查询而不需要再进行多余的PageInfo与List转换,而doSelectPageInfo的参数则是PageHelper内置的Function(ISelect)接口用以达到转换PageInfo的目的
*/  
PageInfo</*返回类型*/> objectPageInfo = PageHelper.startPage( /*参数页码, 参数页面长度*/)
                .doSelectPageInfo(() -> /*mysql数据查询:mapper.XXXX(参数)*/);
要在Spring Boot项目中使用PageHelper,你可以按照以下步骤进行操作: 1. 首先,在你的项目的pom.xml文件中添加PageHelper的依赖。你可以在优快云或者其他资源中找到PageHelper的最新版本,并将其添加到dependencies标签中,类似于下面的代码片段: ```xml <dependencies> <!-- 其他依赖 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>最新版本</version> </dependency> </dependencies> ``` 2. 接下来,在你的Spring Boot应用程序的配置文件(例如application.properties或application.yml)中,添加PageHelper的配置。下面是一个示例配置: ```properties # application.properties pagehelper.helper-dialect=mysql pagehelper.reasonable=true pagehelper.support-methods-arguments=true pagehelper.params=count=countSql ``` ```yaml # application.yml pagehelper: helper-dialect: mysql reasonable: true support-methods-arguments: true params: count=countSql ``` 根据你的数据库类型,修改`pagehelper.helper-dialect`的值。 3. 最后,你可以在你的Mapper接口方法上使用PageHelper进行分页查询。例如,在使用MyBatis的情况下,可以在查询方法上添加`PageHelper.startPage(pageNum, pageSize)`来指定分页参数。例如: ```java import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; @Service public class UserService { @Autowired private UserMapper userMapper; public PageInfo<User> getUsers(int pageNum, int pageSize) { PageHelper.startPage(pageNum, pageSize); List<User> userList = userMapper.getUsers(); return new PageInfo<>(userList); } } ``` 以上就是在Spring Boot项目中引入和使用PageHelper的基本步骤。希望能对你有所帮助!如有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值