springboot之mybatis-plus实现条件查询(restful风格,纯后端代码,使用swagger测试)

你可能需要的准备工作:

springboot之mybatis-plus配置与简单增删改查:https://blog.csdn.net/VEclipses/article/details/106385595

springboot之统一返回json数据结果(新手必备):https://blog.csdn.net/VEclipses/article/details/106383459

springboot之swagger2简单配置使用:https://blog.csdn.net/VEclipses/article/details/106356372

 

准备工作:

文章不展示mybatis-plus配置等配置,

数据库表文件:

springboot之mybatis-plus实现条件查询(restful风格,纯后端代码,使用swagger测试)_第1张图片

数据库表数据:

springboot之mybatis-plus实现条件查询(restful风格,纯后端代码,使用swagger测试)_第2张图片

实验

       业务层代码可以使用mybatis-plus提供的代码生成器,不过我不知道为什么实现用不了。。。。业务层、控制器层和mapper映射层的所有需要的类都要自己动手新建。。。。确实费时间,业务层和mapper层的文件需要继承mybatis-plus提供对应的类。

1.mapper层

可以添加注解,不过必须要继承BaseMapper

package com.example.homeManager.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.homeManager.entity.User;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper extends BaseMapper {

}

2.service层

接口类:

需要继承IService

package com.example.homeManager.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.homeManager.dto.UserSearchDto;
import com.example.homeManager.entity.User;

public interface UserService extends IService {
    void pageQuery(Page pageParam, UserSearchDto userSearchDto);
}

接口实现类:

添加注解@Service,并且继承ServiceImpl类,实现接口层方法。

接口方法接收两个参数,一个参数是Page对象,里面的数据包含分页的页面数据的信息,另一个是前端查询的条件对象

package com.example.homeManager.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.homeManager.dto.UserSearchDto;
import com.example.homeManager.entity.User;
import com.example.homeManager.mapper.UserMapper;
import com.example.homeManager.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl extends ServiceImpl implements UserService {

    @Override
    public void pageQuery(Page pageParam, UserSearchDto userSearchDto) {
        QueryWrapper userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.orderByDesc("ruzhu_time");

        if (userSearchDto == null){
            baseMapper.selectPage(pageParam,userQueryWrapper);
            return;
        }

        if (!StringUtils.isEmpty(userSearchDto.getUserName())){
            userQueryWrapper.like("name",userSearchDto.getUserName());
        }
        if (!StringUtils.isEmpty(userSearchDto.getUserClass())){
            userQueryWrapper.eq("user_class",userSearchDto.getUserClass());
        }
        if (!StringUtils.isEmpty(userSearchDto.getHouseId())){
            userQueryWrapper.eq("house_id",userSearchDto.getHouseId());
        }
        if (!StringUtils.isEmpty(userSearchDto.getBegin())){
            userQueryWrapper.ge("ruzhu_time",userSearchDto.getBegin());
        }
        if (!StringUtils.isEmpty(userSearchDto.getEnd())){
            userQueryWrapper.le("ruzhu_time",userSearchDto.getEnd());
        }
         baseMapper.selectPage(pageParam,userQueryWrapper);
    }

}

 

3.DTO查询的条件对象

前端可选择的查询条件有5个,分别是用户姓名、用户类型、房间号、开始时间和查询结束时间

package com.example.homeManager.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
@ApiModel(value = "查询用户条件对象",description = "查询用户的条件")
public class UserSearchDto implements Serializable {
    private static final long serialVersionUID = 5286539299409998481L;

    @ApiModelProperty(value = "用户姓名 模糊查询")
    private String userName;
    @ApiModelProperty(value = "用户类型 1超级管理员 2管理员 3现任租客 4前任租客")
    private String userClass;
    @ApiModelProperty(value = "房间号")
    private String houseId;
    @ApiModelProperty(value = "查询开始时间",example = "2020-01-10 10:10:10")
    private String begin;
    @ApiModelProperty(value = "查询结束时间",example = "2020-01-10 10:10:10")
    private String end;
}

4.实体类

package com.example.homeManager.entity;

import com.baomidou.mybatisplus.annotation.*;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;

@Data
@TableName(value = "sys_user")
@ApiModel(value = "User对象",description = "用户")
public class User implements Serializable {
    private static final long serialVersionUID = -2324485611576806000L;

    //  数据库的id自增方式:@TableId(value = "id",type = IdType.AUTO)
    @TableId(value = "id",type = IdType.ID_WORKER_STR)
    @ApiModelProperty(value = "用户ID")
    private String id;

    @TableField(value = "accound")
    @ApiModelProperty(value = "用户账号")
    private String acc;

    @ApiModelProperty(value = "用户密码")
    private String password;

    @ApiModelProperty(value = "用户姓名")
    private String name;

