spring整合redis以及使用RedisTemplate的方法

 更新时间:2017年05月27日 09:47:14   作者:梁鹏的博客  
本篇文章主要介绍了spring整合redis以及使用RedisTemplate的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

需要的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 配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/util 
   http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<!--[redis-JedisPoolConfig配置](http://blog.csdn.net/liang_love_java/article/details/50510753)-->
<!--  jedis-2.7.2.jar 依赖jar包 commons-pool2-2.3.jar 
    jedis基于 commons-pool2-2.3.jar 自己实现了一个资源池。
    配置参数 详见 http://blog.csdn.net/liang_love_java/article/details/50510753
-->
  <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> 
    <property name="maxIdle" value="1" /> 
    <property name="maxTotal" value="5" /> 
    <property name="blockWhenExhausted" value="true" /> 
    <property name="maxWaitMillis" value="30000" /> 
    <property name="testOnBorrow" value="true" /> 
  </bean> 

  <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> 
    <property name="hostName" value="10.1.8.200" /> 
    <property name="port" value="6379"/> 
    <property name="poolConfig" ref="jedisPoolConfig" /> 
    <property name="usePool" value="true"/> 
  </bean> 

  <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
    <property name="connectionFactory"  ref="jedisConnectionFactory" />  
    <property name="keySerializer">  
      <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
    </property>   
    <property name="valueSerializer">  
      <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  
    </property>  
    <property name="hashKeySerializer">   
      <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>   
    </property>  
    <property name="hashValueSerializer">  
      <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>   
    </property> 
   </bean> 

</beans>

测试代码

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<String, Object> redisTemplate = appCtx.getBean("redisTemplate",RedisTemplate.class);
    //添加一个 key 
    ValueOperations<String, Object> value = redisTemplate.opsForValue();
    value.set("lp", "hello word");
    //获取 这个 key 的值
    System.out.println(value.get("lp"));
    //添加 一个 hash集合
    HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
    Map<String,Object> map = new HashMap<String,Object>();
    map.put("name", "lp");
    map.put("age", "26");
    hash.putAll("lpMap", map);
    //获取 map
    System.out.println(hash.entries("lpMap"));
    //添加 一个 list 列表
    ListOperations<String, Object> list = redisTemplate.opsForList();
    list.rightPush("lpList", "lp");
    list.rightPush("lpList", "26");
    //输出 list
    System.out.println(list.range("lpList", 0, 1));
    //添加 一个 set 集合
    SetOperations<String, Object> set = redisTemplate.opsForSet();
    set.add("lpSet", "lp");
    set.add("lpSet", "26");
    set.add("lpSet", "178cm");
    //输出 set 集合
    System.out.println(set.members("lpSet"));
    //添加有序的 set 集合
    ZSetOperations<String, Object> 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));
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 解决BeanUtils.copyProperties之大坑

    解决BeanUtils.copyProperties之大坑

    这篇文章主要介绍了解决BeanUtils.copyProperties之大坑,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • java实现简单的推箱子小游戏

    java实现简单的推箱子小游戏

    这篇文章主要为大家详细介绍了java实现简单的推箱子小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-05-05
  • Java常用的一些多媒体文件基本操作方法简介

    Java常用的一些多媒体文件基本操作方法简介

    这篇文章主要介绍了Java常用的一些多媒体文件基本操作方法,包括对音频视频以及幻灯片的播放,需要的朋友可以参考下
    2015-10-10
  • SpringCloud Nacos作为配置中心超详细讲解

    SpringCloud Nacos作为配置中心超详细讲解

    这篇文章主要介绍了Springcloud中的Nacos作为配置中心,本文以用户微服务为例,进行统一的配置,结合实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-12-12
  • Java中EasyExcel使用自定义Converter处理方法详解

    Java中EasyExcel使用自定义Converter处理方法详解

    EasyExcel自定义Converter是指在使用EasyExcel进行Excel读写操作时,可以自定义转换器来处理一些不支持的数据类型,这篇文章主要给大家介绍了关于Java中EasyExcel使用自定义Converter处理的相关资料,需要的朋友可以参考下
    2024-08-08
  • 使用RequestBodyAdvice实现对Http请求非法字符过滤

    使用RequestBodyAdvice实现对Http请求非法字符过滤

    这篇文章主要介绍了使用RequestBodyAdvice实现对Http请求非法字符过滤的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • Centos中yum方式安装java的实现示例

    Centos中yum方式安装java的实现示例

    这篇文章主要介绍了Centos中yum方式安装java的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • Spring @Bean vs @Service注解区别

    Spring @Bean vs @Service注解区别

    本篇文章主要介绍了Spring @Bean vs @Service注解区别,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • MyBatis注解开发-@Insert和@InsertProvider的使用

    MyBatis注解开发-@Insert和@InsertProvider的使用

    这篇文章主要介绍了MyBatis注解开发-@Insert和@InsertProvider的使用,具有很好的参考价值,希望对大家有所帮助。
    2022-07-07
  • Java实现双链表互相交换任意两个节点的方法示例

    Java实现双链表互相交换任意两个节点的方法示例

    这篇文章主要介绍了Java实现双链表互相交换任意两个节点的方法,简单讲述了双链表的概念,并结合实例形式给出了java双链表实现任意两个节点交换的操作技巧,需要的朋友可以参考下
    2017-11-11

最新评论