最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java redis存Map對(duì)象類(lèi)型數(shù)據(jù)的實(shí)現(xiàn)

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

背景描述

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

實(shí)體類(lèi)

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

父類(lèi)

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;
? ? }
}

子類(lèi)

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");

? ? ? ? //構(gòu)造存入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);
? ? ? ? //獲取緩存內(nèi)容
? ? ? ? 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);
? ? }
}

結(jié)果

參考
http://www.fzitv.net/article/246815.htm

方法2°

將對(duì)象轉(zhuǎn)成byte[]

序列化及反序列化工具類(lèi)

import java.io.*;

/**
?* 序列化及反序列化工具類(lèi)
?*/
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");

? ? ? ? //構(gòu)造存入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

到此這篇關(guān)于Java redis存Map對(duì)象類(lèi)型數(shù)據(jù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java redis存Map對(duì)象類(lèi)型內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring-boot集成pg、mongo多數(shù)據(jù)源過(guò)程詳解

    Spring-boot集成pg、mongo多數(shù)據(jù)源過(guò)程詳解

    這篇文章主要介紹了Spring-boot集成pg、mongo多數(shù)據(jù)源過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • SpringBoot項(xiàng)目中使用Mockito的示例代碼

    SpringBoot項(xiàng)目中使用Mockito的示例代碼

    這篇文章主要介紹了SpringBoot項(xiàng)目中使用Mockito的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Spring中的DeferredImportSelector實(shí)現(xiàn)詳解

    Spring中的DeferredImportSelector實(shí)現(xiàn)詳解

    這篇文章主要介紹了Spring中的DeferredImportSelector實(shí)現(xiàn)詳解,兩個(gè)官方的實(shí)現(xiàn)類(lèi)AutoConfigurationImportSelector和ImportAutoConfigurationImportSelector都是Spring Boot后新增的實(shí)現(xiàn),需要的朋友可以參考下
    2024-01-01
  • java 中平方根(sqrt)算法 的實(shí)例詳解

    java 中平方根(sqrt)算法 的實(shí)例詳解

    這篇文章主要介紹了java 中平方根(sqrt)算法 的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Spring IOC與DI核心深入理解

    Spring IOC與DI核心深入理解

    IOC也是Spring的核心之一了,之前學(xué)的時(shí)候是采用xml配置文件的方式去實(shí)現(xiàn)的,后來(lái)其中也多少穿插了幾個(gè)注解,但是沒(méi)有說(shuō)完全采用注解實(shí)現(xiàn)。那么這篇文章就和大家分享一下,全部采用注解來(lái)實(shí)現(xiàn)IOC+DI
    2023-02-02
  • 關(guān)于@Component注解的含義說(shuō)明

    關(guān)于@Component注解的含義說(shuō)明

    這篇文章主要介紹了關(guān)于@Component注解的含義說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • SpringBoot創(chuàng)建定時(shí)任務(wù)的示例詳解

    SpringBoot創(chuàng)建定時(shí)任務(wù)的示例詳解

    在Spring Boot中創(chuàng)建定時(shí)任務(wù),通常使用@Scheduled注解,這是Spring框架提供的一個(gè)功能,允許你按照固定的頻率(如每天、每小時(shí)、每分鐘等)執(zhí)行某個(gè)方法,本文給大家介紹了SpringBoot創(chuàng)建定時(shí)任務(wù)的示例,需要的朋友可以參考下
    2024-04-04
  • 如何在 Linux 上搭建 java 部署環(huán)境(安裝jdk/tomcat/mysql) + 將程序部署到云服務(wù)器上的操作)

    如何在 Linux 上搭建 java 部署環(huán)境(安裝jdk/tomcat/mys

    這篇文章主要介紹了如何在 Linux 上搭建 java 部署環(huán)境(安裝jdk/tomcat/mysql) + 將程序部署到云服務(wù)器上的操作),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-01-01
  • Java springboot項(xiàng)目jar發(fā)布過(guò)程解析

    Java springboot項(xiàng)目jar發(fā)布過(guò)程解析

    這篇文章主要介紹了Java springboot項(xiàng)目jar發(fā)布過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • HTTPClient如何在Springboot中封裝工具類(lèi)

    HTTPClient如何在Springboot中封裝工具類(lèi)

    這篇文章主要介紹了HTTPClient如何在Springboot中封裝工具類(lèi)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09

最新評(píng)論

遂溪县| 岳西县| 奉化市| 和田市| 达拉特旗| 正宁县| 工布江达县| 镇原县| 聂拉木县| 高陵县| 沾化县| 东宁县| 宁夏| 平定县| 收藏| 晴隆县| 洛阳市| 肇州县| 兴义市| 南陵县| 合阳县| 昌乐县| 黎城县| 隆回县| 南和县| 合江县| 仪征市| 施秉县| 阳曲县| 菏泽市| 沙田区| 郑州市| 镇雄县| 平阳县| 石屏县| 闻喜县| 新野县| 广汉市| 西宁市| 冀州市| 滁州市|