1. 高級資料庫設計中,什麼是遞歸關系遞歸關系有幾種什麼是交疊規則和分離規則
遞歸關系就是對自身的引用,多媒體資料庫中就是記錄與記錄之間的直接或間接專的調用。遞歸關系有屬兩種 一對多 多對多 至於分離和交疊規則 就是 ,父類實例與子類實體之間的關系。若父類的實例屬於多個字類實體則是交疊規則,若父類實例至多屬於一個子類實體則是分離規則。不用謝!^_^
2. java 遞歸資料庫生成 樹形結構問題
1、准備表結構及對應的表數據
a、表結構:
create table TB_TREE
(
CID NUMBER not null,
CNAME VARCHAR2(50),
PID NUMBER //父節點
)
b、表數據:
insert into tb_tree (CID, CNAME, PID) values (1, '中國', 0);
insert into tb_tree (CID, CNAME, PID) values (2, '北京市', 1);
insert into tb_tree (CID, CNAME, PID) values (3, '廣東省', 1);
insert into tb_tree (CID, CNAME, PID) values (4, '上海市', 1);
insert into tb_tree (CID, CNAME, PID) values (5, '廣州市', 3);
insert into tb_tree (CID, CNAME, PID) values (6, '深圳市', 3);
insert into tb_tree (CID, CNAME, PID) values (7, '海珠區', 5);
insert into tb_tree (CID, CNAME, PID) values (8, '天河區', 5);
insert into tb_tree (CID, CNAME, PID) values (9, '福田區', 6);
insert into tb_tree (CID, CNAME, PID) values (10, '南山區', 6);
insert into tb_tree (CID, CNAME, PID) values (11, '密雲縣', 2);
insert into tb_tree (CID, CNAME, PID) values (12, '浦東', 4);
2、TreeNode對象,對應tb_tree
public class TreeNode implements Serializable {
private Integer cid;
private String cname;
private Integer pid;
private List nodes = new ArrayList();
public TreeNode() {
}
//getter、setter省略
}
3、測試數據
public class TreeNodeTest {
@Test
public void loadTree() throws Exception{
System.out.println(jsonUtils.javaToJson(recursiveTree(1)));
}
/**
* 遞歸演算法解析成樹形結構
*
* @param cid
* @return
* @author jiqinlin
*/
public TreeNode recursiveTree(int cid) {
//根據cid獲取節點對象(SELECT * FROM tb_tree t WHERE t.cid=?)
TreeNode node = personService.getreeNode(cid);
//查詢cid下的所有子節點(SELECT * FROM tb_tree t WHERE t.pid=?)
List childTreeNodes = personService.queryTreeNode(cid);
//遍歷子節點
for(TreeNode child : childTreeNodes){
TreeNode n = recursiveTree(child.getCid()); //遞歸
node.getNodes().add(n);
}
return node;
}
}
輸出的json格式如下:
{
"cid": 1,
"nodes": [
{
"cid": 2,
"nodes": [
{
"cid": 11,
"nodes": [
],
"cname": "密雲縣",
"pid": 2
}
],
"cname": "北京市",
"pid": 1
},
{
"cid": 3,
"nodes": [
{
"cid": 5,
"nodes": [
{
"cid": 7,
"nodes": [
],
"cname": "海珠區",
"pid": 5
},
{
"cid": 8,
"nodes": [
],
"cname": "天河區",
"pid": 5
}
],
"cname": "廣州市",
"pid": 3
},
{
"cid": 6,
"nodes": [
{
"cid": 9,
"nodes": [
],
"cname": "福田區",
"pid": 6
},
{
"cid": 10,
"nodes": [
],
"cname": "南山區",
"pid": 6
}
],
"cname": "深圳市",
"pid": 3
}
],
"cname": "廣東省",
"pid": 1
},
{
"cid": 4,
"nodes": [
{
"cid": 12,
"nodes": [
],
"cname": "浦東",
"pid": 4
}
],
"cname": "上海市",
"pid": 1
}
],
"cname": "中國",
"pid": 0
}
3. sql 怎麼遞歸查詢的方法:
1.創建測試表,createtabletest_connect(idnumber,p_idnumber);
4. java 遞歸查詢資料庫
這個就是個樹形結構,數據結構里的東西。查詢後構建一個樹就行了。稍後給你一個代碼。