创建一个SpringBoot项目,我就用快速方法建的
主要要注意一个点:记得勾选NoSql中的Spring Data Redis
Redis主要需要下面的这依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
我们点进去看一下:ctrl+鼠标左键
下面就是spring-boot-starter-data-redis的基本依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.3.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<!--Jedis 改成 lteeuce >
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.3.2.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
我们仔细看一下这个依赖,发现没有Jedis这个链接工具,为什么?
原因:
SpringBoot2.x之后,原来的Jedis被替换成了lettuce
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.boot.autoconfigure.data.redis;
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnClass({RedisOperations.class})
//RedisProperties 绑定的默认配置类
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
public RedisAutoConfiguration() {
}
@Bean
//ConditionalOnMissingBean 这个注解的疑是就是:如果这个bean不存在的话,下面的方法就生效,也就是我们可以自己声明的一个redisTemplate来替换下面默认的方法
@ConditionalOnMissingBean(
name = {"redisTemplate"}
)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
//默认的redisTemplate 没有过多的设置,每一个redis对象都需要序列化
//两个泛型都是Objiect,使用的时候需要强转 <String, Object>
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean //String是redis最常用的数据类型,所以单独封装了一个bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
# redis 配置
spring.redis.host=127.0.1
spring.redis.port=6379
下面图片就是自动配置类的源码:
下面测试:
@SpringBootTest
class RedisSpringbootApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
/**
* opsFor 就是操作某个数据类型
* opsForValue - String
* opsForSet - Set
* 等等其他也都是一样的
*/
//获取连接对象
RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
connection.flushDb();
redisTemplate.opsForValue().set("myKey","redis");
System.out.println(redisTemplate.opsForValue().get("myKey"));
}
}
测试成功!
我们去redis上获取一下键值:
发现 key 前面有 一串乱码、获取key值也是为nil?
下面的内容还有点多,我还是写在下一篇吧
Redis学习(六)—RedisTemplate序列化、自定义RedisTemplate,RedisTemplate工具.
本文地址:https://blog.csdn.net/weixin_43157543/article/details/107584095
如对本文有疑问, 点击进行留言回复!!
HashMap、Hashtable、ConcurrentHashMap三者间的异同
解决RecycleView 中Item包含Edittext时,滑动view复用导致数据错乱的问题
多线程、同步工作原理、死锁案例、Lock接口、线程的生命周期的讲解及实现
网友评论