当前位置: 首页 > news >正文

springboot+redis实现将树形结构存储到redis

1.pom配置redis

 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>

2.yml文件配置:

spring:redis:database: 0host: 1.1.1.1port: 6379timeout: 2000password:jedis:pool:max-idle: 100min-idle: 50max-wait: 10000

3.TreeNode实体对象

import lombok.Data;import java.util.List;
@Data
public class TreeNode {private String id;private String name;private String parentId;private List<TreeNode> children;
}

4.service实现逻辑

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;@Service
public class TreeStructureRedisService {private static final String TREE_KEY_PREFIX = "tree:";@Autowiredprivate StringRedisTemplate redisTemplate;private final ObjectMapper objectMapper = new ObjectMapper();/*** 存储树形结构到 Redis* @param treeId 树形结构的唯一标识* @param root 树的根节点* @throws JsonProcessingException JSON 处理异常*/public void saveTree(String treeId, TreeNode root) throws JsonProcessingException {String key = TREE_KEY_PREFIX + treeId;String jsonTree = objectMapper.writeValueAsString(root);redisTemplate.opsForValue().set(key, jsonTree);}/*** 从 Redis 中获取树形结构* @param treeId 树形结构的唯一标识* @return 树的根节点* @throws JsonProcessingException JSON 处理异常*/public TreeNode getTree(String treeId) throws JsonProcessingException {String key = TREE_KEY_PREFIX + treeId;String jsonTree = redisTemplate.opsForValue().get(key);if (jsonTree != null) {return objectMapper.readValue(jsonTree, TreeNode.class);}return null;}
}

5.controller实现

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.io.IOException;@RestController
@RequestMapping("/trees")
public class TreeStructureController {@Autowiredprivate TreeStructureRedisService treeStructureRedisService;/*** 保存树形结构到 Redis* @param treeId 树形结构的唯一标识* @param root 树的根节点* @return 操作结果信息*/@PostMapping("/{treeId}")public String saveTree(@PathVariable String treeId, @RequestBody TreeNode root) {try {treeStructureRedisService.saveTree(treeId, root);return "Tree saved successfully";} catch (IOException e) {return "Error saving tree: " + e.getMessage();}}/*** 从 Redis 中获取树形结构* @param treeId 树形结构的唯一标识* @return 树的根节点*/@GetMapping("/{treeId}")public TreeNode getTree(@PathVariable String treeId) {try {return treeStructureRedisService.getTree(treeId);} catch (IOException e) {return null;}}
}
http://www.lryc.cn/news/534601.html

相关文章:

  • 6、使用one-api管理统一管理大模型,并开始使用本地大模型
  • Windows安装Lyx
  • 一文讲透大模型部署工具ollama--结合本地化部署deepseek实战
  • 网络防御高级
  • 使用PyCharm进行Django项目开发环境搭建
  • 如何定义“破坏环境”
  • 现代前端开发的演进与未来趋势:从工具革新到技术突破
  • 活动预告 |【Part1】Microsoft 安全在线技术公开课:安全性、合规性和身份基础知识
  • idea Ai工具通义灵码,Copilot我的使用方法以及比较
  • 【JavaScript】《JavaScript高级程序设计 (第4版) 》笔记-Chapter8-对象、类与面向对象编程
  • 介绍下SpringBoot常用的依赖项
  • 深度解析策略模式:从理论到企业级实战应用
  • 【Linux】深入理解linux权限
  • C++STL(六)——list模拟
  • 网络安全与AI:数字经济发展双引擎
  • WPS接入DeepSeek模型
  • 深度学习之神经网络框架搭建及模型优化
  • 采用分步式无线控制架构实现水池液位自动化管理
  • OpenEuler学习笔记(二十三):在OpenEuler上部署开源MES系统
  • SpringSecurity:授权服务器与客户端应用(入门案例)
  • 没用的文章又➕1
  • BiGRU双向门控循环单元多变量多步预测,光伏功率预测(Matlab完整源码和数据)
  • 谷歌浏览器多开指南:如何完成独立IP隔离?
  • Django开发入门 – 3.用Django创建一个Web项目
  • 【Java】多线程和高并发编程(三):锁(下)深入ReentrantReadWriteLock
  • 讲解ES6中的变量和对象的解构赋值
  • DeepSeek Coder + IDEA 辅助开发工具
  • 云计算——AWS Solutions Architect – Associate(saa)4.安全组和NACL
  • 动量+均线组合策略关键点
  • Blazor-<select>