SpringBoot整合Redis
SpringBoot整合Redis
实现步骤
- ① 搭建SpringBoot工程
- ② 引入redis起步依赖
- ③ 配置redis相关属性
- ④ 注入RedisTemplate模板和编写测试方法
①搭建SpringBoot工程

② 引入redis起步依赖
勾上 NoSQL -> Spring Data Redis(Access + Driver)

③ 配置redis相关属性
如果是使用本机的redis,则不需要配置
-
application.yml
spring: redis: host: 127.0.0.1 # redis的主机ip port: 6379
④ 注入RedisTemplate模板和编写测试方法
package com.cmy.springbootredis;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
class SpringbootRedisApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testSet() {
// 存入数据
redisTemplate.boundValueOps("name").set("zhangsan");
}
@Test
public void testGet() {
// 获取数据
Object name = redisTemplate.boundValueOps("name").get();
System.out.println(name);
}
}
本文详细介绍了如何在SpringBoot项目中集成Redis,包括搭建SpringBoot工程、引入Redis依赖、配置连接参数以及编写RedisTemplate的使用示例。通过实际操作,读者将掌握如何进行数据存储和检索,是Redis入门开发者的实用指南。
764

被折叠的 条评论
为什么被折叠?



