首页 > 数据库 >SpringBoot中使用Redis

SpringBoot中使用Redis

时间:2024-08-19 13:06:01浏览次数:7  
标签:SpringBoot ops Redis Student 使用 new 序列化 redisTemplate template

SpringBoot中使用Redis

1.在本地或者云端安装redis服务

2.项目中使用

2.1 引入依赖

  <!--        redis start-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--common-pool 对象池,使用redis时必须引入-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

        <!--Jackson依赖  如果需要将redis存入时json格式化 必须引入 而且必须写配置文件 RedisConfig -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

image

2.2 写配置信息

spring:
  redis:
    database: 3
    host: localhost
    port: 16379
    password: [email protected]++

image

2.3写配置文件

用于序列化键值,便于观看

redisTemplate是使用jdk默认编码格式来序列化的。

存的key value在redis数据库中实际上是乱码的。而StringTemplate不会。

@Configuration
public class RedisConfig {

    /**
     *
     * @param connectionFactory
     * @return org.springframework.data.redis.core.RedisTemplate<java.lang.String, java.lang.Object>
     * @author pyb
     * @date 2023/12/21 15:41
     * 初始化 redisTemplate 使存储的键值为 json 字符串
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory){
        // 创建RedisTemplate对象
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 设置连接工厂
        template.setConnectionFactory(connectionFactory);
        // 创建JSON序列化工具
        GenericJackson2JsonRedisSerializer jsonRedisSerializer =
                new GenericJackson2JsonRedisSerializer();
        // 设置Key的序列化
        template.setKeySerializer(RedisSerializer.string());
        template.setHashKeySerializer(RedisSerializer.string());
        // 设置Value的序列化
        template.setValueSerializer(jsonRedisSerializer);
        template.setHashValueSerializer(jsonRedisSerializer);
        // 返回
        return template;
    }
}

image

2.4 进行测试

这个 redisTemplate 不知道为什么只能用 @Resource 进行注入,使用

// @Autowired
// @Qualifier(value = "redisTemplate")

注入失败

 @Resource
    private RedisTemplate<String,Student> redisTemplate;

    @Test
    void setObject(){
        Student student = new Student(1, "张三", 183);
        redisTemplate.opsForValue().set("user:stu",student,60,TimeUnit.SECONDS);

        Student student1 = redisTemplate.opsForValue().get("user:stu");
        System.out.println(student1);
    }

    @Test
    void setList(){
        ListOperations<String, Student> ops = redisTemplate.opsForList();
        Student s1 = new Student(1, "张三", 18);
        Student s2 = new Student(2, "张三2", 18);
        Student s3 = new Student(3, "张三3", 18);
        ops.rightPush("stu:List",s1);
        ops.rightPushAll("stu:List",s2,s3);

        List<Student> range = ops.range("stu:List", 0, -1);
        System.out.println(range);

    }

image

image

2.5 使用 StringRedisTemplate

这个自带序列化

 @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    void set(){
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        ops.set("str:name1","ppp");
        ops.set("str:name2","y");
        ops.set("str:name3","b");
    }

image

标签:SpringBoot,ops,Redis,Student,使用,new,序列化,redisTemplate,template
From: https://www.cnblogs.com/pyb999/p/18367036

相关文章

  • 基于SpringBoot+Vue+uniapp的吃了吗管理系统(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我代码参考数据库参考源码获取前言......
  • 基于SpringBoot+Vue+uniapp的航班订票管理系统(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我代码参考数据库参考源码获取前言......
  • Redis的十大数据类型的常用命令(上)
    目录1.key的操作命令2.String的常用命令案例一:dy点赞案例二:文章的喜欢数3.List的常用命令案例:公众号订阅的消息4.Hash的常用命令案例:早期购物车设计5.Set的常用命令案例一:抽奖小程序案例二:朋友圈点赞案例三:朋友圈点赞6.Zset的常用集合(sortedset)案例一:根据商品......
  • redis 哨兵模式开启方案
    哨兵模式一、配置sentinel模式二、测试sentinel日志输出的状态信息环境准备准备三台系统为CentOS7的主机master:192.168.152.71slave1:192.168.152.72slave2:192.168.152.73.哨兵模式的介绍:Redis哨兵模式(RedisSentinel)用于提供高可用性和监控功能,主要......
  • 免费分享一套SpringBoot+Vue员工管理(职工管理,考勤管理,奖惩管理,合同管理)管理系统【论
    大家好,我是java1234_小锋老师,看到一个不错的SpringBoot+Vue员工管理(职工管理,考勤管理,奖惩管理,合同管理)管理系统,分享下哈。项目视频演示【免费】SpringBoot+Vue员工管理(职工管理,考勤管理,奖惩管理,合同管理)管理系统Java毕业设计_哔哩哔哩_bilibili项目介绍在数字化转型......
  • 使用 preloadRouteComponents 提升 Nuxt 应用的性能
    title:使用preloadRouteComponents提升Nuxt应用的性能date:2024/8/19updated:2024/8/19author:cmdragonexcerpt:preloadRouteComponents是提升Nuxt应用性能的一个简单而有效的工具。通过在适当的时候预加载路由组件,你可以为用户提供更快速、更流畅的导航体验。......
  • 使用duckdb加载mysql
    安装duckdbhttps://duckdb.org/docs/installation/index加载mysql扩展https://duckdb.org/docs/extensions/mysql.html离线安装的话,可以等INSTALLmysql;超时后,根据它提示的URL到有网环境下载,再传到服务器挂载Nginx,修改/etc/hosts指向,再重新执行INSTALL。Demo代码如下:import......
  • 不用再找了,国内无限制使用GPT4o的方法【2024年最新】
     都知道ChatGPT很强大,聊聊天、写论文、搞翻译、写代码、写文案、审合同等等,无所不能~那么到底怎么使用呢?其实很简单了,国内AI产品发展也很快,很多都很好用了~我一直在用,建议收藏下来~  有最先进、最新的GPT模型,还有很多其他效率工具都是在各自领域,绝对领先地位的产品~......
  • 全面指南:LLMs中的Llama-3模型——简介、安装教程、使用技巧及案例实践详解
    LLMs之Llama3:Llama-3的简介、安装和使用方法、案例应用之详细攻略导读:2024年4月18日,Meta重磅推出了MetaLlama3,本文章主要介绍了Meta推出的新的开源大语言模型MetaLlama3。模型架构Llama3是一种自回归语言模型,采用了优化的Transformer架构。调优版本使用了监督......
  • Excel使用
    01Excel入门跨列居中:对齐方式-水平对齐-选定“跨列居中”(Office365版本)自动调整栏宽单栏调整:双击间隔线。多栏同宽调整:选定栏数,拖动任一栏宽度调整。(调整列亦同)。日期输入输入使用斜线格式,方便excel函数等功能计算使用使用“右键单元格格式-数字-日期”更换日期格式显......