加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 20:00:11 所属栏目:Java 来源:网络整理
导读:需要的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.springframewo

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

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

您可能感兴趣的文章:

  • Redis整合Spring结合使用缓存实例
  • Spring与Mybatis基于注解整合Redis的方法
  • Spring基于注解整合Redis完整实例
  • spring整合redis缓存并以注解(@Cacheable、@CachePut、@CacheEvict)形式使用
  • 详解redis与spring的整合(使用缓存)
  • Spring整合Redis完整实例代码
  • 详解java之redis篇(spring-data-redis整合)
  • redis与spring整合使用的步骤实例教程

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读