RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
RedisTemplate方法如何在spring中使用

本篇文章给大家分享的是有关redisTemplate方法如何在spring中使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联公司:自2013年创立以来为各行业开拓出企业自己的“网站建设”服务,为上千公司企业提供了专业的成都网站建设、网站制作、网页设计和网站推广服务, 按需策划设计由设计师亲自精心设计,设计的效果完全按照客户的要求,并适当的提出合理的建议,拥有的视觉效果,策划师分析客户的同行竞争对手,根据客户的实际情况给出合理的网站构架,制作客户同行业具有领先地位的。

需要的jar包
spring-data-Redis-1.6.2.RELEASE.jar

jedis-2.7.2.jar(依赖 commons-pool2-2.3.jar)

commons-pool2-2.3.jar

spring-redis.xml 配置文件





   
     
     
     
     
     
   

   
     
     
     
     
   

    
      
      
        
       
      
        
      
       
         
      
      
         
     
    

测试代码

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

public static void main(String[] args) {
    ClassPathXmlApplicationContext appCtx = new ClassPathXmlApplicationContext("spring-redis.xml");
    final RedisTemplate redisTemplate = appCtx.getBean("redisTemplate",RedisTemplate.class);
    //添加一个 key 
    ValueOperations value = redisTemplate.opsForValue();
    value.set("lp", "hello word");
    //获取 这个 key 的值
    System.out.println(value.get("lp"));
    //添加 一个 hash集合
    HashOperations hash = redisTemplate.opsForHash();
    Map map = new HashMap();
    map.put("name", "lp");
    map.put("age", "26");
    hash.putAll("lpMap", map);
    //获取 map
    System.out.println(hash.entries("lpMap"));
    //添加 一个 list 列表
    ListOperations list = redisTemplate.opsForList();
    list.rightPush("lpList", "lp");
    list.rightPush("lpList", "26");
    //输出 list
    System.out.println(list.range("lpList", 0, 1));
    //添加 一个 set 集合
    SetOperations set = redisTemplate.opsForSet();
    set.add("lpSet", "lp");
    set.add("lpSet", "26");
    set.add("lpSet", "178cm");
    //输出 set 集合
    System.out.println(set.members("lpSet"));
    //添加有序的 set 集合
    ZSetOperations zset = redisTemplate.opsForZSet();
    zset.add("lpZset", "lp", 0);
    zset.add("lpZset", "26", 1);
    zset.add("lpZset", "178cm", 2);
    //输出有序 set 集合
    System.out.println(zset.rangeByScore("lpZset", 0, 2));
  }

以上就是RedisTemplate方法如何在spring中使用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


本文标题:RedisTemplate方法如何在spring中使用
文章位置:http://sczitong.cn/article/jdeeph.html