博客主页:
@不会压弯的小飞侠
✨欢迎关注:
点赞
收藏
⭐留言
✒
✨系列专栏:
瑞吉外卖
✨知足上进,不负野心。
欢迎大佬指正,一起学习!一起加油!
@Log4j
注解在类上,为类提供一个属性名为log的log4j日志对象
MP
BaseMapper
,并使用泛型!MP简化了单表CRUD的SQL操作,多表最好还是要自己写 SQL的。IService
实现层也要继承对应的实现类。ServiceImpl
并实现Service
接口密码md5加密
import org.springframework.util.DigestUtils;
String inputMD = "123456";
DigestUtils.md5DigestAsHex(inputMD.getBytes());
掌握MyBatis-Plus 之LambdaQueryWrapper的使用
可以在MyBatis-plus专栏学习。
@Controller注解
@ResponseBody注解
@Controller和@RestController的区别:
Controller 方法参数引入 HttpServletRequest
比如访问一个网站,登录后用户信息被保存到session中,在session过期之前或者用户关闭页面之前,用户信息可以通过request.getSession().getAttribute()方式 获得
request.getSession().removeAttribute()
销毁当前会话域中的一个属性。
F12快捷键
),点击登陆后,页面会发送请求localhost:8080/employee/login
post
username,password
package com.jkj.reggie.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
@Data
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;
private LocalDateTime createTime;
private LocalDateTime updateTime;
@TableField(fill = FieldFill.INSERT)
private Long createUser;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Long updateUser;
}
package com.jkj.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jkj.reggie.entity.Employee;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface EmployeeMapper extends BaseMapper<Employee> {
}
package com.jkj.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.jkj.reggie.entity.Employee;
public interface EmployeeService extends IService<Employee> {
}
package com.jkj.reggie.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jkj.reggie.entity.Employee;
import com.jkj.reggie.mapper.EmployeeMapper;
import com.jkj.reggie.service.EmployeeService;
import org.springframework.stereotype.Service;
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
}
package com.jkj.reggie.common;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
@Data
public class R<T> {
private Integer code; //编码:1成功,0和其它数字为失败
private String msg; //错误信息
private T data; //数据
private Map map = new HashMap(); //动态数据
public static <T> R<T> success(T object) {
R<T> r = new R<T>();
r.data = object;
r.code = 1;
return r;
}
public static <T> R<T> error(String msg) {
R r = new R();
r.msg = msg;
r.code = 0;
return r;
}
public R<T> add(String key, Object value) {
this.map.put(key, value);
return this;
}
}
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
/**
* 员工登录
* @param request
* @param employee
* @return
*/
@PostMapping("/login")
public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
//1、将页面提交的密码password进行md5加密处理
String password = employee.getPassword();
password = DigestUtils.md5DigestAsHex(password.getBytes());
//2、根据页面提交的用户名username查询数据库
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Employee::getUsername,employee.getUsername());
Employee emp = employeeService.getOne(queryWrapper);
//3、如果没有查询到则返回登录失败结果
if(emp == null){
return R.error("登录失败");
}
//4、密码比对,如果不一致则返回登录失败结果
if(!emp.getPassword().equals(password)){
return R.error("登录失败");
}
//5、查看员工状态,如果为已禁用状态,则返回员工已禁用结果
if(emp.getStatus() == 0){
return R.error("账号已禁用");
}
//6、登录成功,将员工id存入Session并返回登录成功结果
request.getSession().setAttribute("employee",emp.getId());
return R.success(emp);
/**
* 员工退出
* @param request
* @return
*/
}
}
localhost:8080/backend/page/login/login.html
登录成功
密码错误的情况下,会提示登录失败
用户名错误的情况下,会提示登录失败
将用户status属性修改为0,提示账号已禁用信息
F12快捷键
),点击关机按钮后,页面会发送请求localhost:8080/employee/logout
post
清理session中的id
返回结果
/**
* 员工退出
* @param request
* @return
*/
@PostMapping("/logout")
public R<String> logout(HttpServletRequest request){
//清理Session中保存的当前登录员工的id
request.getSession().removeAttribute("employee");
return R.success("退出成功");
}