本章导学:
- Result类设计
- enum设计
- controller层设计
- service与mapper层设计
在我们平时的开发中,后端响应回给前端的请求一般都需要规范成统一的格式,比如下图的这种格式,当然,也可以按照实际的需求进行修改。
- data:响应回来的数据
- msg:响应返回的消息
- code:接口执行后返回的状态码(用于判断访问成功或失败)
一、enum枚举设计
先看看不使用枚举我们怎么处理code
可以看的出,是比较麻烦和重复的,而且也只定义了code没有定义msg
接下来我们定义一个枚举类来统一处理code和msg
package com.brrbaii.reggie.common;
import lombok.Getter;
@Getter
public enum CommontEnum {
LOGIN_SUCCESS(20001,"登陆成功!"),
LOGIN_ERROR(20002,"登录失败,请检查用户名或密码!");
private Integer code;
private String msg;
CommontEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
}
我们把code和msg封装在一起
二、定义Result类
package com.brrbaii.reggie.common;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Data
/**
* 通用返回类
* @author brrbaii
* @date 2022/9/24
*/
public class Result<T> {
//响应数据
private T data;
//响应消息
private String msg;
//响应代码
private Integer code;
/**
* 请求成功
* @param msg
* @param data
* @return
*/
public static Result success(CommontEnum commontEnum,Object data){
Result result = new Result();
result.setMsg(commontEnum.getMsg());
result.setData(data);
result.setCode(commontEnum.getCode());
return result;
}
/**
* 请求失败
* @param msg
* @return
*/
public static Result error(CommontEnum commontEnum){
Result result = new Result();
result.setMsg(commontEnum.getMsg());
result.setCode(commontEnum.getCode());
return result;
}
}
在Result类里,我们定义了两个静态方法success和error,用于处理成功和失败的情况
在success里,我们传输先前定义的枚举对象commontEnum,和data
commontEnum里有我们需要的code和msg信息,data用来接收mapper处理后返回的数据
三、编写Controller层
package com.brrbaii.reggie.contorller;
import com.brrbaii.reggie.common.CommontEnum;
import com.brrbaii.reggie.common.Result;
import com.brrbaii.reggie.entity.Employee;
import com.brrbaii.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
/**
* 员工控制类
* @author brrbaii
* @date 2022/9/24
*/
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
/**
* 登录请求处理
* @param employee
* @return
*/
@PostMapping("/login")
public Result login(HttpServletRequest req, @RequestBody Employee employee){
Employee result = employeeService.login(req, employee);
if(result == null){
return Result.error(CommontEnum.LOGIN_ERROR);
}
else{
//登录成功后把ID设置到Session里方便后续使用
req.getSession().setAttribute("EmployeeId",result.getId());
return Result.success(CommontEnum.LOGIN_SUCCESS,result);
}
}
}
在controller层,我们只需要判断service返回的数据传枚举类里对应的属性就好了
用postMan测试一下,看看返回的数据格式是什么
这样前端访问我们接口返回的数据只用使用如下格式就行了
axios.post("/接口","数据模型").then((res)=>{
res.data.data //获取data数据
res.data.msg //获取msg消息
res.data.code //获取状态码code})
四、service层与Mapper
Service接口:
package com.brrbaii.reggie.service;
import com.brrbaii.reggie.entity.Employee;
import javax.servlet.http.HttpServletRequest;
/**
* 员工业务接口
* @author brrbaii
* @date 2022/9/24
*/
public interface EmployeeService {
Employee login(HttpServletRequest req, Employee employee);
}
Service实现类
package com.brrbaii.reggie.service.Impl;
import com.brrbaii.reggie.entity.Employee;
import com.brrbaii.reggie.mapper.EmployeeMapper;
import com.brrbaii.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import javax.servlet.http.HttpServletRequest;
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeMapper employeeMapper;
/**
* 登录模块
* @param req
* @param employee
* @return
*/
@Override
public Employee login(HttpServletRequest req, Employee employee) {
//1、获取密码,并进行MD5加密
String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
//2、根据前台传过来的用户名查询数据库是否存在当前用户
Employee resultEmp = employeeMapper.selectByName(employee);
//3、密码正确则返回
if(resultEmp.getPassword().equals(password)){
return resultEmp;
}
return null;
}
}
mapper:
Mapper接口:
package com.brrbaii.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.brrbaii.reggie.entity.Employee;
import org.springframework.stereotype.Repository;
/**
* 员工数据层
* @author brrbaii
* @date 2022/9/24
*/
@Repository
public interface EmployeeMapper extends BaseMapper<Employee> {
Employee selectByName(Employee employee);
}
MapperXml:
<?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.brrbaii.reggie.mapper.EmployeeMapper">
<select id="selectByName" resultType="employee">
select * from employee where username = #{username}
</select>
</mapper>