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

网友投稿 322 2023-05-12


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 配置文件

xmlns:context="http://springframework.org/schema/context" xmlns:p="http://springframework.org/schema/p"

xmlns:mvc="http://springframework.org/schema/mvc" xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:tx="http://springframework.org/schema/tx" xmlns:util="http://springframework.org/schema/util"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc

http://springframework.org/dRtTOrRschema/mvc/spring-mvc-3.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util-3.0.xsd">

dRtTOrR

dRtTOrR

xmlns:context="http://springframework.org/schema/context" xmlns:p="http://springframework.org/schema/p"

xmlns:mvc="http://springframework.org/schema/mvc" xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:tx="http://springframework.org/schema/tx" xmlns:util="http://springframework.org/schema/util"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc

http://springframework.org/dRtTOrRschema/mvc/spring-mvc-3.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util-3.0.xsd">

dRtTOrR

dRtTOrR

测试代码

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 redisTemplate = appCtx.getBean("redisTemplate",RedisTemplate.class);

//添加一个 key

ValueOperations value = redisTemplate.opsForValue();

value.set("lp", "hello word");

//获取 这个 key 的值

System.out.println(value.get("lp"));

//添加 一个 hash集合

HashOperations hash = redisTemplate.opsForHash();

Map map = new HashMap();

map.put("name", "lp");

map.put("age", "26");

hash.putAll("lpMap", map);

//获取 map

System.out.println(hash.entries("lpMap"));

//添加 一个 list 列表

ListOperations list = redisTemplate.opsForList();

list.rightPush("lpList", "lp");

list.rightPush("lpList", "26");

//输出 list

System.out.println(list.range("lpList", 0, 1));

//添加 一个 set 集合

SetOperations set = redisTemplate.opsForSet();

set.add("lpSet", "lp");

set.add("lpSet", "26");

set.add("lpSet", "178cm");

//输出 set 集合

System.out.println(set.members("lpSet"));

//添加有序的 set 集合

ZSetOperations 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));

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:BootStrap Table 后台数据绑定、特殊列处理、排序功能
下一篇:Cors实现java后端完全跨域实例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~