将从数据库查询出的父子关系数据 转换成树形结构

		List<TreeNode> nodes = new ArrayList<>();//替换成从数据库查询获得的list
		Map<String, TreeNode> map = new HashMap<>();
        List<TreeNode> parents = new ArrayList<>();
        for (TreeNode node : nodes) {
            map.put(node.getId(), node);
            if (node.getParentId() == null) {
                parents.add(node);
            }
        }
        for (TreeNode node : nodes) {
            TreeNode parent = map.get(node.getParentId());
            if (parent != null) {
                parent.getChildren().add(node);
            }
        }
        return parents;
要将数据库中的数据制作成树形矢量图,您需要执行以下步骤: 1. 从数据库中选择数据并将其转换为树形结构。这可以通过递归查询和组装节点来完成。例如,如果您的数据中以父子关系存储,则可以使用递归查询来获取所有节点,并将它们组装成一棵树。 2. 将树形结构转换为矢量图。您可以使用Java的绘图库,如Java2D或JavaFX,来实现此目的。在绘制过程中,您需要为每个节点创建一个图形对象,并将它们放置在正确的位置上,以形成树形矢量图。 以下是一个简单的示例代码,用于从数据库中获取数据并将其转换为树形矢量图(使用JavaFX): ```java import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Line; import javafx.scene.shape.Rectangle; import javafx.scene.text.Text; import javafx.stage.Stage; import java.sql.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class TreeVector extends Application { private Map<Integer, List<Node>> nodesByDepth = new HashMap<>(); private int maxDepth = 0; public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { Group root = new Group(); Scene scene = new Scene(root, 800, 600, Color.WHITE); // Get data from database List<Node> nodes = getDataFromDatabase(); // Create tree structure Node rootNode = createTree(nodes); // Convert tree to vector graphics createVectorGraphics(root, rootNode); primaryStage.setScene(scene); primaryStage.show(); } private List<Node> getDataFromDatabase() throws SQLException { List<Node> nodes = new ArrayList<>(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT id, parent_id, name FROM mytable"); while (rs.next()) { int id = rs.getInt("id"); int parentId = rs.getInt("parent_id"); String name = rs.getString("name"); nodes.add(new Node(id, parentId, name)); } rs.close(); stmt.close(); conn.close(); return nodes; } private Node createTree(List<Node> nodes) { Map<Integer, Node> nodeById = new HashMap<>(); Node rootNode = null; for (Node node : nodes) { if (node.parentId == 0) { rootNode = node; } else { Node parent = nodeById.get(node.parentId); if (parent == null) { parent = new Node(node.parentId, 0, ""); nodeById.put(node.parentId, parent); } parent.children.add(node); } nodeById.put(node.id, node); } return rootNode; } private void createVectorGraphics(Group parent, Node node) { // Create rectangle for node Rectangle rect = new Rectangle(50, 30); rect.setFill(Color.LIGHTGRAY); rect.setStroke(Color.BLACK); rect.setX(node.depth * 100); rect.setY(node.position * 50); parent.getChildren().add(rect); // Create text for node Text text = new Text(node.name); text.setX(node.depth * 100 + 10); text.setY(node.position * 50 + 20); parent.getChildren().add(text); // Create lines for children for (Node child : node.children) { Line line = new Line(node.depth * 100 + 50, node.position * 50 + 15, child.depth * 100, child.position * 50 + 15); parent.getChildren().add(line); createVectorGraphics(parent, child); } } private class Node { int id; int parentId; String name; List<Node> children = new ArrayList<>(); int depth; int position; public Node(int id, int parentId, String name) { this.id = id; this.parentId = parentId; this.name = name; } } } ``` 请注意,上述代码仅提供了一个基本的示例,并且可能需要针对您的特定用例进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值