spring:
redis:
host: localhost
port: 6379
在项目创建的时候选择,如果没有选择就添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
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;
@SpringBootTest
class Redis02SpringbootApplicationTests {
/**
* 我居然直接就指定了泛型 RedisTemplate<String,Object>结果就直接报错了,删除泛型后成功
*/
@Autowired
private RedisTemplate redisTemplate;
@Test
void 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*****/
作者:彼岸舞
时间:2021\05\05
内容关于:Redis
本文属于作者原创,未经允许,禁止转发