spring-data-redis的基本使用

Spring Data Redis

​ Spring-data-redis是spring大家族的一部分,提供了在srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装,RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。

​ spring-data-redis针对jedis提供了如下功能:

  1. 连接池自动管理,提供了一个高度封装的“RedisTemplate”类

  2. 针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口

    • ValueOperations:简单K-V操作

    • SetOperations:set类型数据操作

    • ZSetOperations:zset类型数据操作

    • HashOperations:针对map类型的数据操作

    • ListOperations:针对list类型的数据操作

首先,需要在虚拟机安装一个redis数据库,安装 ,我电脑安装redis数据库的主机地址是:192.168.5.111

其次:为了更直观的看reids中的数据变化,所以最好下载一个工具  RedisDesktopManager 可以直观的看到数据有没有在redis数据库中

spring-data-redis的基本使用_第1张图片

第一步:导包

在pom文件中需要导入:spring相关的jar包,srping-data-redis的包,再导入两个测试包,方便测试


    
        demo
        cn.itcast.demo
        1.0-SNAPSHOT
    
    4.0.0

    spring_data_redis
    
    
        4.2.4.RELEASE
    

    
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
            org.springframework
            spring-jms
            ${spring.version}
        
        
            org.springframework
            spring-context-support
            ${spring.version}
        
        
            redis.clients
            jedis
            2.8.1
        
        
        
            org.springframework.data
            spring-data-redis
            1.7.7.RELEASE
        
        
        
            junit
            junit
            4.12
        
        
            org.springframework
            spring-test
            4.2.4.RELEASE
        
    

第二步:引入spring-data-redis的配置文件  redis-config.properties

#装有redis数据库的虚拟机中的一台机器的ip
redis.host=192.168.5.111
redis.port=6379
redis.pass=
#默认存储在databse[0]数据库, redis的database相当于一个数据,默认也是0
redis.database=0
#maxIdle:最大空闲数
redis.maxIdle=300
#maxWait:最大等待时长,3秒
redis.maxWait=3000
#testOnBorrow:在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到的jedis实例均是可用的;
redis.testOnBorrow=true

第三步:在applicationContext_redis.xml中配置一下第二步的配置文件



    
    

   
    
        
        
        
    

    
    

    
    
          
    

第四步:开始使用redisTemplate进行各类型的CURD操作

package spring_data_redis_test;

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")
public class AllTest {
    //注入template对象
    @Autowired
    private RedisTemplate redisTemplate;
//----------------------Hash类型的操作:经常用---------------------------------
//(1)存入值
@Test
public void  boundHashOpsSet(){
    redisTemplate.boundHashOps("namehash").put("country1","中国");
    redisTemplate.boundHashOps("namehash").put("country2","日本");
    redisTemplate.boundHashOps("namehash").put("country3","韩国");
}
//(2)提取所有的KEY
@Test
public void  boundHashOpsKeys(){
    Set keys = redisTemplate.boundHashOps("namehash").keys();
    System.out.println(keys);
}
//(3)提取所有的值
@Test
public void boundHashOpsValues(){
    List values = redisTemplate.boundHashOps("namehash").values();
    System.out.println(values);
}
//(4) 根据KEY提取值
@Test
public void boundHashOpsByKey(){
    Object name = redisTemplate.boundHashOps("namehash").get("country1");
    System.out.println(name);
}
//根据KEY移除值
@Test
public void boundHashOpsDelByKey(){
    redisTemplate.boundHashOps("namehash").delete("country2");
}
//----------------------------值类型的操作:因为操作数量少,所以不长用---------------------------------
    @Test
    public void setValue(){
        redisTemplate.boundValueOps("name").set("王五");
    }
    @Test
    public void getValue(){
        Object name = redisTemplate.boundValueOps("name").get();
        System.out.println(name);
    }
    @Test
    public void deleteValue(){
        redisTemplate.delete("name");
    }

//----------------------set类型的操作:因为操作数量少,所以不长用---------------------------------
    /**
     * 存入值
     */
    @Test
    public void boundSetOpsAdd(){
    redisTemplate.boundSetOps("nameset").add("曹操"); //放入值
    redisTemplate.boundSetOps("nameset").add("刘备");
    redisTemplate.boundSetOps("nameset").add("孙权");
    }
    /**
     * 提取值
     */
    @Test
    public void boundSetOpsGet(){
        Set names= redisTemplate.boundSetOps("nameset").members();//取出值
        System.out.println(names);
    }
    /**
     * 删除集合中的某一个值
     */
    @Test
     public void boundSetOpsDelete(){
         redisTemplate.boundSetOps("nameset").remove("曹操");
     }

    /**
     * 删除整个集合
     */
    @Test
    public void boundSetOpsDeleteAll(){
        redisTemplate.delete("nameset");
    }

    //----------------------list类型的操作:因为操作数量少,所以不长用---------------------------------
    /**
     * 右压栈:后添加的对象排在后边
     * 右压栈用的多,因为快,原理是
     */
    @Test
    public void boundListrightPush(){
        redisTemplate.boundListOps("namelist").rightPush("赵子龙");
        redisTemplate.boundListOps("namelist").rightPush("张飞");
        redisTemplate.boundListOps("namelist").rightPush("关羽");
    }
    /**
     * 显示右压栈集合
     */
    @Test
    public void boundListRange(){
        List namelist = redisTemplate.boundListOps("namelist").range(0, 10);
        System.out.println(namelist);
    }
    

    /**
     * 查询:根据索引查询集合某个元素
     */
    @Test
    public void boundListIndex(){
        Object name = redisTemplate.boundListOps("namelist").index(1);
        System.out.println(name);
    }

    /**
     * 删除: 根据值移除集合某个元素
     */
    @Test
    public void  bondListRemove(){
        redisTemplate.boundListOps("namelist").remove(1,"关羽");
    }
    /**
     * 删除: 删除全部
     */
    @Test
    public void  bondListDelete(){
        redisTemplate.delete("namelist");
    }

}

 

 

 

 

 

 

 

 

你可能感兴趣的:(品优购)