Spring Data Redis 是属于 Spring Data 下的一个模块。作用就是简化对于 redis 的操作
1 整合步骤:
step1:pom 文件配置
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<groupId>com.panyuro</groupId>
<artifactId>springboot-redis</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>springboot-redis Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- springBoot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Spring Data Redis的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
step2:提取 redis 的配置信息
-
在 src/main/resource/ 目 录 下 新 建 一 个 配 置 文件:application.properties
spring.redis.pool.max-idle=10
spring.redis.pool.min-idle=5
spring.redis.pool.max-total=20
spring.redis.hostName=192.168.0.25
spring.redis.port=6379
step3:编写 Spring Data Redis 的配置类(重点)
package com.my.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
/**
* 完成Springboot对Redis的整合的一些配置
*/
@Configuration
public class RedisConfig {
/**
* 1.创建JedisPoolConfig对象。在该对象中完成一些链接池配置
*/
@Bean
@ConfigurationProperties(prefix="spring.redis.pool")
public JedisPoolConfig jedisPoolConfig(){
JedisPoolConfig config = new JedisPoolConfig();
return config;
}
/**
* 2.创建JedisConnectionFactory:配置redis链接信息
*/
@Bean
@ConfigurationProperties(prefix="spring.redis")
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config){
JedisConnectionFactory factory = new JedisConnectionFactory();
//关联链接池的配置对象
factory.setPoolConfig(config);
return factory;
}
/**
* 3.创建RedisTemplate:用于执行Redis操作的方法
*/
@Bean
public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory factory){
RedisTemplate<String, Object> template = new RedisTemplate<>();
//关联
template.setConnectionFactory(factory);
//为key设置序列化器
template.setKeySerializer(new StringRedisSerializer());
//为value设置序列化器
template.setValueSerializer(new StringRedisSerializer());
return template;
}
}
2 测试整合
2.1 Spring Data Redis存取字符串
step1:修改 pom 文件添加测试启动器坐标
<!-- Test的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
step2:编写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes= App.class)
public class TestRedis {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 添加一个字符串
*/
@Test
public void testSet(){
this.redisTemplate.opsForValue().set("cc", "你好");
}
/**
* 获取一个字符串
*/
@Test
public void testGet(){
String value = (String)this.redisTemplate.opsForValue().get("cc");
System.out.println(value);
}
}
step3:创建启动类
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
测试结果:
先连接Redis Desktop Manage
运行完testSet,可发现默认的数据库db0,增加一条数据。
运行完testGet,控制台输出:
2.2.Spring Data Redis 以 JSON 格式存储实体对象
step1:创建实体类(必须实现序列化接口)
public class Users implements Serializable {
private int id;
private String name;
private int age;
step2:测试代码
/**
* 基于JSON格式存Users对象
*/
@Test
public void testSetUsersUseJSON(){
Users users = new Users(2,"张三",20);
this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
this.redisTemplate.opsForValue().set("users_json", users);
}
/**
* 基于JSON格式取Users对象
*/
@Test
public void testGetUseJSON(){
this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
Users users = (Users)this.redisTemplate.opsForValue().get("users_json");
System.out.println(users);
}
测试结果:
运行完testSetUsersUseJSON,可发现默认的数据库db0,增加一条数据。
附:项目结构