全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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));
  }

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


# spring  # RedisTemplate  # 整合redis  # redis与spring整合  # Spring学习笔记之RedisTemplate的配置与使用教程  # 解决spring中redistemplate不能用通配符keys查出相应Key的问题  # spring使用RedisTemplate操作Redis数据库  # Spring中RedisTemplate使用方法详解  # 配置文件  # 大家多多  # 实现了  # liang_love_java  # net  # csdn  # article  # blog  # details  # tx  # schemaLocation  # aop  # util  # JedisPoolConfig  # gt  # xsd  # bean  # testOnBorrow  # true  # maxWaitMillis 


相关文章: 阿里云网站搭建费用解析:服务器价格与建站成本优化指南  攀枝花网站建设,攀枝花营业执照网上怎么年审?  ,网页ppt怎么弄成自己的ppt?  如何将凡科建站内容保存为本地文件?  公众号网站制作网页,微信公众号怎么制作?  上海网站制作开发公司,上海买房比较好的网站有哪些?  广州网站制作的公司,现在专门做网站的公司有没有哪几家是比较好的,性价比高,模板也多的?  ,如何利用word制作宣传手册?  如何选择香港主机高效搭建外贸独立站?  微信推文制作网站有哪些,怎么做微信推文,急?  实惠建站价格推荐:2025年高性价比自助建站套餐解析  python的本地网站制作,如何创建本地站点?  如何做静态网页,sublimetext3.0制作静态网页?  如何用花生壳三步快速搭建专属网站?  如何快速生成ASP一键建站模板并优化安全性?  简历在线制作网站免费,免费下载个人简历的网站是哪些?  最好的网站制作公司,网购哪个网站口碑最好,推荐几个?谢谢?  微信小程序 input输入框控件详解及实例(多种示例)  c# Task.Yield 的作用是什么 它和Task.Delay(1)有区别吗  广州网站设计制作一条龙,广州巨网网络科技有限公司是干什么的?  建站之星会员如何解锁更多建站功能?  实例解析angularjs的filter过滤器  建站之星如何实现网站加密操作?  网站专业制作公司,网站编辑是做什么的?好做吗?工作前景如何?  JS中使用new Date(str)创建时间对象不兼容firefox和ie的解决方法(两种)  西安大型网站制作公司,西安招聘网站最好的是哪个?  制作公司内部网站有哪些,内网如何建网站?  建站ABC备案流程中有哪些关键注意事项?  如何在Golang中实现微服务服务拆分_Golang微服务拆分与接口管理方法  nginx修改上传文件大小限制的方法  宝塔新建站点为何无法访问?如何排查?  兔展官网 在线制作,怎样制作微信请帖?  建站之星安全性能如何?防护体系能否抵御黑客入侵?  Python lxml的etree和ElementTree有什么区别  高端企业智能建站程序:SEO优化与响应式模板定制开发  上海制作企业网站有哪些,上海有哪些网站可以让企业免费发布招聘信息?  招商网站制作流程,网站招商广告语?  弹幕视频网站制作教程下载,弹幕视频网站是什么意思?  建站之星安装模板失败:服务器环境不兼容?  网站设计制作企业有哪些,抖音官网主页怎么设置?  ,制作一个手机app网站要多少钱?  佛山企业网站制作公司有哪些,沟通100网上服务官网?  猪八戒网站制作视频,开发一个猪八戒网站,大约需要多少?或者自己请程序员,需要什么程序员,多少程序员能完成?  建站主机如何选?性能与价格怎样平衡?  购物网站制作公司有哪些,哪个购物网站比较好?  建站之星CMS建站配置指南:模板选择与SEO优化技巧  韩国代理服务器如何选?解析IP设置技巧与跨境访问优化指南  如何基于PHP生成高效IDC网络公司建站源码?  北京网页设计制作网站有哪些,继续教育自动播放怎么设置?  如何在自有机房高效搭建专业网站? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。