1.配置redis依赖
因为springboot框架帮们集成了大部分的依赖和它自动配置类的特点,我们只需要在maven中配置后就可以使用了,极大的挺高了我们开发的效率!
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.配置yaml文件
我这里使用的是本地redis服务,所以使用的是本地ip地址,即localhost即可;
如果是虚拟机上配置的redis服务则只需要修改ip地址即可(如果配置了端口号或密码则也要重新配置)
redis:
port: 6379
host: localhost
3.创建redisConfig配置类
@Configuration
public class RedisConfig {
@Bean
//绑定yaml配置
@ConfigurationProperties(prefix = "redis")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
//创建RedisTemplate模板
RedisTemplate<String, Object> template = new RedisTemplate<>();
//关联redisConnectionFactory
template.setConnectionFactory(redisConnectionFactory);
//创建序列化类
GenericToStringSerializer genericToStringSerializer = new GenericToStringSerializer(Object.class);
//序列化类,对象映射设置
//设置value的转化格式和key的转化格式
template.setValueSerializer(genericToStringSerializer);
template.setKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet();
return template;
}
}
4.前端部分,一个简单的测试表单