欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-18 14:19:34
需要的jar包 spring-data-redis-1.6.2.release.jar jedis-2.7.2.jar(依赖 commons-pool2-2.3...

需要的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));
  }

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