spring 整合 redis,以及spring的RedisTemplate如何使用
来源:互联网 发布:加密软件破译系统 编辑:程序博客网 时间:2024/06/07 18:36
需要的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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
测试代码
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)); }
阅读全文
0 0
- spring 整合 redis,以及spring的RedisTemplate如何使用
- Spring 整合Redis以及Spring的RedisTemplate如何使用
- spring 整合 redis,以及spring的RedisTemplate如何使用
- spring 整合 redis,以及spring的RedisTemplate如何使用
- spring 整合 redis,以及spring的RedisTemplate如何使用
- spring 整合 redis,以及spring的RedisTemplate如何使用
- spring 整合 redis,以及spring的RedisTemplate如何使用
- spring与redis整合,redisTemplate的使用
- Redis使用二、redis整合spring(redisTemplate工具类)
- Spring-Data-Redis之RedisTemplate的使用
- Spring Data Redis(RedisTemplate的使用)
- Spring-Data-Redis之RedisTemplate的使用
- Spring集成Redis使用RedisTemplate
- redis整合spring(redisTemplate工具类)
- redis整合spring(redisTemplate工具类)
- redis整合spring(redisTemplate工具类)
- Spring整合redis哨兵及redisTemplate
- 关于spring整合redis之后使用RedisTemplate保存对象的问题。
- SQL Server监控
- java菜鸟之路
- 《TP5.0学习笔记---URL和路由篇》
- 大数据--一致性哈希算法
- SVG动态生成的子元素,同时绑定"click"和"dblclick".
- spring 整合 redis,以及spring的RedisTemplate如何使用
- golang中使用go时出现的的问题
- new/delete与malloc/free的区别是什么
- iOS工作小技巧及填坑记录
- Android 测试CTS,GTS目录及脚本整理
- Leetcode习题记录——Longest Common Prefix
- PAT甲级 1022
- springboot入门3-操作redis
- Meanshift,聚类算法