Home  >  Article  >  Database  >  Mysql递归查询

Mysql递归查询

WBOY
WBOYOriginal
2016-06-07 15:33:481894browse

在Oracle 中我们知道有一个 Hierarchical Queries 通过CONNECT BY 我们可以方便的查了所有当前节点下的所有子节点。但是,在MySQL的目前版本中还没有对应的函数!!! 下面在java中实现了mysql的递归查询: 1、递归的类 package cn.edu.nuc.maven.MavenTest;

在Oracle 中我们知道有一个 Hierarchical Queries 通过CONNECT BY 我们可以方便的查了所有当前节点下的所有子节点。但是,在MySQL的目前版本中还没有对应的函数!!!

下面在java中实现了mysql的递归查询:

1、递归的类

package cn.edu.nuc.maven.MavenTest;


import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


public class HQuery {
static Connection conn;  
 
    static Statement st;
/**
* 层次查询
*/
public static void main(String[] args) { 
test_getSortList();
    } 

public static void test_getSortList() {
List sortList = new ArrayList();
getSortList(sortList, 0L, 0); //起始根节点id为0,等级为0
for(Product bean: sortList){
String str = "";
for(int i=0;i str +="——";
}
System.out.println(str+bean.getId() + " " + bean.getParentId() + " " + bean.getName());
}
}

/**
* 查询分类的树型结构
*/
public static void getSortList(List sortList, Long parentId,int level){   


Product bean = null;   
List list = new ArrayList();   
String sql = "select * from product s where s.parentId = "+parentId;
try{   
System.out.println("sql:"+sql);     
list = (List)DBUtils.query4ListBean(sql, new Product().getClass());//每次查询出上级为的分类 
System.out.println(list.size());
if(list != null && list.size() > 0){   
for(int i=0;i bean = (Product)list.get(i);  
bean.setLevel(level+1);//添加等级字段
sortList.add(bean);   
getSortList(sortList,bean.getId(),level+1);   //递归查询
}   
}else{   
level--;
}
}catch(Exception e){   
e.printStackTrace();   
}   

}


2、DBUtils封装类:

package cn.edu.nuc.maven.MavenTest;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;


import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.KeyedHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;


public class DBUtils {
static Connection conn =null;
static String jdbcURL ="jdbc:mysql://172.30.204.163:3306/crm";
static String jdbcDriver ="com.mysql.jdbc.Driver";
static QueryRunner qRunner = null;

static {
DbUtils.loadDriver(jdbcDriver);
try {
conn = DriverManager.getConnection(jdbcURL, "crm", "crm");
conn.setAutoCommit(false);//关闭自动提交
qRunner = new QueryRunner();
} catch (Exception e) {
e.printStackTrace();
}
}

public static List query4ListBean(String sql,Class clazz) throws ClassNotFoundException, SQLException {
List lBean = (List) qRunner.query(conn,
sql, 
new BeanListHandler(clazz));//"cn.edu.nuc.maven.MavenTest.Product"

return lBean;
}

}


3、实体类:(注:在表中并没有level这一列)

package cn.edu.nuc.maven.MavenTest;


public class Product {
private Long id;
private Long parentId;
private String name;
private String note;
private int level;

  。。。

}


4、表结构:

Mysql递归查询


运行结果:

——1 0 产品A
————2 1 产品A-1
————3 1 产品A-2
——4 0 产品B
————5 4 产品B-1
————6 4 圣达菲

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn