springboot配置pagehelper5.0和4.0版本的区别

本文详细对比了PageHelper 4.0与5.0版本的主要区别,包括拦截器的变化、移除的类以及在SpringBoot环境下配置两者的不同方法。适用于从4.0版本升级到5.0版本的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot配置pagehelper5.0和4.0版本的区别

pagehelper5.0使用的拦截器是PageInterceptor,pagehelper4.0使用的拦截器是PageHelper;而且5.0的版本去除了sqlsource模块的好几个类OrderByStaticSqlSource、PageSqlSource、PageRawSqlSource、PageStaticSqlSource、PageDynamicSqlSource、PageProviderSqlSource,pagehelper4.0版本切到pagehelper5.0需要注意这个问题。下面分别讲解springboot配置pagehelper5.0和4.0的版本,详细的配置见码云上的代码工程:freemarkerDemo

springboot配置pagehelper5.0

maven配置

引入pagehelper-spring-boot-starter采用的是pagehelper5.0版本

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>

controller类测试方法

    //localhost:8080/list?pageNum=1&pageSize=11
    @RequestMapping("/list")
    public String list(Model model, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                       @RequestParam(value = "pageSize", defaultValue = "2") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);//pageNum页码,pageSize每页大小
        List<UserInfo> list = userService.selectAllUsers();
        PageInfo page = new PageInfo(list);
        model.addAttribute("page", page);
        return "content";
    }

springboot配置pagehelper4.0

maven配置

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

配置pagehelper拦截器

方式一:使用mybatis配置文件

第一步:application.properties文件增加一条配置

mybatis.config-location= classpath:mybatis-config.xml

第二步:在mybatis-config.xml配置拦截器

<?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>
    <typeAliases>
        <!--<package name="com.pingan.core.entity"/>-->
    </typeAliases>

    <!-- 配置mybatis的分页插件PageHelper -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 设置数据库类型Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 -->
            <property name="dialect" value="oracle"/>
        </plugin>
    </plugins>
</configuration>
方式二:直接在启动类App.java类里通过编程式配置拦截器
@SpringBootApplication
@ComponentScan(basePackages = {"com.pingan.core"})
@Slf4j
public class App {
    public static void main(String[] args) throws Exception {
       SpringApplication.run(App.class, args);
    }

    @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum","true");
        properties.setProperty("rowBoundsWithCount","true");
        properties.setProperty("reasonable","true");
        properties.setProperty("dialect","oracle");    //配置mysql数据库的方言
        pageHelper.setProperties(properties);
        return pageHelper;
    }
}

controller类测试方法(同上)

    //localhost:8080/list?pageNum=1&pageSize=11
    @RequestMapping("/list")
    public String list(Model model, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                       @RequestParam(value = "pageSize", defaultValue = "2") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);//pageNum页码,pageSize每页大小
        List<UserInfo> list = userService.selectAllUsers();
        PageInfo page = new PageInfo(list);
        model.addAttribute("page", page);
        return "content";
    }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值