NIIT实训(春季 git + Maven + Springboot + MyBatis + Vue)第四次

DAY12

封装对象,返回状态响应码,状态异常返回,分页查询
在这里插入图片描述

ExceptionCodeEnum

package cn.niit.hospital.common.enums;

/**
 * @Description: 响应状态码枚举类
 * @Author: yang_yong
 * @CreateTime: 2023/3/21 9:24
 * @param: null
 * @return: null
 **/
public enum ExceptionCodeEnum {

    /*
    200	请求成功
    400	请求参数错误	请求参数错误
    405	未登录或登录失效
    500	请求失败	服务器内部错误
    9001	验证码错误或已过期!	登录时验证码验证失败
    9002	账号或密码错误!	登录时验证账号密码失败
*/

    SUCCESS("200", "请求成功"),
    PARAMETER_VALID_ERROR("9001", "参数校验错误!");

    private String resCode;
    private String resDesc;

    public String getResCode() {
        return resCode;
    }

    public String getResDesc() {
        return resDesc;
    }


    ExceptionCodeEnum(String resCode, String resDesc) {
        this.resCode = resCode;
        this.resDesc = resDesc;
    }

}

Controller

package cn.niit.hospital.controller;

import cn.niit.hospital.common.enums.ExceptionCodeEnum;
import cn.niit.hospital.entity.domain.User;
import cn.niit.hospital.entity.vo.BaseResponse;
import cn.niit.hospital.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 用户控制器
 * @Author: tang_meng
 * @CreateTime: 2023/3/16 16:23
 */
@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * @Description: 接口一, 根据某个id查询用户
     * @Author: tang_meng
     * @CreateTime: 2023/3/20 8:39
     * @param: id
     * @return: cn.niit.hospital.entity.domain.User
     **/
    @RequestMapping("/user/{id}")
    public User getById(@PathVariable("id") Long id){
        User user = userService.getUserById(id);
        return user;
    }

    @GetMapping("/user/{id}")
    public BaseResponse getByIdForBaseResponse(@PathVariable("id") Long id){
        if (id <0 ){
            return BaseResponse.error(ExceptionCodeEnum.PARAMETER_VALID_ERROR);
        }
        else {
            User user = userService.getUserById(id);
        return BaseResponse.success(user);}
    }

    /**
     * @Description: 接口二: 查询用户列表接口
     * @Author: tang_meng
     * @CreateTime: 2023/3/20 8:42
     * @param: 封装着查询(多个)条件的用户对象
     * @return: java.util.List<cn.niit.hospital.entity.domain.User>
     **/
    @GetMapping("/user")
    public BaseResponse list(User user){
        log.info("接受得到的参数 {}  "+user);
        /**
        开启分页查询
         PageHelper.startPage(2,3) //参数一:第几页 参数二:每页多少条
         不能写死
         **/
    //    PageHelper.startPage(user.getPage(), user.getRows());
        List<User> users = userService.getListByUser(user);
        PageInfo<User> pageInfo = new PageInfo<>(users);//获取分页对象
        return BaseResponse.success(users, pageInfo); //返回带分页的结果
    }

    /**
     * @Description: 接口三: 新增接口
     * @Author: tang_meng
     * @CreateTime: 2023/3/20 8:44
     * @return: java.lang.String
     **/
    @PostMapping("/user")
   // public Integer add(@RequestBody User user)
      public BaseResponse add(@RequestBody User user){
        log.info("接收到的参数: {} "+user);
        int rows = userService.addUser(user);
        if (rows>0){
            return BaseResponse.success();
        }
        return BaseResponse.error("90002","添加失败");
    }



    /**
     * @Description: 接口四: 更新接口
     * @Author: tang_meng
     * @CreateTime: 2023/3/20 8:45
     * @param: user
     * @return: java.lang.String
     **/
    @PutMapping("/user")
    public String update(User user){
        log.info("接收到的参数: {} "+user);
        int rows = userService.updateUser(user);
        if(rows == 1)
            return "更新成功";
        else return "更新失败!";
    }

   // @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
    @DeleteMapping("/user/{id}") //和上面等价,推荐这个
    public String delete(@PathVariable("id") Long id){
        int rows = userService.deleteUser(id);
        return "ol";
    }


}

BaseEntity

package cn.niit.hospital.entity.domain;

import lombok.Data;

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

