官网:http://projects.spring.io/spring-data-redis/ 这是spring整合的Redis框架十分简洁好用
导入SpringDataRedis依赖也会传递导入了SpringBoot基本依赖,为了演示基本操作我在测试类里操作,我引入了 text包
<!--SpringDataRedis依赖我没有加版本号,这个一定要加上,除非你有springboot依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
<!-- SpringDataRedis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>package com.lianxi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringDataRedisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDataRedisApplication.class,args);
}
}在 application.yml 中添加如下配置,端口不修改默认是 6379
spring:
redis:
host: 127.0.0.1 # 自己的Redis地址
port: 6379在使用SpringData-Redis的类里注入StringRedisTemplate,好处就是在查看是不会有看不懂的编码的问题。
里面封装了对于Redis的五种数据结构的各种操作,包括:
例如我们对字符串操作比较熟悉的有:get、set等命令,这些方法都在 opsForValue()返回的对象中有:

package com.lianxi.text;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SDRDeomStr {
@Autowired
private StringRedisTemplate redisTemplate;
@Test //如果有就修改,没有就添加
public void testAdd(){
//为设置过期时间
redisTemplate.boundValueOps("name1").set("未设置过期时间");
//设置过期时间,10秒后销毁,TimeUnit:是告诉Redis时间单位
redisTemplate.boundValueOps("name2").set("设置过期时间",10, TimeUnit.SECONDS);
}
@Test //获取
public void testGet(){
String name = redisTemplate.boundValueOps("name").get();
}
@Test //删除
public void testDel(){
redisTemplate.delete("name");
}
}package com.lianxi.text;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SDRDeomList {
@Autowired
private StringRedisTemplate redisTemplate;
@Test //添加,右压栈
public void testAddList(){
redisTemplate.boundListOps("listKey").rightPush("迪丽热巴");
redisTemplate.boundListOps("listKey").rightPush("哈妮克孜");
redisTemplate.boundListOps("listKey").rightPush("古力娜扎");
redisTemplate.boundListOps("listKey").rightPush("王五赵四");
}
@Test //获取,range相当于分页,从0开始取10个
public void testGetList(){
List<String> stringList = redisTemplate.boundListOps("listKey").range(0, 10);
//打印结果
System.out.println(stringList);
}
@Test //删除,用右弹出或者直接删除 都可以
public void testDelList(){
//弹出方式,谈一个没一个
String listKey = redisTemplate.boundListOps("listKey").rightPop();
System.out.println(listKey);
// redisTemplate.delete("listKey");
}
}package com.lianxi.text;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SDRDeomSet {
@Autowired
private StringRedisTemplate redisTemplate;
@Test //添加,直接add添加
public void testAddSet(){
redisTemplate.boundSetOps("setKey").add("杨幂");
redisTemplate.boundSetOps("setKey").add("唐嫣");
redisTemplate.boundSetOps("setKey").add("刘诗诗");
redisTemplate.boundSetOps("setKey").add("张萌");
redisTemplate.boundSetOps("setKey").add("准备删除");
}
@Test //获取 members:所有成员
public void testGetSet(){
//获取所有成员
Set<String> stringList = redisTemplate.boundSetOps("setKey").members();
//打印
System.out.println(stringList);
}
@Test //删除
public void testDelSet(){
//根据大key删除,这个是全部删除
// redisTemplate.delete("setKey");
//根据小key删除
redisTemplate.boundSetOps("setKey").remove("准备删除");
}
}package com.lianxi.text;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SDRDeomHash {
@Autowired
private StringRedisTemplate redisTemplate;
@Test //添加
public void testAddHash(){
redisTemplate.boundHashOps("hashKey").put("a","AAA");
redisTemplate.boundHashOps("hashKey").put("b","BBB");
redisTemplate.boundHashOps("hashKey").put("c","CCC");
redisTemplate.boundHashOps("hashKey").put("d","DDD");
}
@Test //获取
public void testGetHash(){
//先获取key集合
Set<Object> stringList = redisTemplate.boundHashOps("hashKey").keys();
//打印key集合
System.out.println("所有key:"+stringList);
//遍历打印
for (Object oj : stringList) {
Object hashKey = redisTemplate.boundHashOps("hashKey").get(oj);
//打印value值
System.out.println(oj+":"+hashKey);
}
}
@Test //删除弹出和直接删除都可以
public void testDelHash(){
//删除全部
// redisTemplate.delete("hashKey");
//根据key删除
redisTemplate.boundHashOps("hashKey").delete("a");
}
}