本文介绍Spring Boot 整合Spring Data Redis的简单使用。
一、概要:
Spring Data Redis是Spring大家族的一部分,提供了在Srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装,RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。
官方文档:https://docs.spring.io/spring-data/redis/docs/2.1.4.RELEASE/reference/html/
二、Spring Boot 中入门使用
1、创建Spring Boot 项目
pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wdg</groupId>
<artifactId>redisdemo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
</project>
2、application.yml文件:
spring:
redis:
host: 127.0.0.1
3、启动类RunApplication.java:
package com.wdg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author WDG
* @date 2019-2-5
*/
@SpringBootApplication
public class RunApplication {
public static void main(String[] args) {
SpringApplication.run(RunApplication.class,args);
}
}
Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)。下面针对前四种种类型给出Demo:
下面示例可直接注入RedisTemplate 实例是因为Spring Boot的自动配置:在 spring-boot-autoconfigure-2.0.1.RELEASE.jar中META-INF/spring.factories配置文件中,配置了要自动装配的Bean:
点进去发现:
@Configuration
@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
public RedisAutoConfiguration() {
}
@Bean
@ConditionalOnMissingBean(
name = {"redisTemplate"}
)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@ConditionalOnMissingBean注解表示如果我们没有手动装载RedisTemplate实例,Spring容器自己会装载一个RedisTemplate实例到容器中。
3.1、String 类型操作
package com.wdg.redisTest;
import com.wdg.RunApplication;
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.test.context.junit4.SpringRunner;
import java.util.concurrent.TimeUnit;
/**
* @author WDG
* String类型操作
* @date 2019-2-5
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RunApplication.class )
public class StringDemo {
//注入RedisTemplate
@Autowired
private RedisTemplate redisTemplate;
@Test
public void setValue(){
redisTemplate.opsForValue().set("name","张三");
redisTemplate.boundValueOps("age").set(20,1, TimeUnit.SECONDS);//设置key过期时间
}
@Test
public void getValue(){
System.out.println(redisTemplate.opsForValue().get("name"));
System.out.println(redisTemplate.opsForValue().get("age"));
}
@Test
public void deleteValue(){
redisTemplate.delete("age");
}
}
3.2、Set类型操作:
package com.wdg.redisTest;
import com.wdg.RunApplication;
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.test.context.junit4.SpringRunner;
import java.util.Set;
/**
* @author WDG
* @date 2019-2-5
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RunApplication.class )
public class SetDemo {
@Autowired
private RedisTemplate redisTemplate;
/**
* 存入值
*/
@Test
public void setValue(){
redisTemplate.boundSetOps("setDemo").add("张三");
redisTemplate.boundSetOps("setDemo").add("李四");
redisTemplate.boundSetOps("setDemo").add("王二");
}
/**
* 提取值
*/
@Test
public void getValue(){
Set members = redisTemplate.boundSetOps("setDemo").members();
System.out.println(members);
}
/**
* 删除集合中的某一个值
*/
@Test
public void deleteValue(){
redisTemplate.boundSetOps("setDemo").remove("王二");
}
/**
* 删除整个集合
*/
@Test
public void deleteAllValue(){
redisTemplate.delete("nameset");
}
}
3.3 List类型操作:
package com.wdg.redisTest;
import com.wdg.RunApplication;
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.test.context.junit4.SpringRunner;
import java.util.List;
/**
* @author WDG
* @date 2019-2-5
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RunApplication.class )
public class ListDemo {
@Autowired
private RedisTemplate redisTemplate;
/**
* 右压栈:后添加的对象排在后边
*/
@Test
public void testSetListValue1(){
redisTemplate.boundListOps("nameDemo").rightPush("jack");
redisTemplate.boundListOps("nameDemo").rightPush("lucy");
redisTemplate.boundListOps("nameDemo").rightPush("ali");
}
/**
* 显示右压栈集合
*/
@Test
public void testGetListValue1(){
List list = redisTemplate.boundListOps("nameDemo").range(0, 10);
//[jack, lucy, ali]
System.out.println(list);
}
/**
* 左压栈:后添加的对象排在前边
*/
@Test
public void testSetListValue2(){
redisTemplate.boundListOps("nameDemo").leftPush("sunshine");
redisTemplate.boundListOps("nameDemo").leftPush("mirror");
redisTemplate.boundListOps("nameDemo").leftPush("eye");
}
/**
* 显示左压栈集合
*/
@Test
public void testGetListValue2(){
List list = redisTemplate.boundListOps("nameDemo").range(0, 10);
//[eye, mirror, sunshine, jack, lucy, ali]
System.out.println(list);
}
// 根据索引查询元素
/**
* 查询集合某个元素
*/
@Test
public void testSearchByIndex(){
String s = (String) redisTemplate.boundListOps("nameDemo").index(1);
//mirror
System.out.println(s);
}
/**
* 移除集合某个元素
*/
@Test
public void testRemoveByIndex(){
redisTemplate.boundListOps("nameDemo").remove(1l,"mirror");
}
}
3.4、Hash类型操作
package com.wdg.redisTest;
import com.wdg.RunApplication;
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.test.context.junit4.SpringRunner;
import java.util.Set;
/**
* @author WDG
* @date 2019-2-5
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RunApplication.class )
public class HashDemo {
@Autowired
private RedisTemplate redisTemplate;
/**
* 存值
*/
@Test
public void testSetHashValue(){
redisTemplate.boundHashOps("hashDemo").put("name", "张三");
redisTemplate.boundHashOps("hashDemo").put("age", 21);
redisTemplate.boundHashOps("hashDemo").put("sex", "男");
redisTemplate.boundHashOps("hashDemo").put("mobile", "1354646");
}
/**
* 获取所有的key
*/
@Test
public void testGetKeys(){
Set s = redisTemplate.boundHashOps("hashDemo").keys();
//[name, sex, age, mobile]
System.out.println(s);
}
/**
* 根据key取值
*/
@Test
public void testGetValueByKey(){
Object object = redisTemplate.boundHashOps("hashDemo").get("age");
//21
System.out.println(object);
}
}
有错误的地方欢迎指正!