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

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
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
根据引用和引用的内容,com.github.pagehelper:pagehelper5.0可能会导致ClassNotFoundException: mysql错误。在引用中提到,版本不兼容可能是导致问题的原因之一。你使用的是mysql 8.0.24版本,而com.github.pagehelper:pagehelper5.0可能适用于mysql 5.0及以下版本。因此,你需要确认你的pagehelper版本是否与你使用的mysql版本兼容。你可以尝试使用适用于mysql 8.0.24版本pagehelper版本来解决这个问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Caused by: java.lang.ClassCastException: class com.github.pagehelper.PageHelper cannot be cast to cl](https://blog.csdn.net/sm923/article/details/118704108)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [mybatis使用pagehelper分页报错java.lang.ClassCastException: com.github.pagehelper.PageHelper cannot ...](https://blog.csdn.net/Baymax0912/article/details/109318367)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值