[Java2023] Day12.1 - 基础登录功能的视线

101 阅读1分钟
  • 1.添加LoginController
package com.tlias.controller;

import com.tlias.pojo.Emp;
import com.tlias.pojo.Result;
import com.tlias.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

    @Autowired
    public EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        Emp e = empService.login(emp);
        return e != null ? Result.success() : Result.error("账号或者密码错误");
    }
}
  • 2.1 EmpService 中
// 省略
public interface EmpService {
    // 省略
    Emp login(Emp emp);
}
  • 2.2 service实现类EmpServiceImpl中
package com.tlias.service.impl;

@Service
public class EmpServiceImpl implements EmpService {
    // 省略
     
    @Override
    public Emp login(Emp emp) {
        return empMapper.getByUsernameAndPassword(emp);
    }

}
  • 3.Mapper中
package com.tlias.mapper;
// 省略

@Mapper
public interface EmpMapper {

    // 省略

    @Select("SELECT * from emp where username=#{username} and password=#{password}")
    Emp getByUsernameAndPassword(Emp emp);
}

image.png