redis整合springBoot
1.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 https://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>2.6.5</version>
<relativePath/>
</parent>
<groupId>com.xing</groupId>
<artifactId>redissb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redissb</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<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.6.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.17</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.application.yml
server:
port: 8899
spring:
redis:
host: localhost
port: 6379
database: 6
3.Redis的配置类—防止中文乱码
redis配置类 主要是配置了redis的序列化规则,替换默认的jdkSerializer key的序列化规则用StringRedisSerializer value的序列化规则用jackson2JsonRedisSerializer
package com.xing.redissb.util;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* redis配置类
* 主要是配置了redis的序列化规则,替换默认的jdkSerializer
* key的序列化规则用StringRedisSerializer
* value的序列化规则用jackson2JsonRedisSerializer
*/
@Configuration
public class RedisConfig {
//交给spring管理bean
@Bean
public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory connectionFactory){
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
//使用Jackson2JsonRedisSerialize替换默认序列化
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
//设置key和value的序列化规则
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
4.测试redis的String、Hash、List,进行数据的存取,设置过期时间
redis的list类型:双向链表形式,可以从最左边或者最右边进行添加元素和取出元素。
redis的String类型:一个key对应一个value
redis的hash类型:一个key对应一个map:key和value
package com.xing.redissb;
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.*;
import java.util.concurrent.TimeUnit;
@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testRedis(){
String key="hello";
redisTemplate.opsForValue().set("hello","你好");
String response = (String) redisTemplate.opsForValue().get(key);
System.out.println("redis里key为hello的value:"+response);
}
@Test
public void testKeyOps(){
Set<String> keySet = new HashSet<>();
String key1="name";
keySet.add(key1);
redisTemplate.opsForValue().set(key1,"key1的value值",1, TimeUnit.MINUTES);
String key2="token:user1";
String key3="token:user2";
keySet.add(key2);
keySet.add(key3);
redisTemplate.opsForValue().set(key2,"{\"name2\":\"ekonw2\"},\"role2\":\"admin2\"");
redisTemplate.opsForValue().set(key3,"{\"name3\":\"ekonw3\"},\"role3\":\"admin3\"");
List<String> valueList = redisTemplate.opsForValue().multiGet(keySet);
for (String s : valueList) {
System.out.println("============");
System.out.println(s);
}
}
@Test
public void testHashOps(){
String key="hash";
redisTemplate.opsForHash().put(key,"1","你好");
Map<String , Object> map = new HashMap<>();
map.put("2","hello");
map.put("3a","china==3a");
redisTemplate.opsForHash().putAll(key,map);
Map<String,Object> resultMap = redisTemplate.opsForHash().entries(key);
for (String hashKey : resultMap.keySet()) {
System.out.println(hashKey+":"+resultMap.get(hashKey));
}
}
@Test
public void testListOps(){
String listKey="list";
redisTemplate.opsForList().leftPush(listKey,"first value");
redisTemplate.opsForList().leftPush(listKey,"第二个数据从最左边");
redisTemplate.opsForList().rightPush(listKey,"第三个数据从最右边插入数据");
ArrayList<Object> list = new ArrayList<>();
list.add("hello");
list.add("nihao");
list.add(23456);
list.add(false);
redisTemplate.opsForList().rightPushAll(listKey,list);
Long size = redisTemplate.opsForList().size(listKey);
if(size>0){
for (int i=0;i<size-1;i++){
System.out.println(i+":"+redisTemplate.opsForList().leftPop(listKey).toString());
}
}
}
}
|