DAO就不写接口了,直接写实现类
public List<Dept> tree(String hql, String par) { Session session = getHibernateTemplate().getSessionFactory() .getCurrentSession(); List list = new ArrayList(); if (par.length() == 0) { list = session.createQuery(hql).list(); } else { list = session.createQuery(hql).setString(0, par).list(); } return list; }
Service实现类
public List<Dept> tree(Dept dept) { String hql = ""; String par = ""; if (dept.getId()==null || dept.getId().trim().equals("")) { hql ="from Dept d where d.dept is null"; }else{ hql = "from Dept d where d.dept.id = ?"; par = dept.getId(); } return deptDao.tree(hql, par); }
action
//添加里面的树 public String comboxtree() throws Exception { List<Dept> findAll = deptService.tree(dept); List list = new ArrayList(); for (Dept dept : findAll) { Map map = new HashMap(); if(dept.getDept() == null){ map.put("id", dept.getId()); map.put("text", dept.getName()); map.put("pid", 0); map.put("state", "closed"); list.add(map); }else{ map.put("id", dept.getId()); map.put("text", dept.getName()); map.put("pid", dept.getDept().getId()); List<Dept> depts = deptService.tree(dept); if(depts.size()>0){ map.put("state", "closed"); } list.add(map); } } System.out.println(JSONArray.fromObject(list).toString()); System.out.println(); ServletActionContext.getResponse().getWriter().write(JSONArray.fromObject(list).toString()); return null; }
时间: 2024-12-22 08:28:33