贝利信息

SpringBoot集成Redis操作API的方法

日期:2023-05-29 00:00 / 作者:PHPz

SpringDataRedis调用Redis底层解读

SpringBoot整合Redis(源码分析)

SpringBoot整合Redis(配置)

yml

spring:
  redis:
    host: localhost
    port: 6379

Maven

在项目创建的时候选择,如果没有选择就添加

  org.springframework.boot
    spring-boot-starter-data-redis

编写测试

redisTemplate.unwatch();
redisTemplate.watch("key");
redisTemplate.multi();
redisTemplate.discard();
redisTemplate.exec();

关于数据库的操作需要获取链接后使用连接对象操作

RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();connection.flushAll();connection.flushDb();connection.close();

测试代码及其执行结果

package co.flower.redis02springboot;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.connection.RedisConnection;import org.springframework.data.redis.core.RedisTemplate;

@SpringBootTestclass Redis02SpringbootApplicationTests {/** * 我居然直接就指定了泛型 RedisTemplate结果就直接报错了,删除泛型后成功     */@Autowiredprivate RedisTemplate redisTemplate;

    @Testvoid contextLoads() {// 英文测试redisTemplate.opsForValue().set("name","xiaojiejie");
        System.out.println(redisTemplate.opsForValue().get("name"));// 中文测试redisTemplate.opsForValue().set("name","小姐姐");
        System.out.println(redisTemplate.opsForValue().get("name"));
    }

}

执行结果,SpringBoot的启动加载和结束销毁没有粘贴/***SpringBootStart****/xiaojiejie
小姐姐/***SpringBootStop*****/