1.引入jedis-2.7.3.jar
2.在spring配置文件中添加配置(前提要安装redis)
<bean id="redisUserDao" class="com.cn.kong.redis.RedisUserDaoImpl"/>
<!-- redis配置开始 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="300" />
<property name="maxActive" value="600" />
<property name="maxWait" value="1000" />
<property name="testOnBorrow" value="true" />
</bean>
<bean id="jedis.shardInfo" class="redis.clients.jedis.JedisShardInfo" >
<constructor-arg index="0" value="127.0.0.1" />
<constructor-arg index="1" value="6379" />
</bean>
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<ref bean="jedis.shardInfo" />
</list>
</constructor-arg>
</bean>
<!-- redis配置结束 -->
3.新建接口
RedisUserDao
package com.cn.kong.redis;
public interface RedisUserDao {
//存储简单字符串
void set(String key, String value);
//获取简单字符串
String getStr(String key);
void setBytes(byte[] keyBytes, byte[] valueByte);
byte[] getBytes(byte[] keyBytes);
}
4.新建实现类
package com.cn.kong.redis;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
public class RedisUserDaoImpl implements RedisUserDao {
@Autowired
private ShardedJedisPool shardedJedisPool;
public String getStr(String key) {
ShardedJedis jedis = shardedJedisPool.getResource();
return jedis.get(key);
}
public void set(String key, String value) {
ShardedJedis jedis = shardedJedisPool.getResource();
jedis.set(key, value);
}
public void setBytes(byte[] keyBytes, byte[] valueByte) {
ShardedJedis jedis = shardedJedisPool.getResource();
jedis.set(keyBytes, valueByte);
}
public byte[] getBytes(byte[] keyBytes) {
ShardedJedis jedis = shardedJedisPool.getResource();
return jedis.get(keyBytes);
}
}
5.测试
@Autowired
private RedisUserDao redisUserDao;
//redis测试
List<ExShop> exShopList = exShopMapper.selectByExample(example);
redisUserDao.setBytes("exShopList".getBytes(), ObjectsTranscoder.serializ(exShopList));
//验证
byte[] in = redisUserDao.getBytes("exShopList".getBytes());
List<ExShop> list = ObjectsTranscoder.deserialize(in);
for(ExShop shop : list){
System.out.println("testSetEnsemble user name is:" + shop.getShopName());
}
mv.addObject("exShopList", exShopList);
由于redis没有提供存入list,要将查询出来的list序列化,序列化的类如下
package com.cn.kong.redis;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import com.cn.kong.bean.User;
public class ObjectsTranscoder {
public static <T> byte[] serializ(List<T> value) {
if (value == null) {
throw new NullPointerException("Can't serialize null");
}
byte[] rv=null;
ByteArrayOutputStream bos = null;
ObjectOutputStream os = null;
try {
bos = new ByteArrayOutputStream();
os = new ObjectOutputStream(bos);
for(T t : value){
os.writeObject(t);
}
os.writeObject(null);
os.close();
bos.close();
rv = bos.toByteArray();
} catch (IOException e) {
throw new IllegalArgumentException("Non-serializable object", e);
} finally {
close(os);
close(bos);
}
return rv;
}
public static <T> List<T> deserialize(byte[] in) {
List<T> list = new ArrayList<T>();
ByteArrayInputStream bis = null;
ObjectInputStream is = null;
try {
if(in != null) {
bis=new ByteArrayInputStream(in);
is=new ObjectInputStream(bis);
while (true) {
T t = (T) is.readObject();
if(t == null){
break;
}else{
list.add(t);
}
}
is.close();
bis.close();
}
} catch (IOException e) {
} catch (ClassNotFoundException e) {
} finally {
/*CloseUtil.close(is);
CloseUtil.close(bis); */
}
return list;
}
public static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (Exception e) {
}
}
}
}