将数据库查询出来的数据转化成树形结构

从数据库查询的每一条数据都包含id和fid字段,将获得的数据转换成树形结构

假设从数据库查询出来的数据对应的实体类:

public class department {
    private  int id;
    private int fid;
    private String name;
    private List<department> list;
    public department() {
    }
    public department(int id, int fid, String name, List<department> list) {
        this.id = id;
        this.fid = fid;
        this.name = name;
        this.list = list;
    }
    .......
}

将获得数据转化成属性结构

public class DepartmentUtil {

    public List<department> transDepartment(List<department> rootList){
        List<department> nodeList = new ArrayList<>();
        for (department department : rootList) {
            if (department.getId()==0){//表明是一级父类
                nodeList.add(department);
            }
            department.setList(setChild(department.getId(),rootList));
        }
        return nodeList;
    }
    public List<department> setChild(int id, List<department> list ){
        List<department> childList = new ArrayList<>();
        for (department department : list) {
            if (id == department.getFid()){
                childList.add(department);
            }
        }
        for (department department : childList) {
            department.setList(setChild(department.getId(),list));
        }
        if (childList.size()==0){
            return null;
        }
        return childList;
    }
}

通过sql一对多方式实现树形结构的数据格式

  <resultMap id="cityMap" type="com.jiucheng.vo.city">
        <id column="CityID" property="CityID"/>
        <result column="CityName" property="CityName"/>
        <result column="RootID" property="RootID"/>
        <result column="Child" property="Child"/>
        <result column="Layer" property="Layer"/>
        <result column="Sort" property="Sort"/>
        <result column="Isopen" property="Isopen"/>
        <collection property="list" ofType="com.jiucheng.vo.city"
                    column="CityID" select="getChildCity"/>
    </resultMap>

    <select id="findAll" resultMap="cityMap">
        select * from city where RootID = 0
    </select>

    <select id="getChildCity" resultType="com.jiucheng.vo.city">
         select * from city WHERE  RootID = #{CityID}
     </select>

实体类为数据库的映射对应的实体类

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
要将数据库中的数据制作成树形矢量图,您需要执行以下步骤: 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; } } } ``` 请注意,上述代码仅提供了一个基本的示例,并且可能需要针对您的特定用例进行修改和扩展。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

格斗小巨人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值