开发中,经常遇到重复提交表单问题,前端响应慢,鼠标快速点了几次,导致后台插入了两条重复的数据,尽管生成的主键id不一样,但在业务上任然属于重复数据,造成业务数据混乱。所以有必要就这个问题研究下解决方案。当然只有增删改的操作需要考虑防重复提交问题。
package com.example.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;
@SpringBootConfiguration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
//创建一个json的序列化对象
GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
//设置value的序列化方式json
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//设置key序列化方式String
redisTemplate.setKeySerializer(new StringRedisSerializer());
//设置hash key序列化方式String
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//设置hash value序列化json
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
// 设置支持事务
redisTemplate.setEnableTransactionSupport(true);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
@Bean
public RedisSerializer