1. 加载业务层Service的接口
    2. 编写API接口,使用@PostMapping,@GetMapping,@PutMapping,@DeleteMapping ```java package com.tj.reggie.controller;

    import com.alibaba.druid.sql.visitor.functions.Hex; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.tj.reggie.controller.utils.R; import com.tj.reggie.entity.Users; import com.tj.reggie.service.UsersService; import com.tj.reggie.utils.Sha256Util; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.DigestUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;

    import javax.servlet.http.HttpServletRequest; import java.security.MessageDigest;

    @Slf4j //日志 @RestController //RESTful风格写法 @RequestMapping(“/users”) //定义API接口 public class UsersController {

    1. @Autowired //自动加载业务层接口
    2. private UsersService usersService;
    3. /**
    4. * 用户登录
    5. *
    6. * @param request
    7. * @param users
    8. * @return
    9. */
    10. @PostMapping("/login")
    11. public R<Users> login(HttpServletRequest request, @RequestBody Users users) {
    12. //1.将页面提交的密码password进行md5加密处理
    13. String password = users.getPassword();
    14. password = Sha256Util.sha256(password);//sha256加密

    // password = DigestUtils.md5DigestAsHex(password.getBytes());//md5加密

    1. //2.根据页面提交的用户名userName查询数据库
    2. LambdaQueryWrapper<Users> lqw = new LambdaQueryWrapper<>(); //条件对象
    3. lqw.eq(Users::getUserId, users.getUserId()); //判断数据库里是否有相同的用户名
    4. Users res = usersService.getOne(lqw); //查询一条记录
    5. //3.如果没有查询到则返回登录失败结果
    6. if (res == null) {
    7. return R.error("用户名不存在");
    8. }
    9. //4.密码比对,如果不一致则返回登录失败结构
    10. if (!res.getPassword().equals(password)) {
    11. return R.error("密码不正确");
    12. }
    13. //5.查看员工状态,如果为已禁用,则返回用户禁用信息
    14. if (res.getIsuse() == 1) {
    15. return R.error("账号已经被禁用");
    16. }
    17. //6.登录成功,将员工id存入Session并返回登录成功结果
    18. request.getSession().setAttribute("users", res.getId());
    19. return R.success(res);
    20. }

    }

    ```