1.登录功能
@PostMapping("/login")
public R<Employee> login (HttpServletRequest request, @RequestBody Employee employee){
//把密码进行加密处理
String password = employee.getPassword();
password = DigestUtils.md5DigestAsHex(password.getBytes());
//根据用户名查询数据库
LambdaQueryWrapper<Employee> employeeLambdaQueryWrapper = new LambdaQueryWrapper<>();
employeeLambdaQueryWrapper.eq(Employee::getUsername , employee.getUsername());
Employee one = employeeService.getOne(employeeLambdaQueryWrapper);
System.out.println(employee.getUsername()+employee.getPassword());
//如果没有查到则失败
if (one == null) {
return R.error("登录失败");
}
//比对密码如果不一致返回失败
if(!one.getPassword().equals(password)){
return R.error("登录失败");
}
//密码比对成功查看状态
if (one.getStatus() == 0){
return R.error("用户已被禁用");
}
//登录成功把id放session
request.getSession().setAttribute("employee",one.getId());
System.out.println("sdsss");
return R.success(one);
}
其中密码需要进行md5加密登录成功后需要把用户的id通过session响应给前端
2.添加员工功能
要给新创建的员工设置一个默认的密码同样需要进行md5加密
还需要从之前登录时穿给前端的用户id取出来添加到creatUser中
@PostMapping
public R<String> save(@RequestBody Employee employee,HttpServletRequest request){
log.info("新增员工员工信息:{}",employee.toString());
//设置初始密码md5加密
employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
employee.setCreateTime(LocalDateTime.now());
employee.setUpdateTime(LocalDateTime.now());
//
Long id = (Long) request.getSession().getAttribute("employee");
employee.setCreateUser(id);
employee.setUpdateUser(id);
employeeService.save(employee);
return R.success("新增员工成功");
}
3.分页功能
其中条件查询需要根据前端给的name看是否为空来进行查询最后结果返回Page对象
@GetMapping("/page")
public R<Page> page(int page ,int pageSize , String name){
//构造分页查询
Page pageInfo = new Page<>(page,pageSize);
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
queryWrapper.like(!StringUtils.isEmpty(name), Employee::getName , name);
queryWrapper.orderByDesc(Employee::getUpdateTime);
employeeService.page(pageInfo,queryWrapper);
return R.success(pageInfo);
}
4.做项目时遇到的问题
如果开启项目没法访问到index.html页面需要配置静态资源映射,需要写一个
WebMvcConfig配置类继承WebMvcConfigurationSupport重写addResourceHandlers()方法
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
/**
* 设置静态资源映射
* @param registry
*/
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
//开始静态资源映射
log.info("静态资源映射");
registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
}
因为用户的id是由mp根据雪花算法自动生成的当long型的id传给前端时前端会丧失精度导致后面几位的数字无法准确的显示,所以需要把long型的数据转换为字符串的数据就可以正常显示了
需要先写一个对象映射器
public class JacksonObjectMapper extends ObjectMapper {
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
public JacksonObjectMapper() {
super();
//收到未知属性时不报异常
this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
//反序列化时,属性不存在的兼容处理
this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
SimpleModule simpleModule = new SimpleModule()
.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)))
.addSerializer(BigInteger.class, ToStringSerializer.instance)
.addSerializer(Long.class, ToStringSerializer.instance)
.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
//注册功能模块 例如,可以添加自定义序列化器和反序列化器
this.registerModule(simpleModule);
}
}
然后在之前的 WebMvcConfig配置类中重写extendMessageConverters方法
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//创建消息转换器对象
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
//设置对象转换器,底层使用Jackson将Java对象转为json
messageConverter.setObjectMapper(new JacksonObjectMapper());
//将上面的消息转换器对象追加到mvc框架的转换器集合中
converters.add(0,messageConverter);
}
注意要把转换器加到第一个不然还是使用springmvc默认的转换器