redis能存map类型么,redis获取map值
//RedisCustom value = (RedisCustom)redisTemplate.opsForHash().get("redisTest","小明"); return ResponseData.success(resultMap); }}结果
参考https:///article/246815.htm
方法2°
将对象转成byte[]
序列化及反序列化工具类
import java.io.*;/** * 序列化及反序列化工具类 */public class SerializeObjectTool { //序列化 public static byte[] serialize(Object obj) { ObjectOutputStream obi = null; ByteArrayOutputStream bai = null; try { bai = new ByteArrayOutputStream(); obi = new ObjectOutputStream(bai); obi.writeObject(obj); byte[] byt = bai.toByteArray(); return byt; } catch (IOException e) { e.printStackTrace(); } return null; } // 反序列化 public static Object unserizlize(byte[] byt) { ObjectInputStream oii = null; ByteArrayInputStream bis = null; bis = new ByteArrayInputStream(byt); try { oii = new ObjectInputStream(bis); Object obj = oii.readObject(); return obj; } catch (Exception e) { e.printStackTrace(); } return null; }}
示例代码
@Controller@RequestMapping("/redis")public class RedisController { /** * @param * @return */ @RequestMapping(value = "/setRedisData", method = RequestMethod.GET) @ResponseBody public Map<String, Object> setRedisData() { RedisCustom redis1 = new RedisCustom(); redis1.setName("小明"); redis1.setAge(12); redis1.setStuCode("36"); RedisCustom redis2 = new RedisCustom(); redis2.setName("小红"); redis2.setAge(11); redis2.setStuCode("24"); //构造存入redis中的map Map<String, RedisCustom> redisDataMap = new HashMap<String, RedisCustom>(); redisDataMap.put(redis1.getName(), redis1); redisDataMap.put(redis2.getName(), redis2); //连接redis Jedis redis = new Jedis("xx.xx.xxx.xx", 6379); redis.auth("xxxxxxxxxxx"); //存 byte[] personByte = SerializeObjectTool.serialize(redisDataMap); redis.set("redisData".getBytes(), personByte); //取 byte[] byt = redis.get("redisData".getBytes()); Object obj = SerializeObjectTool.unserizlize(byt); Map<String, RedisCustom> redisData = (Map<String, RedisCustom>) obj; return ResponseData.success(redisData); }}
参考https://blog.csdn.net/chris_111x/article/details/85236458
到此这篇关于Java redis存Map对象类型数据的实现的文章就介绍到这了,更多相关Java redis存Map对象类型内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。