springboot集成redis

本文详细介绍了如何在Spring Boot应用中添加Redis依赖,配置Redis连接,并在Service中使用RedisTemplate进行数据操作。通过实例展示了如何在`RedisServiceImpl`中实现简单的数据存取功能。

添加依赖

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

设置redis配置信息

在application.properties中设置redis的配置信息

#设置redis配置信息
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=123456

实现

例如在一个Service中实现

package com.example.demo.springboot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisServiceImpl implements RedisService{
    //操作redis数据类型的数据对象
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    public void put(String key, String value) {
        redisTemplate.opsForValue().set(key,value);
    }
}

其中RedisTemplate是springboot框架提供的一个操作redis数据类型的数据对象,通过redisTemplate的opsFor**可以获得操作的对应数据类型的对象
在这里插入图片描述
拿到操作对应数据类型的对象之后即可使用相应的方法操作redis
在这里插入图片描述

---------------------------------
其他springboot相关知识看专栏其他内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值