Springboot整合Redis

Redis下载安装icon-default.png?t=MBR7https://blog.csdn.net/weixin_51882166/article/details/127889515

导入坐标

新建项目模块:

 

 (如果不使用Spring Initializr添加依赖,则需要手动添加依赖项)

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

配置Redis

修改文件名:

 打开Redis服务器,查看参数:

 在yml文件里配置信息:

spring:
  redis:
    host: localhost
    port: 6379

Redis接口

在测试类里测试:

 测试set方法:

package spring_redis;

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.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

@SpringBootTest
class SpringRedisApplicationTests {
    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    void set() {
      ValueOperations ops= redisTemplate.opsForValue();
      ops.set("name","lucy");

    }
}

@Autowired自动装配RedisTemplate(提供各种数据类型的API)。

执行在DesktopManager后查询:

获取键值并打印在控制台上:

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

控制台:

 测试放入Hash:

  @Test
    void hset() {
        HashOperations ops = redisTemplate.opsForHash();
        ops.put("info","A","a");

    }

 

得到放入的Hash值:

 @Test
    void hget() {
        HashOperations ops = redisTemplate.opsForHash();
        Object val = ops.get("info", "A");
        System.out.println(val);

    }

控制台显示:

 

SpringBoot整合Redis:

 pom.xml导入对应redis的starter;

 配置;

 提供操作Redis接口对象RedisTemplate;

 ops*:获取各种数据类型操作接口。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值