spring集成PageHelper

本文介绍如何在MyBatis中配置PageHelper分页插件,包括依赖引入、Spring整合配置、自定义返回类等步骤。适用于希望在项目中实现高效分页功能的开发者。

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

一、导入依赖

	<!--分页插件-->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>4.1.3</version>
    </dependency>
    <dependency>
      <groupId>com.github.jsqlparser</groupId>
      <artifactId>jsqlparser</artifactId>
      <version>0.9.5</version>
    </dependency>

二、在applicationContext.xml中配置
主要是引用文件mybatis-config.xml

	<!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 扫描model包 -->
        <property name="typeAliasesPackage" value="com.chuji.model"/>
        <!-- 扫描sql配置文件:mapper需要的xml文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        <!-- 配置分页插件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>

三、新建一个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>

        <settings>
            <setting name="mapUnderscoreToCamelCase" value="true"/>
        </settings>

        <plugins>
            <!-- com.github.pagehelper为PageHelper类所在包名 -->
            <plugin interceptor="com.github.pagehelper.PageHelper">
                <!-- 设置数据库类型Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 -->
                <property name="dialect" value="mysql"/>
            </plugin>
        </plugins>

    </configuration>

四、在controller里面使用

    /**
     * 获取分页数据
     * @param userVO
     * @return
     */
    @RequestMapping("/getUserList")
    public @ResponseBody
    CommonResponse<DataGrid<UserPO>> getUserList(@RequestBody UserVO userVO){
        // 分页数据
        Page<UserPO> page = PageHelper.startPage(userVO.getPageNum(), userVO.getPageSize(), true);
        // 列表
        List<UserPO> userPOList = userPOMapper.getUserList();

        DataGrid<UserPO> purchaseOrderVOData = DataGridUtils.buildDataGrid(page, userPOList);

        return new CommonResponse<DataGrid<UserPO>>(ResponseTypeEnums.SUCCESS, null, null, purchaseOrderVOData);
    }

五、其中有自己定义的返回类
DataGrid

package com.chuji.util.DataGrid;

import java.util.List;


public class DataGrid<T> {
	private Integer pageSize;// 每页行数
	private long total;// 总行数
	private List<T> data;
	

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public List<T> getData() {
		return data;
	}

	public void setData(List<T> data) {
		this.data = data;
	}

}

好了这样就大功告成了!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欧阳锋feng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值