尚庭公寓学习笔记
枚举类映射问题
http请求时枚举类映射问题
@Componentpublic class StringToItemTypeConverter implements Converter<String, ItemType> {@Overridepublic ItemType convert(String code) {// ItemType.values() 拿到枚举类中所有枚举类型for (ItemType value : ItemType.values()) {// value.getCode()拿到枚举类型的编号if (value.getCode().equals(Integer.valueOf(code))) {return value;}}throw new IllegalArgumentException("code非法");}}
注册转换器
@Configurationpublic class WebMvcConfiguration implements WebMvcConfigurer {@Autowiredprivate StringToItemTypeConverter stringToItemTypeConverter;@Overridepublic void addFormatters(FormatterRegistry registry) {registry.addConverter(this.stringToItemTypeConverter);}}
ConverterFactory统一对所有枚举类的http请求进行类型转换
ConverterFactory可以集中化的统一的编写一系列类的转换逻辑
转换器
package com.atguigu.lease.web.admin.custom.converter;import com.atguigu.lease.model.enums.BaseEnum;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.stereotype.Component;@Component
public class StringToBaseEnumConverterFactory implements ConverterFactory<String, BaseEnum> {/*** BaseEnum:所有目标类的父类* Class<T> targetType:目标类型的class对象*/@Overridepublic <T extends BaseEnum> Converter<String, T> getConverter(Class<T> targetType) {return new Converter<String, T>() {@Overridepublic T convert(String code) {/*** 获取所有实体类*/T[] enumConstants = targetType.getEnumConstants();for (T value : enumConstants) {if (value.getCode().toString().equals(code)) {return value;}}throw new IllegalArgumentException();}};}
}
package com.atguigu.lease.web.admin.custom.config;import com.atguigu.lease.web.admin.custom.converter.StringToBaseEnumConverterFactory;
import com.atguigu.lease.web.admin.custom.interceptor.AuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {@Autowiredprivate StringToBaseEnumConverterFactory stringToBaseEnumConverterFactory;@Overridepublic void addFormatters(FormatterRegistry registry) {registry.addConverterFactory(this.stringToBaseEnumConverterFactory);}
}
Mysql数据库查询时枚举类的映射问题
Mybatis预置的TypeHandler可以处理常用的数据类型转换,例如String、Integer、Date等等,其中也包含枚举类型,但是枚举类型的默认转换规则是枚举对象实例(ItemType.APARTMENT)和实例名称(“APARTMENT”)相互映射。若想实现code属性到枚举对象实例的相互映射,需要自定义TypeHandler。
不过MybatisPlus提供了一个通用的处理枚举类型的TypeHandler。其使用十分简单,只需在ItemType枚举类的code属性上增加一个注解@EnumValue,Mybatis-Plus便可完成从ItemType对象到code属性之间的相互映射
http响应时映射问题
HttpMessageConverter依赖于Json序列化框架(默认使用Jackson)。其对枚举类型的默认处理规则也是枚举对象实例(ItemType.APARTMENT)和实例名称(“APARTMENT”)相互映射。不过其提供了一个注解@JsonValue,同样只需在ItemType枚举类的code属性上增加一个注解@JsonValue,Jackson便可完成从ItemType对象到code属性之间的互相映射。
验证码生成工具(string转图片)
EasyCaptcha:
<dependency><groupId>com.github.whvcse</groupId><artifactId>easy-captcha</artifactId></dependency>
验证码转为图片
public CaptchaVo getCaptcha() {SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);specCaptcha.setCharType(Captcha.TYPE_DEFAULT);String code = specCaptcha.text().toLowerCase();String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID();String image = specCaptcha.toBase64();redisTemplate.opsForValue().set(key, code, RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);return new CaptchaVo(image, key);}
JWT工具
<dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt-api</artifactId></dependency><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt-impl</artifactId><scope>runtime</scope></dependency><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt-jackson</artifactId><scope>runtime</scope></dependency>
md5加密
password = DigestUtils.md5DigestAsHex(password.getBytes());