/**
 * @Description: 基类
 * @Author: tang_meng
 * @CreateTime: 2023/3/21 10:37
 */
@Data
public class BaseEntity implements Serializable {

    //公共字段
    private Integer delFlag;
    private Long createUserId;
    private String createUserName;
    private Date gmtcreate;
    private Long updateUserId;
    private String updateUserName;
    private Date gmtupdate;

    private Integer page = 1; //第几页, 默认第一页
    private Integer rows = 10; //每页多少条,默认10条

}

User

package cn.niit.hospital.entity.domain;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.ibatis.type.Alias;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Description: 用户实体
 * @Author: tang_meng
 * @CreateTime: 2023/3/16 16:22
 */
//加上注解不用写getter和setter方法了
@Getter
@Setter
@ToString
@Alias("User")
@Component
public class User extends BaseEntity{
    private Long id;
    private String phone;
    private String idCard;
    private String userName;
    private String password;
    private Date birthday;
    private Integer sex;
    private Integer status;



}

BaseResponse

package cn.niit.hospital.entity.vo;

import cn.niit.hospital.common.enums.ExceptionCodeEnum;
import cn.niit.hospital.entity.vo.PageVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@NoArgsConstructor
public class BaseResponse<T> {

    private String resCode = "200";

    private String resDesc = "请求成功";

    private PageVO pageParam;

    private T data;

    public BaseResponse(T data){
        this.data = data;
    }

    public BaseResponse(T data, PageVO page){
        this.data = data;
        this.pageParam = page;
    }

    public static BaseResponse success(Object data, PageInfo pageInfo){
        PageVO page = new PageVO();
        page.setCurrent(pageInfo.getPageNum());
        page.setSize(pageInfo.getSize());
        page.setPages(pageInfo.getPages());
        page.setTotal(Integer.valueOf(pageInfo.getTotal() + ""));
        return new BaseResponse(data, page);
    }

    public BaseResponse(String resDesc) {
        this.resDesc = resDesc;
    }

    public BaseResponse(String resCode, String resDesc) {
        this.resCode = resCode;
        this.resDesc = resDesc;
    }

    /**
     * @Description: 成功,默认没有其他数据,只是状态。
     * @Author: yang_yong
     * @CreateTime: 2023/3/18 23:19
     * @return: cn.niit.smbms.model.response.BaseResponse
     **/
    public static BaseResponse success(){
        return new BaseResponse("请求成功!");
    }

    /**
     * @Description: 成功:包含响应数据
     * @Author: yang_yong
     * @CreateTime: 2023/3/18 23:20
     * @param: data
     * @return: cn.niit.smbms.model.response.BaseResponse
     **/
    public static BaseResponse success(Object data){
        return new BaseResponse(data);
    }

    /**
     * @Description: 返回错误信息
     * @Author: yang_yong
     * @CreateTime: 2023/3/18 23:22
     * @param: resCode
     * @param: resDesc
     * @return: cn.niit.smbms.model.response.BaseResponse
     **/
    public static BaseResponse error(String resCode, String resDesc){
        return new BaseResponse(resCode, resDesc);
    }

    /**
     * @Description: 返回错误信息
     * @Author: yang_yong
     * @CreateTime: 2023/3/18 18:05
     * @param: code
     * @return: cn.niit.hospital.entity.vo.response.BaseResponse
     **/
    public static BaseResponse error(ExceptionCodeEnum code) {
        return new BaseResponse(code.getResCode(), code.getResDesc());
    }
}

PageVo

package cn.niit.hospital.entity.vo;

import lombok.*;

@Setter
@Getter
@EqualsAndHashCode
@ToString
@NoArgsConstructor
public class PageVO {
    private int total;  //所有的记录数
    private int size;   //每页多少条记录
    private int pages;  //总页数
    private int current;    //当前页
}

依赖

<properties>
        <java.version>1.8</java.version>
        <pagehelper.starter.version>1.4.5</pagehelper.starter.version>
    </properties>
<!-- 分页插件 pagehelper 依赖 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>${pagehelper.starter.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis.spring.boot</groupId>
                    <artifactId>mybatis-spring-boot-starter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

接口实现方法:

    @Override
    public List<User> getListByUser(User user) {
        PageHelper.startPage(user.getPage(), user.getRows());
        return userMapper.selectListByUser(user);
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值