[英]MyBatis - Mapping tree data-structure
How can I map table id | parent_id | name
如何映射表id | parent_id | name
id | parent_id | name
id | parent_id | name
to my pojo class: 我的pojo班的id | parent_id | name
:
public class Node {
private Integer id;
private Integer parentId;
private String name;
private List<Node> children;
//getters and setters
}
I want to get list of root nodes (parent_id=null) with filled children list. 我想获取带有填充子列表的根节点列表(parent_id = null)。 No restriction in tree depth. 树深度没有限制。 I am thinking of something like: 我在想类似的东西:
<resultMap id="nodeResult" type="Node">
<id property="id" column="id"/>
<result property="parentId" column="parent_id"/>
<result property="name" column="name"/>
<collection property="children" resultMap="nodeResult"/>
</resultMap>
<select id="selectNode" resultMap="nodeResult">
SELECT * FROM NODE
</select>
I don't know where to put restriction for children list: id=parentId. 我不知道对子级列表设置限制:id = parentId。 I don't want to use nested select 我不想使用嵌套选择
There won't be a way to get an arbitrarly deep tree to populate automagically in a ResultMap. 没有办法让任意深的树自动地填充到ResultMap中。
Are DB specific solutions an option? 是否可以选择特定于数据库的解决方案? For example, with PostgreSQL you can use common table expressions to select the data into a ResultSet that may be applicable to be mapped into a tree. 例如,在PostgreSQL中,您可以使用公用表表达式将数据选择到ResultSet中,该数据可能适用于映射到树中。
Otherwise you should just change your collection definition in the ResultMap to point to a query to select the children, which can reuse the same resultmap to get the recursive behavior 否则,您应该只更改ResultMap中的集合定义以指向查询以选择子项,这些子项可以重用相同的resultmap来获得递归行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.