Spring Boot——一种包含分页和排序参数的接收方法DEMO

Maven

主要Maven依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--MyBatis-Plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <!--Swagger 2-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

源代码

package cn.edu.zstu.shihua.xihu.controller.log;


import cn.edu.zstu.shihua.xihu.model.Log;
import cn.edu.zstu.shihua.xihu.model.User;
import cn.edu.zstu.shihua.xihu.service.ILogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ShenTuZhiGang
 * @since 2020-08-02
 */
@RestController
@RequestMapping("/log")
@Api(tags = "日志功能接口")
public class LogController {
    @Autowired
    ILogService iLogService;
    @RequestMapping(value = "/list",method = {RequestMethod.GET})
    public Object pageLog(@ApiParam(name = "page",value = "页数")
                              @RequestParam(defaultValue = "1") Integer page,
                          @ApiParam(name = "size",value = "记录数")
                              @RequestParam(defaultValue = "10") Integer size,
                          @ApiParam(name = "keyword",value = "关键字")
                              @RequestParam(required = false) String keyword,
                          @ApiParam(name = "status",value = "状态")
                              @RequestParam(required = false) String status,
                          @ApiParam(name = "date",value = "时间范围")
                              @RequestParam(required = false) Timestamp[] date,
                          @ApiParam(name = "sort",value = "排序")
                              @RequestParam(required = false) String sort){

        QueryWrapper<Log> logQueryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(keyword)){
            logQueryWrapper.like("operator",keyword);
        }
        if(!StringUtils.isEmpty(status)){
            logQueryWrapper.eq("status",status);
        }
        if(!StringUtils.isEmpty(sort)){
            try{
                Pattern pattern = Pattern.compile("([\\+|-][a-zA-Z]+)");
                Matcher matcher = pattern.matcher(sort);
                while (matcher.find()) {
                    String str = matcher.group(1);
                    if(str.charAt(0)=='+'){
                        logQueryWrapper.orderByAsc(str.substring(1));
                    }else{
                        logQueryWrapper.orderByDesc(str.substring(1));
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if(date!=null && date.length==2){
            logQueryWrapper.between("time",date[0],date[1]);
        }

        return iLogService.page(new Page<>(page,size),logQueryWrapper);
    }
}

参考项目

Vue后台管理项目: Vue Element Admin

参考文章

正则表达式之Matcher类中group方法

官方文档:Mybatis Plus 分页插件

MyBatis Plus——分页插件

©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页