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 自己實(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>
測(cè)試代碼
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)); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在SpringBoot項(xiàng)目中利用maven的generate插件
今天小編就為大家分享一篇關(guān)于在SpringBoot項(xiàng)目中利用maven的generate插件,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01Java SpringBoot實(shí)現(xiàn)帶界面的代碼生成器詳解
這篇文章主要介紹了Java SpringBoot如何實(shí)現(xiàn)帶界面的代碼生成器,幫助大家更好的理解和使用Java SpringBoot編程語言,感興趣的朋友可以了解下2021-09-09Java實(shí)現(xiàn)經(jīng)典游戲復(fù)雜迷宮
這篇文章主要介紹了如何利用java語言實(shí)現(xiàn)經(jīng)典《復(fù)雜迷宮》游戲,文中采用了swing技術(shù)進(jìn)行了界面化處理,感興趣的小伙伴可以動(dòng)手試一試2022-02-02引入QQ郵箱發(fā)送驗(yàn)證碼進(jìn)行安全校驗(yàn)功能實(shí)現(xiàn)
最近遇到這樣的需求用戶輸入自己的郵箱,點(diǎn)擊獲取驗(yàn)證碼,后臺(tái)會(huì)發(fā)送一封郵件到對(duì)應(yīng)郵箱中,怎么實(shí)現(xiàn)呢?下面小編給大家?guī)砹艘隥Q郵箱發(fā)送驗(yàn)證碼進(jìn)行安全校驗(yàn)功能,需要的朋友可以參考下2023-02-02三行Java代碼實(shí)現(xiàn)計(jì)算多邊形的幾何中心點(diǎn)
因?yàn)楣ぷ餍枰?jì)算采煤機(jī)工作面的中心點(diǎn),如果套用數(shù)學(xué)的計(jì)算公式,用java去實(shí)現(xiàn),太麻煩了。本文將利用java幾何計(jì)算的工具包,幾行代碼就能求出多變形的中心,簡(jiǎn)直yyds!還不快跟隨小編一起學(xué)起來2022-10-10intellij idea 2021.2 打包并上傳運(yùn)行spring boot項(xiàng)目的詳細(xì)過程(spring boot 2
這篇文章主要介紹了intellij idea 2021.2 打包并上傳運(yùn)行一個(gè)spring boot項(xiàng)目(spring boot 2.5.4),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09Apache?SkyWalking?修復(fù)TTL?timer?失效bug詳解
這篇文章主要為大家介紹了Apache?SkyWalking?修復(fù)TTL?timer?失效bug詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09