- 项目一学习总结
- 前言
- 一、设置静态资源映射
- 1. 代码
- 二、用户登录密码存入数据库使用MD5算法加密
- 1.使用hutool工具类将用户输入的密码进行md5加密
- 二、拦截器和过滤器
- 1. 拦截器
- 代码一:定义登录校验拦截器
- 代码二:配置拦截器
- 2. 过滤器
- 三、全局异常处理
- 1. 思路分析
- 四、分页查询pageHelper
- 1. 介绍
- 2. 使用
- 2.1 导入依赖
- 2.2 在application.yaml文件中配置信息
- 3. 代码
- 五、 自定义对象转换器
- 具体实现步骤:
- 代码一:
- 代码二:
前言
学习外卖项目总结以后可使用的知识点一,此资料为学习所用
一、设置静态资源映射用于在Springboot项目中, 默认静态资源的存放目录为 : “classpath:/resources/”, “classpath:/static/”, “classpath:/public/” ; 而在我们的项目中静态资源存放在 backend, front 目录中, 那么这个时候要想访问到静态资源, 就需要设置静态资源映射。
1. 代码package com.itheima.reggie.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
@Slf4j
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
log.info("===========静态资源处理=================");
//1. 注册backend静态资源处理方式 **匹配多级路径
registry.addResourceHandler("/backend
@ControllerAdvice(annotations = {Controller.class, RestController.class}) // 表示该类是一个异常处理类
@ResponseBody //该类返回的数据全部都会转换为json字符串
@Slf4j
public class GlobalExceptionHandler {
//@ExceptionHandler(value = Exception.class) 代表了该方法会处理所有的异常, 只要controller那边出现了异常都会给方法方法处理
@ExceptionHandler(value = Exception.class)
public R handlerException(Exception e){ //一旦controller出现了异常会把异常对象传递给形参
log.info("========异常处理======");
e.printStackTrace();
return R.error("服务器异常:"+e.getMessage());
}
}
上述的全局异常处理器上使用了的两个注解 @ControllerAdvice , @ResponseBody , 他们的作用分别为:
@ControllerAdvice : 指定拦截那些类型的控制器;
@ResponseBody: 将方法的返回值 R 对象转换为json格式的数据, 响应给页面;
上述使用的两个注解, 也可以合并成为一个注解 @RestControllerAdvice
PageHelper是国内非常优秀的一款开源的mybatis分页插件,它支持基本主流与常用的数据库, 例如mysql、 oracle、mariaDB、 DB2、 SQLite、Hsqldb等。
本项目在 github 的项目地址: https://github.com/pagehelper/Mybatis-PageHelper
本项目在 gitosc 的项目地址: http://git.oschina.net/free/Mybatis_PageHelper
自己实现分页的弊端:
- (1)每一种数据库分页语句不同,我们实现分页需要针对每一种数据库定制符合其语法的SQL,如果更换数据库,需要修改分页SQL。limit top rownum
- (2)除了这些以外,我们还要定义PageBean,封装分页参数,PageHelper提供了PageInfo对象给你们去使用,这里的PageInfo对象就是你们以前定义pageBean.
2.2 在application.yaml文件中配置信息com.github.pagehelper pagehelper-spring-boot-starter 1.4.0
pagehelper: # 自动获取方言 auto-dialect: true # 合理化分页 reasonable: true3. 代码
package com.itheima.reggie.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.common.Page;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import java.util.List;
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeMapper employeeMapper;
@Override
public Page findByPage(Integer page, Integer pageSize, String name) {
//1. 设置当前页与页面大小
PageHelper.startPage(page,pageSize);
//2. 查询数据
List employeeList = employeeMapper.findByName(name);
//3. 创建PageINfo对象,把list集合传入
PageInfo pageInfo = new PageInfo(employeeList);
//4. 把pageinfo的信息转移到Page对象
Page pageResult = new Page<>(pageInfo.getList(),pageInfo.getTotal());
return pageResult;
}
}
五、 自定义对象转换器
问题:js在对长度较长的长整型数据进行处理时, 会损失精度, 从而导致提交的id和数据库中的id不一致
解决方案:将对象转换成String对象即可
具体实现步骤:1). 提供对象转换器JacksonObjectMapper,基于Jackson进行Java对象到json数据的转换(资料中已经提供,直接复制到项目中使用)
2). 在WebMvcConfig配置类中扩展Spring mvc的消息转换器,在此消息转换器中使用提供的对象转换器进行Java对象到json数据的转换
代码一:引入JacksonObjectMapper
package com.itheima.reggie.config;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
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);
}
}
该自定义的对象转换器, 主要指定了, 在进行json数据序列化及反序列化时, LocalDateTime、LocalDate、LocalTime的处理方式, 以及BigInteger及Long类型数据,直接转换为字符串。
代码二:在WebMvcConfig中重写方法extendMessageConverters
@Override protected void extendMessageConverters(List> converters) { //创建消息转换器对象 MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter(); //设置对象转换器,底层使用Jackson将Java对象转为json messageConverter.setObjectMapper(new JacksonObjectMapper()); //将上面的消息转换器对象追加到mvc框架的转换器集合中 converters.add(0,messageConverter); }



