赞
踩
springboot整合redis时提供了两个模板工具类,StringRedisTemplate和RedisTemplate.
(1) 引入相关的依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- </dependency>
(2)注入StringRedisTemplate该类对象
- @Autowired
- private StringRedisTemplate redisTemplate;
(3)使用StringRedisTemplate
该类把对每种数据类型的操作,单独封了相应的内部类。
- package com.wt;
-
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.data.redis.core.HashOperations;
- import org.springframework.data.redis.core.StringRedisTemplate;
- import org.springframework.data.redis.core.ValueOperations;
-
- import java.time.Duration;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.concurrent.TimeUnit;
-
- @SpringBootTest
- class SpringbootRedis01ApplicationTests {
-
- //里面所有的key还是value field 它的类型必须都是String类型
- //因为key和value获取field它们使用的都是String的序列化方式
- @Autowired
- private StringRedisTemplate redisTemplate;
-
- //以hash方式对String操作
- @Test
- public void test01(){
- HashOperations<String, Object, Object> forHash = redisTemplate.opsForHash();
- //以hash方式向Redis存储数据
- forHash.put("k2","name","王戎");
- forHash.put("k2","age","25");
- forHash.put("k2","sex","男");
- Map map = new HashMap();
- map.put("name","山涛");
- map.put("age","23");
- map.put("sex","男");
- forHash.putAll("k3",map);
-
- //通过hash获取指定的key下指定键的值
- Object o = forHash.get("k3", "name");
- System.out.println(o);
-
- //获取K3下所有的键
- Set<Object> k3 = forHash.keys("k3");
- System.out.println(k3);
- //获取k3下所有的值
- List<Object> k31 = forHash.values("k3");
- System.out.println(k31);
-
- //获取k3下所有键值对
- Map<Object, Object> k32 = forHash.entries("k3");
- System.out.println(map);
-
- //删除k3下的指定键
- Long delete = forHash.delete("k3", "age");
- System.out.println(delete);
-
-
- }
-
- //对String的操作
- @Test
- void contextLoads() {
- ValueOperations<String, String> forValue = redisTemplate.opsForValue();
- //Set<String> keys = redisTemplate.keys("k1");
- forValue.set("k1","k2", Duration.ofSeconds(3000));
- String k1 = forValue.get("k1");
- System.out.println(k1);
-
- //相当于setnx ------如果存在该键则不存入,不存在则存入
- Boolean aBoolean = forValue.setIfAbsent("k1", "刘伶", 30, TimeUnit.SECONDS);
- Boolean aBoolea = forValue.setIfAbsent("k11", "刘伶", 30, TimeUnit.SECONDS);
- System.out.println(aBoolea);
-
- forValue.append("k11","斤斤计较");
- }
-
- }
- package com.wt;
-
- import com.wt.entity.Dept;
- import org.junit.jupiter.api.Test;
- 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.data.redis.serializer.Jackson2JsonRedisSerializer;
- import org.springframework.data.redis.serializer.StringRedisSerializer;
-
-
-
- @SpringBootTest
- public class SprigbootRedis02 {
- //当你存储的value类型为对象类型使用redisTemplate
- //存储的value类型为字符串,StringRedisTemplate
- @Autowired
- private RedisTemplate redisTemplate;
-
-
- @Test
- public void test01(){
- /在使用RedisTemplate前必须序列化,否则数据库会出现乱码
- redisTemplate.setKeySerializer(new StringRedisSerializer()); //给key添加序列化
- redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));//给value添加序列化
-
- //对String类型操作类
- ValueOperations forValue = redisTemplate.opsForValue();
- //如果不设置序列化,key和value默认采用jdk的序列化方式
- forValue.set("k1","陈奕迅");
- Object k1 = forValue.get("k1");
- System.out.println(k1);
-
- //如果不实现序列化,value默认采用jdk的序列化方式,可以给实体类加序列化接口
- forValue.set("k3",new Dept(1,"刘念牛",23));
-
- }
- }
上面的RedisTemplate需要每次都指定key value以及field的序列化方式,所以我们可以写一个配置类,为RedisTemplate指定好序列化。以后再用就无需再指定。
- package com.wt.conf;
-
- import com.fasterxml.jackson.annotation.JsonAutoDetect;
- import com.fasterxml.jackson.annotation.PropertyAccessor;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import org.springframework.cache.CacheManager;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.data.redis.cache.RedisCacheConfiguration;
- import org.springframework.data.redis.cache.RedisCacheManager;
- 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.RedisSerializationContext;
- import org.springframework.data.redis.serializer.RedisSerializer;
- import org.springframework.data.redis.serializer.StringRedisSerializer;
-
- import java.time.Duration;
-
- /**
- * @Author wt
- * @Date 2022/8/2 19:56
- * @PackageName:com.wt.conf
- * @ClassName: RedisConfig
- * @Description: RedisTemplate的序列化配置文件
- * @Version 1.0
- */
- @
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。