无偿!全代码!增删改查+redis+token+mybatisplus 完整代码+讲解适合大一大二以及新手小白 Controller层讲解

这是一个简单的基础的且有不足的代码1.设置了一个管理员账号用于登录页面(用到简单的redis和token)2.增删改查(批量or单个删除)患者信息3.可更改患者身体状况4.excl表导出患者总体信息4.计算不同地区的患者情况并用前端以图标形式展示5.可以与服务器连接(若交作业可忽略)

注意:

1.原理不讲,会讲细节,是为了更好理解我的代码含义

2.我以Controller层->Service层(接口)->ServiceImpl层->Mapper层->Mapper.xml(不一定需要)的结构为主线来讲解

若有讲解的地方不对,请指正,感谢

简单梳理:代码书写顺序:

Controller层->Service层(接口)->ServiceImpl层->Mapper层->Mapper.xml(不一定需要)

接收数据->处理数据->写入数据->返回数据

(1).最主要,最多的代码是写在ServiceImpl

(2).若查询的要求简单,可以直接在Mapper层写,若查询的要求复杂,要写在Mapper.xml文件

(3).层与层之间,要用@Autoward注入,以及写上如下代码才可以连接起来。

patientService.save(patientDTO);

 

Controller层要注意的细节:


@RestController一定要写

@RequestMapping("/patient"),因为这里写了"/patient",所以后面每个小分支只要写"/patient"后面的内容即可,比如/login,就可以,这样最后呈现的效果就是:/patient/login

/**
 * 病患管理
 */
@RestController
@RequestMapping("/patient")
@Slf4j
public class PatientController {

    private PatientService patientService;
    public PatientController(PatientService patientService){
        this.patientService=patientService;
    }

以新增病患为例子,细讲分支

/**
     * 新增病患
     * @param patientDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增病患")
    public Result save(@RequestBody PatientDTO patientDTO){
        log.info("新增病患:{}",patientDTO);
        patientService.save(patientDTO);
        return Result.success();
    }


1.@PostMapping

/patient 用于和前端连接,要符合接口文档写。

Post      也要符合接口文档写

无偿!全代码!增删改查+redis+token+mybatisplus 完整代码+讲解适合大一大二以及新手小白 Controller层讲解_第1张图片

2.ApiOperation(“新增病患”)

是为了在后端用接口文档调试的时候,我们看的更清晰 

无偿!全代码!增删改查+redis+token+mybatisplus 完整代码+讲解适合大一大二以及新手小白 Controller层讲解_第2张图片

接口文档相关知识: 

接口文档?AYPI?全流程?小白新手教程-CSDN博客

3.Result.success

Result这里的数据是与前端交互的,这里其实是自定义的,有人自定义Result.ok(),而我用的是Result.success(),我写在了这里

无偿!全代码!增删改查+redis+token+mybatisplus 完整代码+讲解适合大一大二以及新手小白 Controller层讲解_第3张图片

package com.simplesin.result;

import lombok.Data;

import java.io.Serializable;

/**
 * 后端统一返回结果
 * @param 
 */
@Data
public class Result implements Serializable {

    private Integer code; //编码:1成功,0和其它数字为失败
    private String msg; //错误信息
    private T data; //数据

    public static  Result success() {
        Result result = new Result();
        result.code = 1;
        return result;
    }

    public static  Result success(T object) {
        Result result = new Result();
        result.data = object;
        result.code = 1;
        return result;
    }

    public static  Result error(String msg) {
        Result result = new Result();
        result.msg = msg;
        result.code = 0;
        return result;
    }

}

 4.@RequestBody,另外单独讲

5.

DTO 来的数据,所以Controller层一般是DTO的。

VO 返回的数据

还有一种叫实体参数

一般写在这里

无偿!全代码!增删改查+redis+token+mybatisplus 完整代码+讲解适合大一大二以及新手小白 Controller层讲解_第4张图片

这里贴出所有要用到的DTO,VO,实体参数

DTO:

package com.simplesin.dto;

import lombok.Data;

import java.io.Serializable;

@Data
public class EmployeeDTO implements Serializable {

