springboot+vue建立_2(分頁)

201812發表於2024-05-26

七、分頁

7.1

    //分頁
    @PostMapping("/listPage")
//    public List<Login00> listhu(@RequestBody HashMap map){
    public List<Login00> listhu(@RequestBody QueryPageParam queryPageParam){
        //hashmap
//        System.out.println(map);//會獲取到postman裡面的傳送資料
//        System.out.println("pagesize="+map.get("pageSize"));
        //自定義的方法
        System.out.println(queryPageParam);
        System.out.println("pagesize="+queryPageParam.getPageSize());
        return null;
    }

測試:
前端傳入引數如下:

對於使用HashMap:

對於自定義的:

對於map,前端不管傳入多少,都可以接收到,但是對於自己定義的,可以透過這樣子處理:

獲取param裡面的值:

可以直接用HashMap也可以封裝,封裝的話比較統一吧。

7.2新增分頁攔截器

官網程式碼:https://baomidou.com/pages/97710a/#配置方法

    @PostMapping("/listPage")
    public List<Login00> listhu(@RequestBody QueryPageParam queryPageParam){

        //分頁攔截器
//        Page<Login00> page = new Page<>(1,2);//當前頁1,每頁2條
        //也可以這樣子對當前頁和每頁條數進行設定
        Page<Login00> page = new Page<>();
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());

        //進行查詢
        HashMap param = queryPageParam.getParam();
        String uname = (String) param.get("uname");
        LambdaQueryWrapper<Login00> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Login00::getUname,uname);
        IPage result=iLogin00Service.page(page,lambdaQueryWrapper);
        System.out.println("total="+result.getTotal());
        return result.getRecords();
    }


控制檯輸出total=3,因為資料庫裡面是有3條符合的資料。

7.3 編寫分頁mapper

contorller:

@PostMapping("/listPageC")
//    public List<Login00> listhu(@RequestBody HashMap map){
    public List<Login00> listPageC(@RequestBody QueryPageParam queryPageParam){

        HashMap param = queryPageParam.getParam();
        String uname = (String) param.get("uname");

        Page<Login00> page = new Page<>();
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());

        //進行查詢
//        LambdaQueryWrapper<Login00> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.like(Login00::getUname,uname);
        IPage result=iLogin00Service.pageC(page);
        System.out.println("total="+result.getTotal());
        return result.getRecords();
    }

service:

IPage pageC(IPage<Login00> page);

serviceimpl

@Override
    public IPage pageC(IPage<Login00> page) {
        return login00Mapper.pageC(page);
    }

mapper

IPage pageC(IPage<Login00> page);

mapper配置檔案xml

<select id="pageC" resultType="com.example.entity.Login00">
        select * from login00
</select>

執行結果:
雖然是新增了uname的限制,但是還是查詢了全部,因為在xml檔案中寫的是select * from login00

要實現新增uname限制,可以直接在xml的查詢語句裡面新增限制,也可以使用mybatis-plus提供的Wrapper(官網查詢)
controller

LambdaQueryWrapper<Login00> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.like(Login00::getUname,uname);
IPage result2=iLogin00Service.pageCC(page,lambdaQueryWrapper);

在mapper中(注意是@Pama..這樣子的格式):

IPage pageCC(Page<Login00> page,@Param(Constants.WRAPPER) Wrapper wrapper);

在mapper.xml(注意是${ew.customSqlSegment}這樣子的格式)中

<select id="pageCC" resultType="com.example.entity.Login00">
        select * from login00 ${ew.customSqlSegment}
</select>

後臺total是1
image

後臺total是3
image

分頁涉及的程式碼:

comment層分頁攔截器程式碼:

package com.example.comment;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//分頁攔截器
@Configuration
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//如果配置多個外掛,切記分頁最後新增
        //interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 如果有多資料來源可以不配具體型別 否則都建議配上具體的DbType
        return interceptor;
    }
}

自定義的分頁引數:

package com.example.comment;

import lombok.Data;

import java.util.HashMap;

//分頁引數
@Data
public class QueryPageParam {
    //預設值
    private static int PAGE_SIZE=20;
    private static int PAGE_NUM=1;

    private int pageSize=PAGE_SIZE;
    private int pageNum=PAGE_NUM;

    private HashMap param;
}

controller

package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.comment.QueryPageParam;
import com.example.entity.Login00;
import com.example.service.ILogin00Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/login00")
public class Login00Controller {

    @Autowired
    private ILogin00Service iLogin00Service;


    //分頁
    @PostMapping("/listPage")
//    public List<Login00> listhu(@RequestBody HashMap map){
    public List<Login00> listhu(@RequestBody QueryPageParam queryPageParam){
        //hashmap
//        System.out.println(map);//會獲取到postman裡面的傳送資料
//        System.out.println("pagesize="+map.get("pageSize"));


        //自定義的方法
//        System.out.println(queryPageParam);
//        System.out.println("pagesize="+queryPageParam.getPageSize());
//
//        HashMap param = queryPageParam.getParam();
//        String uname = (String) param.get("uname");
//        String utype = (String) param.get("utype");
//        System.out.println("uname: "+uname+" utype: "+utype);

        //分頁攔截器
//        Page<Login00> page = new Page<>(1,2);//當前頁1,每頁2條
        //也可以這樣子對當前頁和每頁條數進行設定
        Page<Login00> page = new Page<>();
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());

        //進行查詢
        HashMap param = queryPageParam.getParam();
        String uname = (String) param.get("uname");
        LambdaQueryWrapper<Login00> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Login00::getUname,uname);
        IPage result=iLogin00Service.page(page,lambdaQueryWrapper);
        System.out.println("total="+result.getTotal());
        return result.getRecords();
    }

    @PostMapping("/listPageC")
    public List<Login00> listPageC(@RequestBody QueryPageParam queryPageParam){

        HashMap param = queryPageParam.getParam();
        String uname = (String) param.get("uname");

        Page<Login00> page = new Page<>();
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());

        //進行查詢
        IPage result=iLogin00Service.pageC(page);
        System.out.println("total="+result.getTotal());

        return result.getRecords();
    }

    @PostMapping("/listPageCC")
    public List<Login00> listPageCC(@RequestBody QueryPageParam queryPageParam){

        HashMap param = queryPageParam.getParam();
        String uname = (String) param.get("uname");

        Page<Login00> page = new Page<>();
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());

        //進行查詢
        //使用Wrapper自定義SQL 新增限制
        //注意,mybatis-plus版本要>=3.0.7
        LambdaQueryWrapper<Login00> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Login00::getUname,uname);
        IPage result2=iLogin00Service.pageCC(page,lambdaQueryWrapper);
        System.out.println("total2="+result2.getTotal());
        return result2.getRecords();
    }


}

service

public interface ILogin00Service extends IService<Login00> {

    IPage pageC(Page<Login00> page);

    IPage pageCC(Page<Login00> page, Wrapper lambdaQueryWrapper);
}

serviceimpl

@Service
public class Login00ServiceImpl extends ServiceImpl<Login00Mapper, Login00> implements ILogin00Service {

    @Autowired
    private Login00Mapper login00Mapper;

    @Override
    public IPage pageC(Page<Login00> page) {
        return login00Mapper.pageC(page);
    }

    @Override
    public IPage pageCC(Page<Login00> page, Wrapper lambdaQueryWrapper) {
        return login00Mapper.pageCC(page,lambdaQueryWrapper);
    }
}

mapper

@Mapper
public interface Login00Mapper extends BaseMapper<Login00> {

    IPage pageC(Page<Login00> page);

    IPage pageCC(Page<Login00> page,@Param(Constants.WRAPPER) Wrapper wrapper);
}

mapper的xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.Login00Mapper">
    <select id="pageC" resultType="com.example.entity.Login00">
        select * from login00
    </select>
    <select id="pageCC" resultType="com.example.entity.Login00">
        select * from login00 ${ew.customSqlSegment}
    </select>

</mapper>

相關文章