• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
漫椿
博客园    首页    新随笔    联系   管理    订阅  订阅
SpringBoot整合Redis

1导入Redis依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.yml配置中配置Redis(主机、端口)

spring:
redis:
host: localhost
port: 6379

3.调用RedisTemplate提供的操作各种数据存储类型的接口API ,客户端操作RedisTemplate

@SpringBootTest
class SpringbootredisApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void set() {
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set("age",18);
}

@Test
void get() {
ValueOperations valueOperations = redisTemplate.opsForValue();
Object age = valueOperations.get("age");
System.out.println(age);
}

}

4StringReidsTemplate以字符串 作为key和value,与redis客户端操作等效

@SpringBootTest
class SpringbootredisApplicationTests {

@Autowired
private StringRedisTemplate stringRedisTemplate;

@Test
void set(){
ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
stringStringValueOperations.set("testy","testValue");
}
@Test
void get(){
ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
String testy = stringStringValueOperations.get("testy");
System.out.println(testy);
}
}

 

 


posted on 2022-02-09 17:11  编程耽误的厨子  阅读(53)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3