日期:2014-05-16 浏览次数:20352 次
CREATE TABLE `functionlist` ( `id` int(10) NOT NULL, `name` varchar(20) default NULL, `link` varchar(100) default NULL, `parent` int(10) default NULL, PRIMARY KEY (`id`), KEY `parent` (`parent`), CONSTRAINT `functionlist_ibfk_1` FOREIGN KEY (`parent`) REFERENCES `functionlist` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
package com.test.pojos; import java.io.Serializable; import java.util.Set; public class functionList implements Serializable{ private int id; private String name; private String link; private functionList parent; private Set<functionList> children = new java.util.HashSet<functionList>(); //省略getter setter方法 public functionList(){} public functionList(String name,String link,functionList parent){ this.name = name; this.link = link; if(parent!=null){ this.children.add(parent); } } }
package com.test.DAO; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.test.pojos.functionList; public class functionListDaoImpl implements functionListDao { public functionListDaoImpl() {} @Override public List<functionList> findRoot() { // TODO Auto-generated method stub Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction trans = session.beginTransaction(); String sqlQuery="from functionList where parent is null"; Query query =session.createQuery(sqlQuery); List<functionList> rootList = (List<functionList>)query.list(); // for(functionList funL : rootList) // { // System.out.println(funL.getName()); // System.out.println(funL.getLink()); // } trans.commit(); session.close(); return rootList; } }
package com.test.action; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.List; import com.opensymphony.xwork2.ActionSupport; import com.test.DAO.functionListDao; import com.test.DAO.functionListDaoImpl; import com.test.pojos.functionList; public class func extends ActionSupport{ private InputStream inputStream; public InputStream getInputStream() { return inputStream; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } public String execute() throws Exception { functionListDao dao = new functionListDaoImpl(); List<functionList> rootList = dao.findRoot(); //手动拼装数据库中取得的功能树,使其成为一个符合json格式的String String json = "["; for(functionList funl1 : rootList) { String root = "{text:'"+funl1.getName()+"',"; if(funl1.getChildren()!=null) { System.out.println("主节点"+funl1.getName()+"的子节点为:"); String children="children:["; for(functionList funl2 : funl1.getChildren()) { String child = "{text:'"+funl2.getName()+"',id:'"+funl2.getId()+"',href:'"+funl2.getLink()+"',leaf:true},"; System.out.println(funl2.getName()); children =children+child; } children = children.substring(0, children.length()-1); root = root+children+"]},"; } json = json+root