先给出配置,由于版本不同jedis的api不同,这里比较坑人,常常发生错误无从下手,如果是maven项目还好查看源码,如果是web项目那么就很麻烦,
redis.clients jedis 2.0.0 org.springframework.data spring-data-redis 1.0.0.RELEASE
整个目录结构,
然后给出本人利用spring-data-redis封装的redis操作工具,原理就是对象转byte[],然后利用spring-data-redis进行存储,所以save形式为key(String->byte[])-value(Object->byte[]),get形式为key(byte[]->String)-value(byte[]->Object)
package com.zhxjz.framework.util.redis;import java.io.Serializable;import org.springframework.dao.DataAccessException;import org.springframework.data.redis.connection.RedisConnection;import org.springframework.data.redis.core.RedisCallback;import org.springframework.data.redis.core.RedisTemplate;import com.zhxjz.framework.util.ApplicationContextUtil;import com.zhxjz.framework.util.common.SerializeUtil;public class SpringRedisUtil { @SuppressWarnings("unchecked") private static RedisTemplateredisTemplate = (RedisTemplate ) ApplicationContextUtil .getBean("redisTemplate"); public static void save(final String key, Object value) { final byte[] vbytes = SerializeUtil.serialize(value); redisTemplate.execute(new RedisCallback
其中用到ApplicationContextUtil和SerializeUtil另外2个工具,
package com.zhxjz.framework.util;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component;@Componentpublic class ApplicationContextUtil implements ApplicationContextAware { private static ApplicationContext context; @Override public void setApplicationContext(ApplicationContext context) throws BeansException { ApplicationContextUtil.context = context; } public static ApplicationContext getContext() { return context; } public static Object getBean(String beanName) { return context.getBean(beanName); }}
package com.zhxjz.framework.util.common;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class SerializeUtil { public static byte[] serialize(Object object) { ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; try { // 序列化 baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); byte[] bytes = baos.toByteArray(); return bytes; } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } public static Object unserialize(byte[] bytes) { ByteArrayInputStream bais = null; try { // 反序列化 bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); return ois.readObject(); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } }}
然后再给出SpringRedis.xml配置,
记得<import resource="classpath:/SpringRedis.xml" />
下面是redis.properties
#redis configredis.pool.maxActive=100redis.pool.maxIdle=20redis.pool.maxWait=1000redis.pool.testOnBorrow=trueredis.hostname=127.0.0.1redis.port=6379redis.password=
记得注册这个properties文件到PropertyPlaceholderConfigurer
用法:
写一个Controller进行测试:
package com.zhxjz.controller.testredis;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.zhxjz.framework.util.redis.SpringRedisUtil;import com.zhxjz.model.testredis.TESTREDIS;@Controller@RequestMapping("/testredis")public class TESTREDISController { @RequestMapping("/test.do") @ResponseBody public String test(TESTREDIS testredis) { SpringRedisUtil.save("mystr", testredis); return SpringRedisUtil.get("mystr", TESTREDIS.class).toString(); } }
最后把model都给出来
package com.zhxjz.model.testredis;public class TESTREDIS implements java.io.Serializable { private static final long serialVersionUID = 1L; String testStr; int testInt; boolean testBool; public String getTestStr() { return testStr; } public void setTestStr(String testStr) { this.testStr = testStr; } public int getTestInt() { return testInt; } public void setTestInt(int testInt) { this.testInt = testInt; } public boolean isTestBool() { return testBool; } public void setTestBool(boolean testBool) { this.testBool = testBool; } public String toString() { return this.testStr + "|" + this.testInt + "|" + this.testBool; }}
访问:
查看client:
注意:
由于model需要转换为byte[],这里要求model必须implements java.io.Serializable,否则会报错。