springboot+vue创建_2(分页)

七、分页

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>

posted on 2024-05-26 22:06  201812  阅读(63)  评论(0编辑  收藏  举报