“Spring Boot 连 Redis 独立模式”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (建立内容为“https://github.com/jihch/spring-boot-redis”的新页面) |
Jihongchang(讨论 | 贡献) |
||
第1行: | 第1行: | ||
+ | === pom.xml === | ||
+ | <syntaxhighlight lang="xml"> | ||
+ | <dependency> | ||
+ | <groupId>org.springframework.boot</groupId> | ||
+ | <artifactId>spring-boot-starter-data-redis</artifactId> | ||
+ | </dependency> | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | === application.yml === | ||
+ | <syntaxhighlight lang="yaml"> | ||
+ | spring: | ||
+ | redis: | ||
+ | database: 0 | ||
+ | host: 127.0.0.1 | ||
+ | port: 6379 | ||
+ | password: vn4sj5kbxdaG | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | === RedisTemplateTest.java === | ||
+ | <syntaxhighlight lang="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); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
https://github.com/jihch/spring-boot-redis | https://github.com/jihch/spring-boot-redis |
2023年2月18日 (六) 04:44的版本
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);
}
}