环境的搭建参见:http://www.cnblogs.com/yangzhilong/p/4729857.html

下面直接贴具体的测试代码:

 package com.yzl;

 import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.SerializationUtils; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; /**
* RedisApp的测试类
*
* @author yangzhilong
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
public class RedisApp4Test {
private JedisPool pool;
private ApplicationContext app;
private Jedis jedis; @Before
public void before(){
app = new ClassPathXmlApplicationContext("spring-config.xml");
pool = app.getBean(JedisPool.class);
jedis = pool.getResource();
} @Test
public void test(){
//string
operationString(); //map
operationMap(); //list
operationList(); //自定义对象
operationMyObject();
} /**
*
* 功能描述: <br>
* 操作String
*
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
private void operationString(){
jedis.set("name", "hello");
jedis.append("name", " redis");
String value = jedis.get("name");
System.out.println("get value :" + value);
} /**
*
* 功能描述: <br>
* 操作Map
*
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
private void operationMap(){
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
jedis.hmset("map", map);
//这里可以一次读取多个key
List<String> result = jedis.hmget("map", "key1" , "key2");
for(String str : result){
System.out.println("get value from map : " + str);
}
} /**
*
* 功能描述: <br>
* 操作List
*
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
private void operationList(){
List<String> result = Arrays.asList("item1","item2");
for(String str : result){
jedis.lpush("list", str);
}
jedis.lpush("list", "value3");
result = jedis.lrange("list", 0, -1);
for(String str : result){
System.out.println("get value from list : " + str);
}
jedis.lpop("list");
for(String str : result){
System.out.println("get value from list again : " + str);
}
} /**
*
* 功能描述: <br>
* 操作自定义对象
*
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
private void operationMyObject(){
Entity entity = new Entity();
entity.setName("test");
entity.setAge(20);
//保存
jedis.set("obj".getBytes(), SerializationUtils.serialize(entity));
//读取
byte[] bytes = jedis.get("obj".getBytes());
Entity ret = (Entity)SerializationUtils.deserialize(bytes);
System.out.println("读取到自定义的类的属性name的值为:" + ret.getName());
} @After
public void after(){
//释放连接
pool.returnResourceObject(jedis);
System.out.println("end~~~");
}
}

运行的结果:

 get value :hello redis
get value from map : value1
get value from map : value2
get value from list : value3
get value from list : item2
get value from list : item1
get value from list again : value3
get value from list again : item2
get value from list again : item1
读取到自定义的类的属性name的值为:test
end~~~

下一篇:8、redis之事务1-redis命令

7、redis之使用spring集成commons-pool来操作常见数据类型的更多相关文章

  1. 【redis】3.Spring 集成注解 redis 项目配置使用

    spring-data-redis  项目,配合 spring 特性并集成 Jedis 的一些命令和方法. 配置redis继承到spring管理项目,使用注解实现redis缓存功能. 参考:http: ...

  2. 5、redis之使用spring集成commons-pool

    添加spring的依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://w ...

  3. spring集成Junit做单元测试及常见异常解决办法

    spring-test依赖包 <!--Spring-test --> <!-- https://mvnrepository.com/artifact/org.springframew ...

  4. spring 集成redis客户端jedis(java)

    spring集成jedis简单实例   jedis是redis的java客户端,spring将redis连接池作为一个bean配置. “redis.clients.jedis.JedisPool”,这 ...

  5. spring 集成 redis -- pub/sub

    redis除了常用的当做缓存外,还可以当做简单的消息中间件,实现消息发布订阅 spring集成redis,可以使用spring-data-redis 首先引入相关maven依赖(此处我spring相关 ...

  6. Spring集成Redis方案(spring-data-redis)(基于Jedis的单机模式)(待实践)

    说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...

  7. Redis篇之操作、lettuce客户端、Spring集成以及Spring Boot配置

    Redis篇之操作.lettuce客户端.Spring集成以及Spring Boot配置 目录 一.Redis简介 1.1 数据结构的操作 1.2 重要概念分析 二.Redis客户端 2.1 简介 2 ...

  8. Spring集成Redis集群(含spring集成redis代码)

    代码地址如下:http://www.demodashi.com/demo/11458.html 一.准备工作 安装 Redis 集群 安装参考: http://blog.csdn.net/zk6738 ...

  9. Redis 与 Spring 集成

    配置applicationContext.xml <!-- 连接池配置 --> <bean id="jedisPoolConfig" class="re ...

随机推荐

  1. AutoCppHeader AutoHeader 自动根据CPP 或C文件 来生成头文件。

    根据 cpp文件 生成相应的头文件 namespace 声明类定义声明 函数声明 extern 变量声明 选项--------------//[AutoHeader Public] //[AutoHe ...

  2. SharePoint 2016 IT Preview的新feature列表

    大致看了一下, 其中关于存储的部分貌似没有. 感觉Hybrid in SharePoint 2016未来可以做的事情比较多吧, 必须赶紧弄个cloud的东西开始玩玩了. Search的部署的更新, 以 ...

  3. iOS开发-View中frame和bounds区别

    开发中调整View的时候的经常会遇到frame和bounds,刚开始看的时候不是很清楚,不过看了一下官方文档,frame是确定视图在父视图中的位置,和本身的大小,bounds确定可以确定子视图在当前视 ...

  4. ajax hash缓存

    hash 模拟url路由 function hashdone(){        var hash;        hash=(!window.location.hash)?"#one&qu ...

  5. 在Android上山寨了一个Ios9的LivePhotos,放Github上了

    9月10号的凌晨上演了一场IT界的春晚,相信很多果粉(恩,如果你指坚果,那我也没办法了,是在下输了)都熬夜看了吧,看完打算去医院割肾了吧.在发布会上发布了游戏机 Apple TV,更大的砧板 Ipad ...

  6. mysql的sql分页函数limit使用 (转)

    http://www.cnblogs.com/beijingstruggle/p/5631603.html mysql的sql分页函数limit使用 My sql数据库最简单,是利用mysql的LIM ...

  7. THINKPHP 解决模块不存在时出现空页面的问题

    遇到的问题: 最近使用THINKCMF开发了一个企业网站,因为之前客户的域名变更过,然后就发现当某个模块不存在的时候就出现了空页面 在 THINKPHP论坛 上有人说在项目里添加一个EmptyActi ...

  8. sql server 向mysql前移数据-单引号问题

    sql server中的数据导出来 用两个单引号表示一个单引号,这样的格式可以录入到mysql中: 但是遇到特殊的中文字符,例如顿号等,不能正确的显示两个单引号: mysql导出来的数据用反斜线和一个 ...

  9. (转)总结使用Unity 3D优化游戏运行性能的经验

    http://www.199it.com/archives/147913.html 流畅的游戏玩法来自流畅的帧率,而我们即将推出的动作平台游戏<Shadow Blade>已经将在标准iPh ...

  10. How could I create a custom windows message?

    [问题] Our project is running on Windows CE 6.0 and is written in C++ . We have some problems with the ...