Redis作为独立的服务端,在Java中去操作redis需要通过Redis提供的jar
z在这里特别申明,除了基本数据以及String外,其余对象,要想写入redis,必须序列化(即使是对象也要实现序列化接口),且读取redis中的数据也需要反序列化
不懂序列化的请自行百度
下面是Demo代码
Util类
package com.redis.test.util; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.redis.test.module.User; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class RedisTool { // Redis服务器地址 private static String address = "127.0.0.1"; // Redis服务器端口号 private static int port = 6379; private static Jedis jedis; public static void main(String[] args) { RedisTool redisTool = new RedisTool(); jedis = new Jedis(RedisTool.address, RedisTool.port); System.out.println("连接成功"); redisTool.saveMap(); } /** * 存储数组 */ public void saveList() { List<User> list = new ArrayList<User>(); User user1 = new User(); user1.setName("张三"); user1.setPassword("123456"); user1.setAddress("无锡市学前东路12号"); user1.setAge(23); list.add(user1); User user2 = new User(); user2.setName("李四"); user2.setPassword("123456"); user2.setAddress("无锡市仁爱路89号"); user2.setAge(18); list.add(user2); jedis.set(("userList").getBytes(), SerializeUtil.serialize(list)); byte[] userObjs = jedis.get(("userList").getBytes()); List<Object> userList = (List<Object>) SerializeUtil.unserialize(userObjs); System.out.println(userList.size()); } /** * 存储对象 */ public void saveObject() { User user = new User(); user.setName("张三"); user.setPassword("123456"); user.setAddress("无锡市学前东路12号"); user.setAge(23); jedis.set("user".getBytes(), SerializeUtil.serialize(user)); System.out.println("Jedis中存储的字符串为:" + jedis.get("test")); byte[] userObj = jedis.get(("user").getBytes()); user = (User) SerializeUtil.unserialize(userObj); System.out.println(user.toString()); } /** * 存储Map */ public void saveMap() { Map<String , String> map = new HashMap<String, String>(); map.put("name", "你好"); jedis.set("map".getBytes(), SerializeUtil.serialize(map)); byte[] mapObj = jedis.get(("map").getBytes()); Map newMap = (Map) SerializeUtil.unserialize(mapObj); System.out.println(newMap.get("name").toString()); } }
序列化工具类:
package com.redis.test.util; 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; public class SerializeUtil { public static byte[] serialize(Object object) { ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; byte[] bytes = null; if(null!=object){ try { baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); bytes = baos.toByteArray(); } catch (Exception e) { e.printStackTrace(); } } return bytes; } 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) { e.printStackTrace(); } finally { close(bais); } return null; } /** * 列表序列化(用于Redis整存整取) * @param value * @return */ public static <T> byte[] serializeList(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 obj : value){ os.writeObject(obj); } os.writeObject(null); os.close(); bos.close(); rv = bos.toByteArray(); } catch (IOException e) { e.printStackTrace(); } finally { close(os); close(bos); } return rv; } /** * 反序列化列表(用于Redis整存整取) * @param in * @return */ public static <T> List<T> unserializeForList(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 obj = (T) is.readObject(); if(obj == null){ break; }else{ list.add(obj); } } is.close(); bis.close(); } } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { close(is); close(bis); } return list; } /** * 关闭的数据源或目标。调用 close()方法可释放对象保存的资源(如打开文件) * 关闭此流并释放与此流关联的所有系统资源。如果已经关闭该流,则调用此方法无效。 * @param closeable */ public static void close(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (Exception e) { e.printStackTrace(); } } } }
Module类
package com.redis.test.module; import java.io.Serializable; public class User implements Serializable{ private String name; private String password; private String address; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String toString(){ return "姓名:"+this.getName()+";"+"年龄:"+this.getAge()+";"+"地址:"+this.getAddress()+";"; } }