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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值