进行spring boot 整合redis操作的前提是了解redis和spring boot的基本操作
若不清楚这两项基本前提,请参考以下文章
redis学习
spring MVC入门
spring入门
首先新建一个maven工程,然后向pom.xml中添加一些基本依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- spring boot版本在1.5以上就必须加-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.1</version>
</dependency>
等包下载完之后,进行redis的配置,由于我在本项目中用的是application.yaml,所以格式会和application.properties有些出入
#redis配置
spring:
redis:
#redis数据库索引
database: 0
#redis服务器地址
host: localhost
#redis服务器端口
port: 6379
#redis数据库密码,默认为空
password:
lettuce:
pool:
#连接池中最大连接数(使用负数表示没有限制。默认8)
max-active: 8
#连接池中最大阻塞等待时间(是用负值表示没有时间限制,此处用-1)
max-wait: -1
#连接池中最大空闲连接,默认8
max-idle: 8
#连接池中最小空闲连接,默认0
min-idle: 0
#连接超时时间
timeout: 5000
配置完毕后就可以对redis数据库进行操作了,本文采取Junit进行操作,代码如下:
package com.redis.demo;
import com.redis.demo.controller.Student;
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.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void redisStr() throws InterruptedException{
ValueOperations operations = redisTemplate.opsForValue();
operations.set("name","wcc1");
System.out.println("检索key:"+operations.get("name"));
}
@Test
public void redisObj()throws InterruptedException{
ValueOperations operations = redisTemplate.opsForValue();
Student student = new Student();
student.setName("wcc");
student.setAge(16);
student.setClas("123");
operations.set("student",student);
System.out.println("获取"+operations.get("student"));
}
}
值得一提的是,进行对象存储的时候,必须将对象进行序列化,否则会无法存取
对redis数据进行delete操作
@Test
public void redisDel(){
ValueOperations operations = redisTemplate.opsForValue();
redisTemplate.delete("name");
System.out.println("name为:"+operations.get("name"));
}
@Test
public void redisHash(){
HashOperations operations = redisTemplate.opsForHash();
operations.put("hash","name","wcc");
System.out.println(operations.get("hash","name"));
}
@Test
public void redisList(){
ListOperations<String,String> operations = redisTemplate.opsForList();
operations.leftPush("name","张三") ;
operations.leftPush("name","李四");
operations.leftPush("name","王二");
operations.leftPush("name","麻子");
for (int i = 0; i < 4; i++) {
System.out.println(operations.leftPop("name"));
}
}
@Test
public void redisSet(){
SetOperations<String,String> operations = redisTemplate.opsForSet();
operations.add("name1","wcc") ;
operations.add("name1","wcc1");
operations.add("name1","wcc2");
operations.add("name1", "wcc3");
System.out.println(operations.members("name1"));
}
@Test
public void redis_Z_Set(){
ZSetOperations operations = redisTemplate.opsForZSet();
operations.add("name2","wcc",1);
operations.add("name2","wcc1",2);
operations.add("name2","wcc2",3);
operations.add("name2","wcc3",4);
System.out.println(operations.rangeByScore("name2",1,3));
}
值得一提的是,当向redis中存放数据的时候,不同类型的数据不能取相同的key,假设List和Set类型的数据全都用name作为key,当读取的时候就会出现不同类型的数据无法读取的错误。