Springboot整合Redis

一、导入pom依赖



    org.springframework.boot
    spring-boot-starter-data-redis

二、编写RedisCondig对key和value进行序列化

package com.example.redis.config;

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;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

@Configuration
public class RedisConfig{

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory){
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(factory);

        //Json序列化配置
        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);

        //String序列号配置
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        //key和hash的key都采用String的序列化配置
        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);

        //value和hash的value采用Json的序列化配置
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);

        template.afterPropertiesSet();

        return template;
    }
}

三、编写配置文件

 

 redis:
    database: 0
    host: localhost
#    password: 123456 (有密码就写 没有就不写)
    port: 6379
    jedis:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 1

四、创建一个Controller 导入RedisTemplate

package com.example.redis.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
public class controller {
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/ops")
    public void redisOps(){
        //新增一个key
        redisTemplate.opsForValue().set("zhangsan","1001");
        //删除某个key
        redisTemplate.delete("zhangsan");
        //删除多个key
        redisTemplate.delete("zhangsan,wangwu,sunliu");
        //删除多个key
        List list=new ArrayList<>();
        list.add("zhangsan");
        list.add("wangwu");
        list.add("sunliu");
        redisTemplate.delete(list);
        //指定key的失效时间
        redisTemplate.expire("zhangsan",1, TimeUnit.MINUTES);
        //根据key获取过期时间
        Long expire = redisTemplate.getExpire("zhangsan");
        System.out.println(expire);
        //判断key是否存在
        Boolean aBoolean = redisTemplate.hasKey("zhangsan");
        System.out.println(aBoolean);
        //设置Hashkey
        redisTemplate.boundHashOps("HashKey").put("SmallKey", "HashVaue");
        //将StringKey对应的值加3
        redisTemplate.boundValueOps("StringKey").increment(3L);
    }
}

 五、使用redisTemplate操作Redis

以下为常用方法及含义

//新增一个key
redisTemplate.opsForValue().set("zhangsan","1001");
//删除某个key
redisTemplate.delete("zhangsan");
//删除多个key
redisTemplate.delete("zhangsan,wangwu,sunliu");
//删除多个key
List list=new ArrayList<>();
list.add("zhangsan");
list.add("wangwu");
list.add("sunliu");
redisTemplate.delete(list);
//指定key的失效时间
redisTemplate.expire("zhangsan",1, TimeUnit.MINUTES);
//根据key获取过期时间
Long expire = redisTemplate.getExpire("zhangsan");
System.out.println(expire);
//判断key是否存在
Boolean aBoolean = redisTemplate.hasKey("zhangsan");
System.out.println(aBoolean);
//设置Hashkey
redisTemplate.boundHashOps("HashKey").put("SmallKey", "HashVaue");
//将StringKey对应的值加3
redisTemplate.boundValueOps("StringKey").increment(3L);

 六、可以使用rdm客户端查看redis中的信息

rdm安装教程Redis管理工具RDM_rdm redishttps://blog.csdn.net/chen15369337607/article/details/120269945?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522169382674416800192217449%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=169382674416800192217449&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-120269945-null-null.142^v93^chatsearchT3_2&utm_term=rdm%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B&spm=1018.2226.3001.4187

你可能感兴趣的:(Redis,java,spring,boot,redis,java)