当前位置: 首页 > news >正文

springboot调用redis数据库,操作字符串

1.maven导入SpringDataRedis

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

2.config/RedisConfiguration.class

package com.sky.config;import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
@Slf4j
public class RedisConfiguration {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(redisConnectionFactory);// 设置序列化器template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());return template;}
}

3.写测试类,来操作redis/:SpringDataRedisTest

package com.sky.test;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.*;import java.util.concurrent.TimeUnit;@SpringBootTest
public class SpringDataRedisTest {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;@Testpublic void testSpringDataRedis() {// 这里可以编写测试代码,验证Spring Data Redis的功能System.out.println("RedisTemplate: " + redisTemplate);// 例如,测试RedisTemplate的基本操作// 可以使用@Autowired注入RedisTemplate并进行操作ValueOperations<String, Object> value = redisTemplate.opsForValue();ListOperations<String, Object> list = redisTemplate.opsForList();HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();SetOperations<String, Object> set = redisTemplate.opsForSet();ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();}@Testpublic void testRedis() {ValueOperations<String, Object> value = redisTemplate.opsForValue();// 设置code为135790,有效期1分钟,如果不存在则设置,存在则不做任何操作redisTemplate.opsForValue().setIfAbsent("code", 135790,  1, TimeUnit.MINUTES);// 设置code为135790,有效期1分钟,存在就修改,不存在就添加redisTemplate.opsForValue().set("code", 135790,  1, TimeUnit.MINUTES);value.set("city", "北京");// 获取code的值System.out.println(value.get("city"));}
}

相关文章:

  • Java基础打卡-集合2025.05.22
  • Oracle20200714GI_PSU补丁流程及问题收集
  • [yolov11改进系列]基于yolov11引入全维度动态卷积ODConv的python源码+训练源码
  • Java 反射机制深度解析:从原理到实战应用
  • 【25-cv-05791】Aro de luz 摄影灯具商标维权案
  • 端口 3389 服务 ms - wbt - server 漏洞修复方法
  • 2025年渗透测试面试题总结-匿名[实习]安全技术研究员(题目+回答)
  • AI提示工程(Prompt Engineering)高级技巧详解
  • 被忽视的 App 安全入口:资源文件暴露问题与 iOS 混淆实战(含 Ipa Guard 应用经验)
  • 打卡day38
  • Elasticsearch创建快照仓库报错处理
  • 【深度学习】9. CNN性能提升-轻量化模型专辑:SqueezeNet / MobileNet / ShuffleNet / EfficientNet
  • 力扣热题——分类求和并作差
  • Java实现加解密和通信安全
  • C++11 -- 右值引用和移动语义
  • python多进程
  • 在 C++ 中,当回调函数是类的成员函数时,this指针的指向由调用该成员函数的对象决定
  • 4.8.5 利用Spark SQL统计网站每月访问量
  • MySQL事务机制介绍
  • Fastdata极数:中国公路跑步赛事白皮书2025
  • 平度市建设局网站/写软文赚钱的平台都有哪些
  • 鹿城做网站/网络公司主要做哪些
  • 做网站都是需要什么/企业qq下载
  • 攻把受做哭了gv网站/品牌网站建设
  • 上海营销网站设计/外贸网站seo推广教程
  • iis6建设网站浏览/如何在百度上做推广