NoSQL學習:Redis與Spring集成:RedisTemplate

NoSQL學習:Redis與Spring集成:RedisTemplate

1、Redis與Spring集成簡介

通過此文,可以掌握如下功能點:1)spring和redis集成後,配置文件如何配置2)學會spring提供的直接操作redis的模板類RedisTemplate的常用操作,此模板類可以代替原來redis的自帶的jedis的相關操作。RedisTemplate封裝了很多redis的基本操作,使用起來更方便。

2、Redis與Spring集成的編碼核心步驟

1)環境前期準備

2)創建基於maven的工程springredis

3)配置spring的spring-redis.xml文件主要包含如下幾點:

  • 配置spring跟redis的集成信息
  • 配置redis的相關連接信息
  • 配置spring提供的模板類RedisTemplate的bean

4)創建測試類TestRedisTemplate,此測試類共展示了RedisTemplate對redis的key、hash、List、set、sortset等數據類型的基本插入和查詢操作。

5)測試結果展示

3、Redis與Spring集成的具體編碼

1)環境前期準備

已裝好redis,參考之前的redis安裝章節

2)創建基於maven的工程springredis

第一步:基於Maven創建工程springredis

第二步:配置工程依賴的jar包,在pom.xml中:

 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0

com.gongyunit
springredis
0.0.1-SNAPSHOT
jar

springredis
http://maven.apache.org


UTF-8






org.springframework
spring-context
4.2.8.RELEASE


org.springframework.data
spring-data-redis
1.7.4.RELEASE



redis.clients
jedis
2.8.1



junit
junit
3.8.1
test



3)配置spring的spring-redis.xml文件主要包含如下幾點:

配置spring跟redis的集成信息

配置redis的相關連接信息

配置spring提供的模板類RedisTemplate的bean

具體操作如下:

第一步:創建文件夾src/main/resources

第二步:在該文件夾下創建包:config

第三步: 編寫文件spring-redis.xml

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org
/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org

/schema/beans
http://www.springframework.org/schema/
beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
default-autowire="byName" default-lazy-init="false">



class="org.springframework.beans.factory.config.
PropertyPlaceholderConfigurer">





classpath*:all.properties

















class="org.springframework.data.redis.
connection.jedis.JedisConnectionFactory">







class="org.springframework.data.redis.core.RedisTemplate">















4)創建測試類TestRedisTemplate,

此測試類共展示了RedisTemplate對redis的key、hash、List、set、sortset等數據類型的基本插入和查詢操作。

第一步:創建包:com.gy.springredis.test

第二步:創建類:TestRedisTemplate類:

package com.gy.springredis.test;

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.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;

public class TestRedisTemplate {
public static void main(String[] args) {
ClassPathXmlApplicationContext appCtx =
new ClassPathXmlApplicationContext("config/spring-redis.xml");
final RedisTemplate redisTemplate = appCtx.getBean("redisTemplate",RedisTemplate.class);

//添加一個 key
ValueOperations value = redisTemplate.opsForValue();

value.set("tkey", "hello key");
//獲取 這個 key 的值
System.out .println(value.get("tkey"));


//添加 一個 hash集合
HashOperations hash = redisTemplate.opsForHash();
Map map = new HashMap();
map.put("name", "thashmap");
map.put("age", "99");
hash.putAll("thashmap", map);
//獲取 map
System.out .println(hash.entries("thashmap"));


//添加 一個 list 列表
ListOperations list = redisTemplate.opsForList();
list.rightPush("tlist", "list1");
list.rightPush("tlist", "list2");
//輸出 list
System.out .println(list.range("tlist", 0, 1));


//添加 一個 set 集合
SetOperations set = redisTemplate.opsForSet();
set.add("tset", "set1");
set.add("tset", "set2");
set.add("tset", "set3");
//輸出 set 集合
System.out .println(set.members("tset"));


//添加有序的 set 集合
ZSetOperations zset = redisTemplate.opsForZSet();
zset.add("tzset", "zset1", 0);
zset.add("tzset", "zset2", 1);
zset.add("tzset", "zset3", 2);
//輸出有序 set 集合
System.out .println(zset.rangeByScore("tzset", 0, 2));
}

}

5)測試結果展示

第一步:展示創建好的工程結構:

NoSQL學習:Redis與Spring集成:RedisTemplate


第二步:展示測試類的測試結果:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".SLF4J: Defaulting to no-operation (NOP) logger implementationSLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.hello key{age=99, name=thashmap}[list1, list2][set2, set3, set1][zset1, zset2, zset3]


分享到:


相關文章: