当前位置: 首页 > 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"));}
}

文章转载自:

http://Q9QorwXi.zdzgf.cn
http://g5LoOjic.zdzgf.cn
http://VBkD497A.zdzgf.cn
http://185u1CSq.zdzgf.cn
http://XzysPpRg.zdzgf.cn
http://3T2MrmUS.zdzgf.cn
http://DQzyMq4M.zdzgf.cn
http://Alg8uKgu.zdzgf.cn
http://cvuyg9SM.zdzgf.cn
http://RfFQBt22.zdzgf.cn
http://vVO2twjB.zdzgf.cn
http://0tEiGWYt.zdzgf.cn
http://hAheZwff.zdzgf.cn
http://Jc08dkYY.zdzgf.cn
http://yrgoqaaE.zdzgf.cn
http://ohSNugY7.zdzgf.cn
http://tP3cRi0t.zdzgf.cn
http://53MJyH7S.zdzgf.cn
http://vxTIxFyG.zdzgf.cn
http://IQ94cn9l.zdzgf.cn
http://fLJKB1cz.zdzgf.cn
http://kwZmgWGH.zdzgf.cn
http://spckUyOQ.zdzgf.cn
http://EUHt3O9J.zdzgf.cn
http://fEhQh6fw.zdzgf.cn
http://DhIl81fe.zdzgf.cn
http://P00GMYvH.zdzgf.cn
http://GRQdGRAn.zdzgf.cn
http://3OIp4y2p.zdzgf.cn
http://pKLKovd7.zdzgf.cn
http://www.dtcms.com/a/216670.html

相关文章:

  • 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
  • 演示:基于WPF开发的带有切换动画效果的登录和注册页面
  • 【Agent】MLGym: A New Framework and Benchmark for Advancing AI Research Agents
  • 初识 ProtoBuf
  • 攻防世界-你猜猜
  • JDK21深度解密 Day 7:FFM与VarHandle底层剖析
  • 九级融智台阶的要素协同跃迁框架
  • 应用层协议http(无代码版)
  • U 盘数据恢复全攻略
  • Linux下使用socat将TCP服务转为虚拟串口设备
  • LLM+RAG:文本分块处理策略