欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java redis存Map對象類型數(shù)據(jù)的實現(xiàn)

 更新時間:2022年05月13日 10:06:13   作者:Coo~  
本文主要介紹了Java redis存Map<String,RedisCustom>對象類型數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

背景描述

項目需要將設備采集到的最新經緯度信息存入redis緩存中,方便及時查詢檢索??紤]到根據(jù)檢索條件不同,所查詢的設備不同。采取將數(shù)據(jù)以map類型存入redis緩存,在此記錄一下。

實體類

注:一定要實現(xiàn)序列化接口

父類

public class Redis implements Serializable{

? ? private String name;
? ? private Integer age;

? ? public String getName() {
? ? ? ? return name;
? ? }
? ? public void setName(String name) {
? ? ? ? this.name = name;
? ? }
? ? public Integer getAge() {
? ? ? ? return age;
? ? }
? ? public void setAge(Integer age) {
? ? ? ? this.age = age;
? ? }
}

子類

import java.io.Serializable;

public class RedisCustom extends Redis {

? ? private String stuCode;

? ? public String getStuCode() {
? ? ? ? return stuCode;
? ? }
? ? public void setStuCode(String stuCode) {
? ? ? ? this.stuCode = stuCode;
? ? }
}

方法1°

redisTemplate.opsForHash()

示例代碼

@Controller
@RequestMapping("/redis")
public class RedisController {

? ? @Autowired
? ? private RedisTemplate redisTemplate;

? ? /**
? ? ?* @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
? ? ? ? redisTemplate.opsForHash().putAll("redisTest",redisDataMap);
? ? ? ? //獲取緩存內容
? ? ? ? Map<String,RedisCustom> resultMap = redisTemplate.opsForHash().entries("redisTest");
? ? ? ??
? ? ? ? //List<RedisCustom> reslutMapList = redisTemplate.opsForHash().values("redisTest");
? ? ? ? //Set<RedisCustom> resultMapSet = redisTemplate.opsForHash().keys("redisTest");
? ? ? ? //RedisCustom value = (RedisCustom)redisTemplate.opsForHash().get("redisTest","小明");
? ? ? ??
? ? ? ? return ResponseData.success(resultMap);
? ? }
}

結果

參考
http://www.dbjr.com.cn/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對象類型數(shù)據(jù)的實現(xiàn)的文章就介紹到這了,更多相關Java redis存Map對象類型內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • JAVA操作MongoDB數(shù)據(jù)庫實例教程

    JAVA操作MongoDB數(shù)據(jù)庫實例教程

    MongoDB是一個文檔型數(shù)據(jù)庫,是NOSQL家族中最重要的成員之一,下面這篇文章主要給大家介紹了關于JAVA操作MongoDB數(shù)據(jù)庫的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-05-05
  • Java基于SpringBoot和tk.mybatis實現(xiàn)事務讀寫分離代碼實例

    Java基于SpringBoot和tk.mybatis實現(xiàn)事務讀寫分離代碼實例

    這篇文章主要介紹了Java基于SpringBoot和tk.mybatis實現(xiàn)事務讀寫分離代碼實例,讀寫分離,基本的原理是讓主數(shù)據(jù)庫處理事務性增、改、刪操作,而從數(shù)據(jù)庫處理SELECT查詢操作,數(shù)據(jù)庫復制被用來把事務性操作導致的變更同步到集群中的從數(shù)據(jù)庫,需要的朋友可以參考下
    2023-10-10
  • java 方法與數(shù)組基礎使用詳解

    java 方法與數(shù)組基礎使用詳解

    Java語言中的“方法”(Method)在其他語言當中也可能被稱為“函數(shù)”(Function),數(shù)組對于每一門編程語言來說都是重要的數(shù)據(jù)結構之一,當然不同語言對數(shù)組的實現(xiàn)及處理也不盡相同。Java 語言中提供的數(shù)組是用來存儲固定大小的同類型元素
    2022-04-04
  • Java中的單例模式詳解(完整篇)

    Java中的單例模式詳解(完整篇)

    Java單例模式應該是看起來以及用起來簡單的一種設計模式,但是就實現(xiàn)方式以及原理來說,也并不淺顯,下面這篇文章主要給大家介紹了關于Java中單例模式的相關資料,需要的朋友可以參考下
    2021-11-11
  • 一篇帶你解析入門LongAdder源碼

    一篇帶你解析入門LongAdder源碼

    LongAdder類是JDK1.8新增的一個原子性操作類。AtomicLong通過CAS算法提供了非阻塞的原子性操作,因為非常搞并發(fā)的請求下AtomicLong的性能是不能讓人接受的
    2021-06-06
  • Java正則表達式如何匹配特定html標簽內的內容

    Java正則表達式如何匹配特定html標簽內的內容

    這篇文章主要給大家介紹了關于Java正則表達式如何匹配特定html標簽內的內容的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • Java向MySQL添加中文數(shù)據(jù)數(shù)據(jù)庫顯示亂碼的解決方案

    Java向MySQL添加中文數(shù)據(jù)數(shù)據(jù)庫顯示亂碼的解決方案

    在用springboot做項目時,由于重新安裝了本地Mysql數(shù)據(jù)庫(5.7版本)在前臺向數(shù)據(jù)庫插入和更新數(shù)據(jù)可的時候,涉及中文的時候在數(shù)據(jù)庫一直顯示異常,所以本文給大家介紹了相關的解決方案,需要的朋友可以參考下
    2024-02-02
  • rabbitmq basicReject/basicNack/basicRecover的區(qū)別及說明

    rabbitmq basicReject/basicNack/basicRecover的區(qū)別及說明

    這篇文章主要介紹了rabbitmq basicReject/basicNack/basicRecover的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • SpringBoot事件發(fā)布和監(jiān)聽詳解

    SpringBoot事件發(fā)布和監(jiān)聽詳解

    今天去官網查看spring boot資料時,在特性中看見了系統(tǒng)的事件及監(jiān)聽章節(jié),所以下面這篇文章主要給大家介紹了關于SpringBoot事件發(fā)布和監(jiān)聽的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-11-11
  • Java虛擬機JVM性能優(yōu)化(一):JVM知識總結

    Java虛擬機JVM性能優(yōu)化(一):JVM知識總結

    這篇文章主要介紹了Java虛擬機JVM性能優(yōu)化(一):JVM知識總結,本文是系列文章的第一篇,后續(xù)篇章請繼續(xù)關注腳本之家,需要的朋友可以參考下
    2014-09-09

最新評論