Overall is the diagram represented by the transformation, by the database and Java Object Conversion , the code is relatively simple, but the pressure on the database can be a bit larger, multiple queries.
The main object is to establish an association between objects No.2:maptotree ()
The idea is: Use the PID(parent ID) as a grouping, so that each group's parent node is the same, in other words, the same group, all nodes PID is the same. This establishes the association for the grouping operation.
Use map for encapsulation, key for parent ID, value for group list
Use the Queryrunner This is a database tool, as long as the internet to find, download can, can directly query list.
queryrunner jar Package name = = Commons-dbutils-1.5.jar
DTO Code: The code for the Tree class. Javabean:
public class Tree{private string area_id;//primary key Idprivate string area_name;//used to display the name private string parent_id;//father reference Area_ Idprivate list<tree> children = new arraylist<tree> ();//child node
Execute code:
Import Java.sql.connection;import java.sql.drivermanager;import java.sql.sqlexception;import java.util.HashMap; Import Java.util.iterator;import java.util.list;import Java.util.map;import Java.util.properties;import Java.util.set;import Org.apache.commons.dbutils.dbutils;import Org.apache.commons.dbutils.queryrunner;import Org.apache.commons.dbutils.handlers.beanlisthandler;import org.apache.commons.dbutils.handlers.columnlisthandler;/** * Database tree structure data, converted to Java object tree structure (multi-fork tree structure) * @author Liupengyuan * */ public class Listtotree {public static void main (string[] args) throws Exception {//no.1: Querying from the database so grouped nodes map<string, Lis t<tree>> Arrmap = Querygrouptomap ();//no.2: Associate the node with the child nodes Maptotree (ARRMAP);//Find the root from the map. Returns the list. There may be more than one root list<tree> list = Arrmap.get ("root"); System.out.println (List.size ());} /** *: * Grouped with Parent ID, encapsulated in map. Key is the parent ID, and value is the array of all the node arrays whose parent ID is key. * Each array is a set of sub-nodes whose root is the same one. In other words their parent ID is the same, and the map key is that they are the parent ID * @return * @throws SQLException */@SuppressWarnings ({"Deprecation","Unchecked", "rawtypes"}) public static map<string,list<tree>> Querygrouptomap () throws sqlexception{/* * The table is a Chinese regional organization, to the county level * For example, China Next: Beijing, Hebei Province, Shandong Province ... * Shandong Sub: Jinan City, Qingdao, Yantai ... * *///Queryrunner This is a database tool, as long as the internet to find the download can commons-dbutils-1 .5.jarQueryRunner qr = new Queryrunner (); Connection Connection = getjdbcconnection ("Jdbc:oracle:thin:@192.168.3.34:1521:orcl", "DSD_ZJK", "DSD_ZJK", " Oracle.jdbc.driver.OracleDriver "); String sqlgroup = "Select parent_id from Hm_f_area T Group by t.parent_id"; List<string> sqlgrouplist = (list<string>) qr.query (connection, SQLGroup, New string[]{}, new Columnlisthandler ("parent_id")); map<string,list<tree>> Arrmap = new hashmap<string,list<tree>> (SqlGroupList.size ()); for ( int i=0; I <sqlgrouplist.size (); i++) {String _pid = Sqlgrouplist.get (i); String sql = "Select area_id, Area_name, parent_id from Hm_f_area t where t.parent_id = '" +_pid + "'"; List<tree> Listtree = (list<tree>) qr.query (connection, SQL, New string[]{}, new Beanlisthandler (Tree.class)); Arrmap.put (_pid, listtree);} Dbutils.close (connection); return arrmap;} /** * No.2: * Let the node and child nodes relate to each other * The database format is not swapped, but the association is established * @param arrmap */public static void Maptotree (Map<string, List<tree >> arrmap) {//So PID integration set<string> pidset = Arrmap.keyset ();//traverse all parent IDs and then match with the node, parent ID and ID of the same//find the corresponding tree node , and then the node's for (iterator<string> it = Pidset.iterator (); It.hasnext ();) {String pid = (string) it.next (); /* * Compared to PID by group. * If found, then the PID group list, as a child node assigned to the Found node Setchildren (list) * */for (iterator<string> it2 = Pidset.ite Rator (); It2.hasnext ();) {String key = (string) it2.next (); Do not find your own group if (Pid.equals (key)) {break; } list<tree> List = Arrmap.get (key); No.3: Find the corresponding tree node and then the tree tree of the node = indexoflist (list, PID); The node's child nodes are found in the map by PID. List<tree> list2 = Arrmap.get (PID); if (tree!=null) {//todo Here is my own definition of becoming, is not the same. So you need to customize//Handle node list assignment to the tree node's children Tree.setchildre N (list2); }}}}/** * No.3: * Find the ID of the element in the list associated with the PID and return. The corresponding relationship is: ID is the same as parent ID * @param list * @param pid * @return */public S Tatic Tree Indexoflist (list<tree> List, String pid) {for (int i=0; i<list.size (); i++) {Tree tree = list.get (i);/* * PID: is the parent ID * area_id: Is ID *///todo here is my own definition of become, are not the same. So you need to customize if (Pid.equals (tree.getarea_id ())) {return tree;}} return null;} /** * Database connection * @param URL * @param username * @param password * @param driverclassname * @return */public static Connection Getjdbcconnection (string URL, string username, string password, string driverclassname) {Connection Connection = Null;try {Properties Props =new properties (); Props.put ("User", username);p rops.put ("password", password);p rops.put (" Remarksreporting "," true "), try {class.forname (driverclassname). newinstance ();} catch (Instantiationexception e) { E.printstacktrace ();} catch (ILlegalaccessexception e) {e.printstacktrace ();} catch (ClassNotFoundException e) {e.printstacktrace ();} Connection=drivermanager.getconnection (url,props);} catch (SQLException e) {e.printstacktrace ();} return connection;}}
Tree structure data in database, converted to Java object tree structure (multi-fork tree structure)