1、安装Redis
2、启动Redis服务
直接双击Redis安装路径下的redis-server.exe
3、引用依赖
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
4、配置
application.yml
spring:
redis:
database: 0
host: localhost
port: 6379
5、测试
package com.hsb.java_demo;
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.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
@SpringBootTest
public class RedisTest {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
@Test
public void test() {
ValueOperations<String, String> opsValue = stringRedisTemplate.opsForValue();
opsValue.set("name", "nameValue");
opsValue.set("age", "ageValue");
System.out.println(opsValue.get("age"));
opsValue.append("age", ",append");
System.out.println(opsValue.get("age"));
}
}