您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)RedisTemplate方法如何在spring中使用,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
需要的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 自己實(shí)現(xiàn)了一個(gè)資源池。 配置參數(shù) 詳見 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); //添加一個(gè) key ValueOperations<String, Object> value = redisTemplate.opsForValue(); value.set("lp", "hello word"); //獲取 這個(gè) key 的值 System.out.println(value.get("lp")); //添加 一個(gè) 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")); //添加 一個(gè) list 列表 ListOperations<String, Object> list = redisTemplate.opsForList(); list.rightPush("lpList", "lp"); list.rightPush("lpList", "26"); //輸出 list System.out.println(list.range("lpList", 0, 1)); //添加 一個(gè) 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)); }
以上就是RedisTemplate方法如何在spring中使用,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。