Spring 통합 PageHelper

첫째, 종속성 가져 오기

	<!--分页插件-->
    <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>

네, 컨트롤러에서 사용

    /**
     * 获取分页数据
     * @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 의 자체 정의가있는 Five

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;
	}

}

글쎄, 그게 다야! ! !

추천

출처blog.csdn.net/BOOM0BOOM/article/details/115014511