    private Long id;

    private String username;

    private String name;

    private String phone;

    private String sex;

    private String idNumber;

}
package com.simplesin.dto;

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

import java.io.Serializable;

@Data
@ApiModel(description = "员工登录时传递的数据模型")
public class EmployeeLoginDTO implements Serializable {

    @ApiModelProperty("用户名")
    private String username;

    @ApiModelProperty("密码")
    private String password;

    @ApiModelProperty("验证码")
    private String code;

    @ApiModelProperty("手机号码")
    private String phone;

}
package com.simplesin.dto;

import lombok.Data;

import java.io.Serializable;

@Data
public class PatientDTO implements Serializable {
    private Long id;
    private String name;
    private String phone;
    private String sex;
    private String idNumber;
    private String address;
}
package com.simplesin.dto;

import lombok.Data;

import java.io.Serializable;

@Data
public class PatientPageQueryDTO implements Serializable {

    //病患姓名
    private String name;

    //页码
    private int page;

    //每页显示记录数
    private int pageSize;

}

实体参数

package com.simplesin.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDateTime;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Employee implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;

    private String username;

    private String name;

    private String password;

    private String phone;

    private String sex;

    private String idNumber;

    private Integer status;

    //@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    //@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    private Long createUser;

    private Long updateUser;

}
package com.simplesin.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDateTime;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Patient implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;

    private String name;

    private String phone;

    private String sex;

    private String idNumber;

    private Integer beInfected;

    private Integer recover;

    private Integer die;

    private String address;

    //@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    //@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    private Long createUser;

    private Long updateUser;

}

VO:

package com.simplesin.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(description = "员工登录返回的数据格式")
public class EmployeeLoginVO implements Serializable {

    @ApiModelProperty("主键值")
    private Long id;

    @ApiModelProperty("用户名")
    private String userName;

    @ApiModelProperty("姓名")
    private String name;

    @ApiModelProperty("jwt令牌")
    private String token;

}
package com.simplesin.vo;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 疫情数据概览
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class EpidemicVO {

    private Integer southRecover;//南方康复情况

    private Integer northRecover;//北方康复情况

    private Integer westRecover;//西方康复情况

    private Integer eastRecover;//东方康复情况

    private Integer southDie;//南方康复情况

    private Integer northDie;//北方康复情况

    private Integer westDie;//西方康复情况

    private Integer eastDie;//东方康复情况

    private Integer southBeInfected;//南方被感染情况

    private Integer northBeInfected;//北方被感染情况

    private Integer westBeInfected;//西方被感染情况

    private Integer eastBeInfected;//东方被感染情况

}
package com.simplesin.vo;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ReportVO implements Serializable {

    //地址,以逗号分隔,例如:南方,北方,西方,东方
    private String address;

    //康复总人数,以逗号分隔,例如:260,210,215
    private Integer recoverCount ;

    //死亡总人数,以逗号分隔,例如:260,210,215
    private Integer dieCount ;

    //被感染总人数,以逗号分隔,例如:260,210,215
    private Integer beInfectedCount ;
}


6.@Slf4j

作用是用来写log.info,方便我们在控制台查看有没有bug,类似于sout,输出语句,但为了更加规范项目书写格式,运用log.info

例如:

无偿!全代码!增删改查+redis+token+mybatisplus 完整代码+讲解适合大一大二以及新手小白 Controller层讲解_第5张图片

idea控制台输出结果:

7.正常来说,这里可以注入,也就是@Autoward,但是我当时应该是遇到了一些bug,所以改用了这种方式注入,也是可以的

private PatientService patientService;
    public PatientController(PatientService patientService){
        this.patientService=patientService;
    }

病患:

package com.simplesin.controller;

import com.simplesin.dto.PatientDTO;
import com.simplesin.dto.PatientPageQueryDTO;
import com.simplesin.entity.Patient;
import com.simplesin.result.PageResult;

