Spring integrated PageHelper

One, import dependencies

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

Second, the configuration in applicationContext.xml
mainly refers to the file 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>

Three, create a new 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>

Four, use in the 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);
    }

Five, which has its own definition of return class
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;
	}

}

Well, that's it! ! !

Guess you like

Origin blog.csdn.net/BOOM0BOOM/article/details/115014511