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

发布时间 - 2026-01-11 01:24:42    点击率:

需要的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 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Laravel怎么实现支付功能_Laravel集成支付宝微信支付  如何为不同团队 ID 动态生成多个独立按钮  购物网站制作费用多少,开办网上购物网站,需要办理哪些手续?  香港服务器如何优化才能显著提升网站加载速度?  胶州企业网站制作公司,青岛石头网络科技有限公司怎么样?  Laravel如何设置自定义的日志文件名_Laravel根据日期或用户ID生成动态日志【技巧】  如何基于PHP生成高效IDC网络公司建站源码?  Laravel怎么清理缓存_Laravel optimize clear命令详解  Python正则表达式进阶教程_复杂匹配与分组替换解析  Laravel怎么集成Log日志记录_Laravel单文件与每日日志配置及自定义通道【详解】  米侠浏览器网页图片不显示怎么办 米侠图片加载修复  Laravel怎么配置自定义表前缀_Laravel数据库迁移与Eloquent表名映射【步骤】  HTML透明颜色代码怎么让下拉菜单透明_下拉菜单透明背景指南【技巧】  百度输入法全感官ai怎么关 百度输入法全感官皮肤关闭  Laravel如何使用软删除(Soft Deletes)功能_Eloquent软删除与数据恢复方法  实例解析angularjs的filter过滤器  简单实现jsp分页  Laravel如何创建自定义Artisan命令?(代码示例)  Swift中循环语句中的转移语句 break 和 continue  Python企业级消息系统教程_KafkaRabbitMQ高并发应用  Laravel如何使用查询构建器?(Query Builder高级用法)  如何快速重置建站主机并恢复默认配置?  今日头条AI怎样推荐抢票工具_今日头条AI抢票工具推荐算法与筛选【技巧】  详解Android图表 MPAndroidChart折线图  Laravel如何处理和验证JSON类型的数据库字段  java中使用zxing批量生成二维码立牌  潮流网站制作头像软件下载,适合母子的网名有哪些?  Laravel如何实现模型的全局作用域?(Global Scope示例)  如何在 Python 中将列表项按字母顺序编号(a.、b.、c. …)  太平洋网站制作公司,网络用语太平洋是什么意思?  简单实现Android验证码  百度浏览器如何管理插件 百度浏览器插件管理方法  原生JS实现图片轮播切换效果  香港服务器建站指南:免备案优势与SEO优化技巧全解析  如何用花生壳三步快速搭建专属网站?  常州企业网站制作公司,全国继续教育网怎么登录?  HTML5空格和margin有啥区别_空格与外边距的使用场景【说明】  如何快速搭建高效服务器建站系统?  Android中AutoCompleteTextView自动提示  Win11怎么查看显卡温度 Win11任务管理器查看GPU温度【技巧】  Laravel如何使用Guzzle调用外部接口_Laravel发起HTTP请求与JSON数据解析【详解】  Laravel模型关联查询教程_Laravel Eloquent一对多关联写法  如何在服务器上三步完成建站并提升流量?  标题:Vue + Vuex + JWT 身份认证的正确实践与常见误区解析  邀请函制作网站有哪些,有没有做年会邀请函的网站啊?在线制作,模板很多的那种?  如何用wdcp快速搭建高效网站?  Python进程池调度策略_任务分发说明【指导】  Laravel如何实现数据库事务?(DB Facade示例)  如何实现建站之星域名转发设置?  Laravel怎么实现验证码功能_Laravel集成验证码库防止机器人注册