Redis中的hash结构如同java中的map,一个对象里有多个键值对,适合用来存储对象。

SpringBoot中使用RedisTemplate执行Redis基本命令

在application.properties或application.yml文件中配置Redis

spring:
  redis:
    host: 127.0.0.1
    port: 6379

编写代码

package com.heaven.redis;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.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.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.Jedis;

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

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
class RedisApplicationTests {

    @Autowired
    RedisTemplate redisTemplate;

    @Test
    void testHash(){
        //设置生成key value的序列化策略
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(stringSerializer);

        String key = "hash";
        Map<String,String> map = new HashMap<>();
        map.put("f1","val1");
        map.put("f2","val2");
        //为hash结构设置多个键值对(hmset)
        redisTemplate.opsForHash().putAll(key,map);
        //获取hash结构指定字段的value,单个key(hget)
        Object f1 = redisTemplate.opsForHash().get(key, "f1");
        log.info("f1========"+f1);
        //为hash结构设置单个键值对(hset)
        redisTemplate.opsForHash().put(key,"f3","val3");
        Object f3 = redisTemplate.opsForHash().get(key, "f3");
        log.info("f3========"+f3);
        //判断hash结构中是否包含某字段(hexists)
        Boolean haskey = redisTemplate.opsForHash().hasKey(key, "f3");
        log.info("haskey========"+haskey);
        //获取hash结构中所有的键值对(hgetall)
        Map<String,String> entries = redisTemplate.opsForHash().entries(key);
        for(Map.Entry<String,String> entry : entries.entrySet()){
            log.info(entry.getKey()+"======"+entry.getValue());
        }
        //获取所有hash结构中所有的key(hkeys)
        Set<String> keys = redisTemplate.opsForHash().keys(key);
        for(String key1 : keys){
            log.info("key1=========="+keys);
        }
        //获取hash结构中所有的value(hvals)
        List<String> values = redisTemplate.opsForHash().values(key);
        for(String value : values){
            log.info("value======="+value);
        }
        //获取hash结构中指定key的value,可以是多个key(hmget)
        List<String> list = redisTemplate.opsForHash().multiGet(key, keys);
        for(String value2 : list){
            log.info("value2======="+value2);
        }
        //hash结构中若存在相应key才进行操作(hsetnx)
        Boolean success = redisTemplate.opsForHash().putIfAbsent(key, "f3", "6");
        log.info("succes======"+success);
        //删除hash结构中指定的key(hdel)
        Long delete = redisTemplate.opsForHash().delete(key, "f1", "f2");
        log.info("delete=========="+delete);
    }
}

运行结果

RedisTemplate操作Redis数据结构-Hash类型-风君雪科技博客