最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年05月27日 09:47:14   作者:梁鵬的博客  
本篇文章主要介紹了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)了一個資源池。
    配置參數(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);
    //添加一個 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));
  }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解決BeanUtils.copyProperties之大坑

    解決BeanUtils.copyProperties之大坑

    這篇文章主要介紹了解決BeanUtils.copyProperties之大坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • java實(shí)現(xiàn)簡單的推箱子小游戲

    java實(shí)現(xiàn)簡單的推箱子小游戲

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡單的推箱子小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Java常用的一些多媒體文件基本操作方法簡介

    Java常用的一些多媒體文件基本操作方法簡介

    這篇文章主要介紹了Java常用的一些多媒體文件基本操作方法,包括對音頻視頻以及幻燈片的播放,需要的朋友可以參考下
    2015-10-10
  • SpringCloud Nacos作為配置中心超詳細(xì)講解

    SpringCloud Nacos作為配置中心超詳細(xì)講解

    這篇文章主要介紹了Springcloud中的Nacos作為配置中心,本文以用戶微服務(wù)為例,進(jìn)行統(tǒng)一的配置,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • Java中EasyExcel使用自定義Converter處理方法詳解

    Java中EasyExcel使用自定義Converter處理方法詳解

    EasyExcel自定義Converter是指在使用EasyExcel進(jìn)行Excel讀寫操作時,可以自定義轉(zhuǎn)換器來處理一些不支持的數(shù)據(jù)類型,這篇文章主要給大家介紹了關(guān)于Java中EasyExcel使用自定義Converter處理的相關(guān)資料,需要的朋友可以參考下
    2024-08-08
  • 使用RequestBodyAdvice實(shí)現(xiàn)對Http請求非法字符過濾

    使用RequestBodyAdvice實(shí)現(xiàn)對Http請求非法字符過濾

    這篇文章主要介紹了使用RequestBodyAdvice實(shí)現(xiàn)對Http請求非法字符過濾的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Centos中yum方式安裝java的實(shí)現(xiàn)示例

    Centos中yum方式安裝java的實(shí)現(xiàn)示例

    這篇文章主要介紹了Centos中yum方式安裝java的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Spring @Bean vs @Service注解區(qū)別

    Spring @Bean vs @Service注解區(qū)別

    本篇文章主要介紹了Spring @Bean vs @Service注解區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • MyBatis注解開發(fā)-@Insert和@InsertProvider的使用

    MyBatis注解開發(fā)-@Insert和@InsertProvider的使用

    這篇文章主要介紹了MyBatis注解開發(fā)-@Insert和@InsertProvider的使用,具有很好的參考價值,希望對大家有所幫助。
    2022-07-07
  • Java實(shí)現(xiàn)雙鏈表互相交換任意兩個節(jié)點(diǎn)的方法示例

    Java實(shí)現(xiàn)雙鏈表互相交換任意兩個節(jié)點(diǎn)的方法示例

    這篇文章主要介紹了Java實(shí)現(xiàn)雙鏈表互相交換任意兩個節(jié)點(diǎn)的方法,簡單講述了雙鏈表的概念,并結(jié)合實(shí)例形式給出了java雙鏈表實(shí)現(xiàn)任意兩個節(jié)點(diǎn)交換的操作技巧,需要的朋友可以參考下
    2017-11-11

最新評論

连山| 昌黎县| 绵阳市| 康乐县| 桐梓县| 巩义市| 竹北市| 田东县| 泗阳县| 靖江市| 鄂州市| 长治县| 贵阳市| 那曲县| 凉城县| 光山县| 赤水市| 沙坪坝区| 华容县| 平昌县| 康保县| 莱州市| 禹城市| 三门县| 台前县| 中超| 三原县| 波密县| 凤冈县| 榆中县| 延安市| 桐城市| 克东县| 玉溪市| 楚雄市| 吉林省| 柳江县| 宁安市| 临汾市| 黄浦区| 全州县|