spring-data-redis-demo Hash篇

package text;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;
import java.util.Set;

@RunWith(SpringJUnit4ClassRunner.class)//测试嘛
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")//因为是jar类型的项目没有web.xml
public class TestHash {

    @Autowired
    private RedisTemplate redisTemplate;//看下面的配置文件

    @Test
    public void testSetValue(){
        redisTemplate.boundHashOps("namehash").put("a","黄韩琪");
        redisTemplate.boundHashOps("namehash").put("b","傻逼黄");
        redisTemplate.boundHashOps("namehash").put("c","傻逼成");
        redisTemplate.boundHashOps("namehash").put("d","三人游");
    }

    /**
     * 获取所有的key
     */
    @Test
    public void testGetKes(){
            Set keys=redisTemplate.boundHashOps("namehash").keys();
             System.out.println("输出所有的键"+keys);
    }

    /**
     * 获取所有的值
     */
    @Test
    public void testGetValue(){
        List list=redisTemplate.boundHashOps("namehash").values();
        System.out.println("输出所有的值"+list);
    }

    /**
     * 根据key取值
     *
     */
    @Test
    public void searchValueByKey(){
        String str=(String) redisTemplate.boundHashOps("namehash").get("b");
        System.out.println(str);
    }


    /**
     * 移除某个小key的值
     */
        @Test
        public void removeValue(){
           Long stc=redisTemplate.boundHashOps("namehash").delete("c");
            System.out.println("输出的值是"+stc);
        }
}

码云完整的项目:https://gitee.com/xiaosuhao/SpringDataRedisDemo

猜你喜欢

转载自blog.csdn.net/weixin_41244495/article/details/82972536