当前位置: 首页 > news >正文

尚庭公寓学习笔记

枚举类映射问题

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可以处理常用的数据类型转换,例如StringIntegerDate等等,其中也包含枚举类型,但是枚举类型的默认转换规则是枚举对象实例(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());
http://www.dtcms.com/a/528684.html

相关文章:

  • Unity单例模式基类全解析
  • 餐饮行业做网站的数据ctoc网站有哪些
  • 深圳建设局网站投诉电话淄博网站建设优化公司
  • 久治县网站建设公司东莞人才网最新招聘信息
  • MySQL OCP认证、Oracle OCP认证
  • 深入探讨HarmonyOS中ListItem的滑动操作:从基础实现到高级分布式交互
  • Eclipse Uninstall Software
  • 广东南方通信建设有限公司官方网站合肥网站建设的价格
  • C语言<<超全.超重要>>知识点总结
  • 购物网站开发的业务需求分析做钢材什么网站好
  • Spring框架常用注解全面详解与技术实践
  • 机器学习三要素
  • synchronized锁优化与升级机制
  • 设计公司网站运营wordpress+编辑模板
  • URL下载网络资源
  • Spring Bean注解终极指南:从入门到精通
  • wordpress旅游类网站深圳哪里做网站
  • 【FPGA】38译码器板级验证
  • 初学JVM---什么是JVM
  • 企培内训APP开发案例:实现视频课程、考试与绩效考核一体化
  • 网站后台怎么上传图片产品wordpress不能搜索文章
  • 网站首页默认的文件名一般为云指官网
  • Kafka消费者在金融领域的深度实践:从交易处理到风险控制的完整架构
  • 使用阿里云效搭建个人maven私有仓库
  • Android Studio新手开发第三十一天
  • (四)Gradle 依赖树分析与依赖关系优化
  • Drogon: 一个开源的C++高性能Web框架
  • Java Stream 流:让数据处理更优雅的 “魔法管道“
  • 查看网站服务器ip受欢迎的购物网站建设
  • fpga实现音频预加重(pre-emphasis)滤波器