import com.simplesin.result.Result;
import com.simplesin.service.PatientService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 病患管理
 */
@RestController
@RequestMapping("/patient")
@Slf4j
public class PatientController {

    private PatientService patientService;
    public PatientController(PatientService patientService){
        this.patientService=patientService;
    }

    /**
     * 新增病患
     * @param patientDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增病患")
    public Result save(@RequestBody PatientDTO patientDTO){
        log.info("新增病患:{}",patientDTO);
        patientService.save(patientDTO);
        return Result.success();
    }

    /**
     *
     * 病患分页查询
     * @param patientPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("病患分页查询")
    public Result page(PatientPageQueryDTO patientPageQueryDTO){
        log.info("病患分页查询,参数为:{}",patientPageQueryDTO);
        PageResult pageResult = patientService.pageQuery(patientPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     *
     * 被感染情况
     * @param beInfected
     * @param id
     * @return
     */
    @PostMapping("/beInfected/{beInfected}")
    @ApiOperation("被感染情况")
    public Result beInfectedOrnotBeInfected(@PathVariable Integer beInfected,Long id){
        log.info("被感染情况:{},{}",beInfected,id);
        patientService.beInfected(beInfected,id);
        return Result.success();
    }

    /**
     * 康复情况
     * @param recover
     * @param id
     * @return
     */
    @PostMapping("/recover/{recover}")
    @ApiOperation("康复情况")
    public Result recoverOrnotRecover(@PathVariable Integer recover,Long id){
        log.info("康复情况:{},{}",recover,id);
        patientService.recover(recover,id);
        return Result.success();
    }

    /**
     * 死亡情况
     * @param die
     * @param id
     * @return
     */
    @PostMapping("/die/{die}")
    @ApiOperation("死亡情况")
    public Result dieOrnotDie(@PathVariable Integer die,Long id){
        log.info("死亡情况:{},{}",die,id);
        patientService.die(die,id);
        return Result.success();
    }

    /**
     * 根据id查询病患信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询病患信息")
    public  com.simplesin.result.Result> getById1
    (@PathVariable Long id){
        com.simplesin.result.Result patient = patientService.getById1(id);
        return com.simplesin.result.Result.success(patient);
    }

    /**
     * 编辑病患信息
     * @param patientDTO
     * @return
     */
    @PutMapping
    @ApiOperation("编辑病患信息")
    public Result update(@RequestBody PatientDTO patientDTO){
        log.info("编辑病患信息:{}",patientDTO);
        patientService.update(patientDTO);
        return Result.success();
    }

    /**
     * 病患信息批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation("病患信息批量删除")
    public  Result delete(@RequestParam List ids){
        log.info("菜品批量删除:{}",ids);
        patientService.deleteBatch(ids);
        return Result.success();
    }

}

管理员

package com.simplesin.controller;

import com.simplesin.result.Result;
import com.simplesin.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
 
    
    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result logout() {
        return Result.success();
    }

    /**
     * 发送手机验证码
     * @param phone
     * @param session
     * @return
     */
    @PostMapping("/code")
    public  Result sendCode(@RequestParam("phone") String phone , HttpSession session){
        //发送短信验证码并保存验证码
        log.info("phonecode"+phone);
        employeeService.sendCode(phone,session);
        return Result.success();
    }


    @PostMapping("/login")
    public Result login(String phone,String Code){
        //实现登录功能
        log.info("登录中...");
        log.info("phone"+phone);
        log.info("Code"+Code);
        log.info("aaaa");
        Result login = employeeService.login(phone, Code);
        log.info("bbbb");
        log.info("login"+login);
        return login;

    }
}

 


针对中途出现的bug,可以参考我以往的bug处理专栏,都是在写这个项目的时候遇到的bug

还是决定都无偿啦!有兴趣的可以往下看噢!

你可能感兴趣的:(java,maven,mysql,后端,sql)