一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - spring整合redis以及使用RedisTemplate的方法

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

2020-10-31 23:40梁鵬的博客 Java教程

本篇文章主要介紹了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 配置文件

?
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
<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
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));
  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/liang_love_java/article/details/50497281

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 5g影院天天影院天天爽影院网站 | 国产综合成色在线视频 | 四虎最新网址在线观看 | 好男人免费高清在线观看2019 | 精品手机在线1卡二卡3卡四卡 | 亚洲欧美久久久久久久久久爽网站 | 大jjjj免费看视频 | 韩国帅男同gay网站 韩国三级在线播放 | 国产欧美精品一区二区三区–老狼 | 日韩免费在线视频观看 | 91在线精品视频 | 久久国产视频网站 | 香蕉久久一区二区三区啪啪 | 艹逼的视频| 亚洲天堂日韩在线 | 成人福利在线视频免费观看 | 久久亚洲成a人片 | 精品小视频在线 | 亚洲成熟人网站 | 亚洲 欧美 中文 日韩 另类 | 久久亚洲高清观看 | 大学生情侣在线 | 亚洲成熟人网站 | 日本卡1卡2卡4卡免费 | www.青青草原| 青青热久免费精品视频精品 | 亚洲欧美另类在线观看 | 白丝女榨干蹂躏我 | 蜜桃免费 | 日本色午夜 | 九九热视频免费观看 | 国产自产2023最新麻豆 | 精品一产品大全 | 亚洲天堂网站在线 | 国产欧美久久久精品影院 | 九九99亚洲精品久久久久 | 亚洲AV无码偷拍在线观看 | 国产精品国语自产拍在线观看 | 撕开老师的丝袜白丝扒开粉嫩的小 | 亚洲 欧美 中文 日韩 视频 | 天天排行网 |