Springboot整合reids

发布于:2023-01-04 ⋅ 阅读:(239) ⋅ 点赞:(0)

        Springboot对redis的操作封装了两个

        StringRedisTemplateRedisTemplate类,StringRedisTemplate是RedisTemplate的子类,StringRedisTemplate它只能存储字符串类型,无法存储对象类型。要想用StringRedisTemplate存储对象必须把对象转为json字符串

利用Springboot 的环境搭建和开发便利等优势、

 1.StringRedisTemplate

(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.qy151redisspringboot;

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.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest
class Qy151RedisSpringbootApplicationTests {

    //里面所有的key还是value field它的类型必须都是String类型。
    //因为key和value获取field他们使用的都是String的序列化方式
    @Autowired
    private StringRedisTemplate redisTemplate;


    @Test
    public void test02(){
        //对hash类型的操作。
        HashOperations<String, Object, Object> forHash = redisTemplate.opsForHash();
        forHash.put("k1","name","张三");
        forHash.put("k1","age","15");
        Map<String,String> map=new HashMap<>();
        map.put("name","李四");
        map.put("age","25");
        forHash.putAll("k2",map);

        Object o = forHash.get("k1", "name");
        System.out.println(o);

        Set<Object> k1 = forHash.keys("k1");
        System.out.println(k1);
        List<Object> k11 = forHash.values("k1");
        System.out.println(k11);


        //获取k1对于的所有的field和value
        Map<Object, Object> k12 = forHash.entries("k1");
        System.out.println(k12);

    }

    @Test
    void contextLoads() {
        //删除指定的key
//        redisTemplate.delete();
        //查看所有的key
//        redisTemplate.keys()
        //是否存在指定的key
//         redisTemplate.hasKey()

        //对字符串数据类型的操作ValueOperations
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        //存储字符串类型--key  value long unit  setex();
        forValue.set("k1","张三",30, TimeUnit.SECONDS);
        //等价于setnx  存入成功返回true,失败返回false
        Boolean absent = forValue.setIfAbsent("k11", "李四", 30, TimeUnit.SECONDS);
        System.out.println(absent);
        Integer append = forValue.append("k11", "真帅");
        String key = forValue.get("k11");



    }

}

 2.RedisTemplate

package com.qy151redisspringboot;

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.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest
class Qy151RedisSpringbootApplicationTests02 {

     //当你存储的value类型为对象类型使用redisTemplate
    //存储的value类型为字符串。StringRedisTemplate  验证码
     @Autowired
     private RedisTemplate redisTemplate;

     @Test
     public void test01(){
         //必须认为指定序列化方式
         redisTemplate.setKeySerializer(new StringRedisSerializer());
         redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));

         //对String类型操作类
         ValueOperations forValue = redisTemplate.opsForValue();
         //redis中key和value都变成乱码了。
         //key和value都没有指定序列化方式,默认采用jdk的序列化方式。
         forValue.set("k1","张三");


         //value默认采用jdk,类必须实现序列化接口
         forValue.set("k2",new User(1,"刘德华",22));
     }

}

        上面的RedisTemplate需要每次都指定key value以及field的序列化方式,能不能搞一个配置类,已经为RedisTemplate指定好序列化。以后再用就无需指定。

package com.qy151redisspringboot.config;

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.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setConnectionFactory(factory);
        //key序列化方式
        template.setKeySerializer(redisSerializer);
        //value序列化
        template.setValueSerializer(jackson2JsonRedisSerializer);
        //value hashmap序列化  filed value
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(redisSerializer);
        return template;
    }
}


网站公告

今日签到

点亮在社区的每一天
去签到