Spring Boot 连 Redis 独立模式

来自姬鸿昌的知识库
跳到导航 跳到搜索

pom.xml

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



application.yml

spring:
  redis:
      database: 0
      host: 127.0.0.1
      port: 6379
      password: vn4sj5kbxdaG



RedisTemplateTest.java

package io.github.jihch;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;

@SpringBootTest
public class RedisTemplateTest {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    private static String KEY = "20230218";

    private static String VALUE = "11:28:00";

    @Test
    public void testSet() {
        redisTemplate.opsForValue().set(KEY, VALUE);
    }

    @Test
    public void testGet() {
        Object o = redisTemplate.opsForValue().get(KEY);
        Assertions.assertEquals(VALUE, o);
    }
}



https://github.com/jihch/spring-boot-redis-standalone