    @TableField(value = "user_class")
    @ApiModelProperty(value = "用户类型")
    private String userClass;

    @TableField(value = "house_id")
    @ApiModelProperty(value = "房间号")
    private String houseId;

    @TableField(value = "ruzhu_time")
    @ApiModelProperty(value = "入住时间")
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private Date ruzhuTime;

    @TableField(value = "out_time")
    @ApiModelProperty(value = "搬出时间",example = "2020-01-10 10:10:10")
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private Date outTime;

    @ApiModelProperty(value = "是否禁用",example = "2020-01-10 10:10:10")
    private String ban;

    //fill = FieldFill.INSERT自动填充数据库表字段内容,结合MyMetaObjectHandler类
    @TableField(value = "create_time",fill = FieldFill.INSERT)
    @ApiModelProperty(value = "创建时间",example = "2020-01-10 10:10:10")
    private Date createTime;
    @TableField(value = "update_time",fill = FieldFill.INSERT_UPDATE)
    @ApiModelProperty(value = "修改时间",example = "2020-01-10 10:10:10")
    private Date updateTime;

    @Version
    @ApiModelProperty(value = "乐观锁")
    private Integer version;

    @TableField(value = "is_deleted")
    @ApiModelProperty(value = "是否删除")
    @TableLogic
    private boolean deleted;

}

5.控制器层

接口都是API restful风格。

这里的返回数据是json格式的,建议查看我的另一篇博客https://blog.csdn.net/VEclipses/article/details/106383459了解。

package com.example.homeManager.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.homeManager.common.vo.ResultJSON;
import com.example.homeManager.dto.UserSearchDto;
import com.example.homeManager.entity.User;
import com.example.homeManager.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/myhouse/admin/sys")
@CrossOrigin    //跨域
@Api(description = "用户列表")
public class LoginController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "所有用户列表")
    @GetMapping("/users")
    public ResultJSON users(){
        List userList = userService.list(null);
        return ResultJSON.ok()
                        .data("items",userList)
                        .message("查询用户列表");
    }

    @ApiOperation(value = "根据id删除用户")
    @DeleteMapping("/users/{id}")
    public ResultJSON delete(
            @ApiParam(name = "id",value = "用户id",required = true)
            @PathVariable String id){
        userService.removeById(id);
        return ResultJSON.ok();
    }

    @ApiOperation(value = "用户列表查询")
    @GetMapping("/users/{page}/{limit}")
    public ResultJSON query(
            @ApiParam(name = "page",value = "查询页码",required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit",value = "页数",required = true)
            @PathVariable Long limit,

            @ApiParam(name = "userSearchDto",value = "用户查询实体",required = false)
                UserSearchDto userSearchDto){


        Page userParam = new Page<>(page, limit);
        userService.pageQuery(userParam,userSearchDto);
        List records = userParam.getRecords();
        return ResultJSON.ok().data("total",userParam.getTotal()).data("rows",records);
    }

}

6.swagger测试

1.启动项目,在url中输入http://localhost:8082/swagger-ui.html访问swagger

2.找到要测试的分页查找接口

springboot之mybatis-plus实现条件查询(restful风格,纯后端代码,使用swagger测试)_第3张图片

查看:

springboot之mybatis-plus实现条件查询(restful风格,纯后端代码,使用swagger测试)_第4张图片

先简单分页查数据:

springboot之mybatis-plus实现条件查询(restful风格,纯后端代码,使用swagger测试)_第5张图片

 

结果:

因为我的查询条件是按“ruzhu_time”时间字段并降序的,因此这条数据在第一位

springboot之mybatis-plus实现条件查询(restful风格,纯后端代码,使用swagger测试)_第6张图片

设置入住时间的查询条件从5月24日开始:

springboot之mybatis-plus实现条件查询(restful风格,纯后端代码,使用swagger测试)_第7张图片

结果为空:

springboot之mybatis-plus实现条件查询(restful风格,纯后端代码,使用swagger测试)_第8张图片

设置入住的开始和结束时间:

springboot之mybatis-plus实现条件查询(restful风格,纯后端代码,使用swagger测试)_第9张图片

结果:有3条数据

springboot之mybatis-plus实现条件查询(restful风格,纯后端代码,使用swagger测试)_第10张图片

 

查看数据库表数据:

有4条数据符合时间条件的数据,但是27号的数据的is_delete字段是“1”,也就是已经删除的了,所以查询没有问题。

查看控制台插件的打印的日志数据(没有的话可以在properties配置文件配置可以查看我mybatis-plus的配置博客):

where条件有 is_delete字段为0。

springboot之mybatis-plus实现条件查询(restful风格,纯后端代码,使用swagger测试)_第11张图片

 

this off

你可能感兴趣的:(springboot,java)