汇总

1.在实体类的属性上添加JsonIgnore注解,使Jackson在转换时忽略该属性 Jackson
2.在实体类的属性上添加@TableField(fill=””) mybatis-plus
FieldFill.INSERT 插入时填充数据
FieldFill.UPDATE 修改时填充数据
定义类,实现MetaObjectHandler接口,并实现insertFill和updateFill方法
3.数据类型转换器: SpringMVC
SpringMVC的转换器
1. 定义类型转换器,实现Converter接口
@Component
public class StringToItemTypeConverter implements Converter<String, ItemType> {
//做类型转换的方法
@Override
public ItemType convert(String source) {

            ItemType[] values = ItemType.values();
            for(ItemType itemType : values) {
                if(source.equals(itemType.getCode()+"")){
                    return itemType;
                }
            }
            throw new RuntimeException("数据类型输入错误,只能输入1或2");
        }
    }
    2.修改SpringMVC配置类,将配置的类型转换器添加到SpringMVC中
    @Configuration
    public class WebMvcConfiguration implements WebMvcConfigurer {

        @Autowired
        private StringToItemTypeConverter stringToItemTypeConverter;

        @Override
        public void addFormatters(FormatterRegistry registry) {
            registry.addConverter(stringToItemTypeConverter);
        }
    }
SpringMVC的转换器工厂
    @Component     
    public class BaseEnumConverterFactory implements ConverterFactory<String, BaseEnum> {
        
        @Override
        public <T extends BaseEnum> Converter<String, T> getConverter(Class<T> targetType) ConditionalOnPrope{
            return new Converter<String, T>() {
                @Override
                public T convert(String source) {
                    //Class.getEnumConstants() 方法是 Java 反射 API 中的一个方法,用于获取表示枚举类型的 Class 对象中所有枚举常量的数组
                    for (T enumConstant : targetType.getEnumConstants()) {
                        if (enumConstant.getCode().equals(Integer.valueOf(source))) {
                            return enumConstant;
                        }
                    }
                    throw new IllegalArgumentException("非法的枚举值:" + source);
                }
            };
        }
    }
    SpringMVC类型转换器工厂,泛型上限为所有需要转换的类型的超接口
    registry.addConverterFactory(this.stringToBaseEnumConverterFactory);  将类型转换器工厂添加到SpringMVC                               --SpringMVC
Jackson的转换器     JsonValue    默认配置的转换器
Mybatis-plus的转换器    EnumValue   默认配置的转换器

spring.servlet.multipart.max-file-size:100MB     设置单个上传文件大小     Servlet
spring.servlet.multipart.max-request-size:150MB  设置一次请求上传文件大小     Servlet

springdoc.default-flat-param-object:true   将接收参数不做优化处理。(不会把参数误当做json)

BeanUtils.copyProperties(Object source,Object target)  将源对象的同名属性赋值给目标对象

@TableField(exit=flase) mybtais-plus在生成时忽略该属性 mybatis-plus

@EnableConfigurationProperties(ServerProperties.class) 如果当前组件被添加到IOC容器中,也会把ServerProperties一起注入到IOC容器 Springboot

SpringBoot集成定时任务:
@EnableScheduling 在配置类上添加该标签,表示启用定时任务
@Scheduled(cron=”0 0 0 * * *”) 在方法上添加该标签,表示这是一个定时方法,配置cron表达式,设置执行时间
cron表达式构成:秒 分 时 日 月 星期 年(年可以省略)
* 代表所有可能的值
, 用于指定多个值
- 用于指定一个范围
/ 用于指定增量值
? 在日期和星期字段中,用于指示该字段不被指定
L 在日期字段中,用于指定最后一天,例如L在日期字段中代表每月最后一天

序列化问题
jdbc数据库参数加上时区
json序列化
1.@JsonFormat(pattern = “yyyy-MM-dd HH:mm:ss”,timezone = “GMT+8”) 添加到属性上,pattern为时间格式,timezone为时区
2. spring.jackson.date-format: yyyy-MM-dd HH:mm:ss 全局配置json序列化时间格式
spring.jackson.time-zone: GTM+8 全局配置json序列化时区

循环引用问题
spring.main.allow-circular-references: true 设置允许循环引用
@Lazy 设置组件懒加载(使用时加载)

加密问题
md5加密

commons-codec
commons-codec

第三方验证码工具

com.github.whvcse
easy-captcha

获取验证码基本使用
@Override
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);
}

登录校验
导入依赖

io.jsonwebtoken
jjwt-api

<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>
工具类
public class JwtUtil {

    private static long tokenExpiration = 60 * 60 * 1000L;
    private static SecretKey tokenSignKey = Keys.hmacShaKeyFor("M0PKKI6pYGVWWfDZw90a0lTpGYX1d4AQ".getBytes());

    public static String createToken(Long userId, String username) {
        String token = Jwts.builder().
                setSubject("USER_INFO").
                setExpiration(new Date(System.currentTimeMillis() + tokenExpiration)).
                claim("userId", userId).
                claim("username", username).
                signWith(tokenSignKey).
                compressWith(CompressionCodecs.GZIP).
                compact();
        return token;
    }

    public static Claims parseToken(String token) {
        try {
            Jws<Claims> claimsJws = Jwts.parserBuilder().
                    setSigningKey(tokenSignKey).
                    build().parseClaimsJws(token);
            return claimsJws.getBody();

        } catch (ExpiredJwtException e) {
            throw new LeaseException(ResultCodeEnum.TOKEN_EXPIRED);
        } catch (JwtException e) {
            throw new LeaseException(ResultCodeEnum.TOKEN_INVALID);
        }
    }
}

@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
classes = MinioClient.class)) 排除某个组件,使他不加载 Springboot

配置短信服务
1.阿里云注册账号,熟悉基本使用
2.导入依赖

com.aliyun
dysmsapi20170525

3.配置参数
aliyun:
sms:
access-key-id: key
access-key-secret: key所属的账号
endpoint: dysmsapi.aliyuncs.com 使用的服务名称
4.定义参数实体类
@Data
@ConfigurationProperties(prefix = “aliyun.sms”)
public class AliyunSMSProperties {

        private String accessKeyId;

        private String accessKeySecret;

        private String endpoint;
    }
5.定义配置类
    @Configuration
    @EnableConfigurationProperties(AliyunSMSProperties.class)
    @ConditionalOnProperty(prefix = "aliyun.sms", name = "endpoint")
    public class AliyunSMSConfiguration {

        @Autowired
        private AliyunSMSProperties properties;

        @Bean
        public Client smsClient() {
            Config config = new Config();
            config.setAccessKeyId(properties.getAccessKeyId());
            config.setAccessKeySecret(properties.getAccessKeySecret());
            config.setEndpoint(properties.getEndpoint());
            try {
                return new Client(config);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }
    }
6.使用

@ConditionalOnProperty(prefix = “”, name = “”) 增强版条件化注入,只有配置了该参数,配置类才会生效 ————Springboot