<span>package com.hsj.domain; import java.util.HashMap; import java.util.Map; public class TreeDTO { private int id; private String text; private String iconCls; private String checked; private int parent_id; private String state; /** * 自定义属性信息 */ private Map<String, Object> attributes = new HashMap<String, Object>(); public TreeDTO() { } public TreeDTO(int id, String text, String iconCls, String checked, int parent_id, String state, Map<String, Object> attributes) { this.id = id; this.text = text; this.iconCls = iconCls; this.checked = checked; this.parent_id = parent_id; this.state = state; this.attributes = attributes; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getText() { return text; } public void setText(String text) { this.text = text; } public String getIconCls() { return iconCls; } public void setIconCls(String iconCls) { this.iconCls = iconCls; } public String getChecked() { return checked; } public void setChecked(String checked) { this.checked = checked; } public int getParent_id() { return parent_id; } public void setParent_id(int parentId) { parent_id = parentId; } public String getState() { return state; } public void setState(String state) { this.state = state; } public Map<String, Object> getAttributes() { return attributes; } public void setAttributes(Map<String, Object> attributes) { this.attributes = attributes; } @Override public String toString() { return "TreeDTO [attributes=" + attributes + ", checked=" + checked + ", iconCls=" + iconCls + ",, parent_id=" + parent_id + ", state=" + state + ", text=" + text + "]"; } } </span>
4、创建接口DAO
MeunDao.java
<span>package com.hsj.dao; import java.util.List; import com.hsj.domain.Menu; import com.hsj.domain.TreeDTO; public interface MenuDao{ /** * 根据父节点的值查询所有的子节点 * @param parentId * @return */ public List<TreeDTO> getChildrenByParentId(String parentId); /** * 根据id值查询当前对象 * @param id * @return */ public Menu findMenuById(int id); /** * 保存指定对象 * @param <T> * @param t */ public <T> void save(T t); /** * 修改菜单对象 * @param menu */ public void update(Menu menu); /** * 根据id删除指定对象 * @param id */ public void delete(int id); /** * 根据父节点删除当前父节点下所有的子节点 * @param parentId 父节点id */ public void deleteChildrenByParentId(int parentId); /** * 根据当前节点 的id值删除它的所有子节点 * @param id */ public void deleteChildren(int id); } </span>
5、实现DAO接口方法Bean
MenuDaoBean.java
<span>package com.hsj.dao.bean; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.hsj.dao.MenuDao; import com.hsj.domain.Menu; import com.hsj.domain.TreeDTO; import com.hsj.utils.JDBCUtils; public class MenuDaoBean implements MenuDao{ public <T> int getTotalRecord(Class<T> clazz) { // TODO Auto-generated method stub return 0; } public <T> void save(T t) { //1.根据对象得到类模板对象 Class<T> clazz= (Class<T>) t.getClass(); //2.得到当前类中所有字段组成的数组,不管访问权限如何,但不包括父类中的字段 Field[] fields=clazz.getDeclaredFields(); //insert into t_menu(field1,field2,....) values(value1,value2,....) List<String> key=new ArrayList<String>();//key用来存储字段列表 List<Object> value=new ArrayList<Object>();//value用来存储值列表 String methodName=null; Method method=null; for(int i=0;i<fields.length;i++) { try { //getName methodName="get"+getMethodName(fields[i].getName()); method=clazz.getMethod(methodName); Object o=method.invoke(t); if(o!=null && !"id".equals(fields[i].getName())) { key.add(fields[i].getName()); value.add(o); } } catch (Exception e) { e.printStackTrace(); }; } //组拼sql语句 //String table=clazz.getName().substring(clazz.getName().lastIndexOf(".")+1); String table=clazz.getSimpleName(); StringBuffer sql= new StringBuffer("insert into "+table+" ("); StringBuffer values=new StringBuffer(" values("); for(int i=0;i<value.size();i++) { sql.append(key.get(i)+","); values.append("?,"); } //insert into menu (name,url) sql.deleteCharAt(sql.length()-1).append(")"); //values(?,?) values.deleteCharAt(values.length()-1).append(")"); //insert into menu (name,url) values(?,?) sql.append(values); Connection conn=null; PreparedStatement ps=null; try { conn=JDBCUtils.getConnection(); ps=conn.prepareStatement(sql.toString()); //只有Object[]不为空且数组中有元素 if(key!=null && key.size()>0) { for(int i=0;i<key.size();i++) { ps.setObject(i+1,value.get(i)); } } System.out.println(ps.toString()); ps.execute(); System.out.println("添加成功"); } catch (Exception e) { e.printStackTrace(); }finally{ JDBCUtils.close(null, ps, conn); } } /** * 将该字符串的一个字母变成大写 * @param fildeName 字符串 * @return * @throws Exception */ private static String getMethodName(String fieldName) throws Exception { byte[] items = fieldName.getBytes(); items[0] = (byte) ((char) items[0] - 'a' + 'A'); return new String(items); } /** * 根据菜单的父id找到他所有的子菜单并存储到集合中 */ public List<TreeDTO> getChildrenByParentId(String parentId) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; List<Menu> menus=new ArrayList<Menu>(); List<TreeDTO> treeDTOS=new ArrayList<TreeDTO>(); try { String sql=""; if(parentId==null || "".equals(parentId)) { sql="select * from menu where parent_id=0"; }else{ sql="select * from menu where parent_id="+parentId; } conn=JDBCUtils.getConnection(); ps=conn.prepareStatement(sql); rs=ps.executeQuery(); while(rs.next()) { Menu menu=new Menu(); menu.setId(rs.getInt("id")); menu.setIcon(rs.getString("icon")); menu.setUrl(rs.getString("url")); menu.setChecked(rs.getString("checked")); menu.setName(rs.getString("name")); menu.setParent_id(rs.getInt("parent_id")); menus.add(menu); } }catch(Exception e){ e.printStackTrace(); }finally{ JDBCUtils.close(rs, ps, conn); } for(Menu m : menus) { TreeDTO td=new TreeDTO(); td.setId(m.getId()); td.setText(m.getName()); td.setChecked(m.getChecked()); td.setIconCls(m.getIcon()); td.setParent_id(m.getParent_id()); List<Menu> childrenList=getChildren(m.getId()); if(childrenList.size()>0) { td.setState("closed"); }else{ td.setState("open"); } Map<String,Object> attributes=new HashMap<String,Object>(); attributes.put("url", m.getUrl()); td.setAttributes(attributes); treeDTOS.add(td); } return treeDTOS; } /** * 根据当前菜单的主键值找到当前菜单有哪些子菜单对象组成的集合并返回 * @param id * @return */ public List<Menu> getChildren(int id) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; List<Menu> menus=new ArrayList<Menu>(); try { conn=JDBCUtils.getConnection(); ps=conn.prepareStatement("select * from menu where parent_id="+id); rs=ps.executeQuery(); while(rs.next()) { Menu menu=new Menu(); menu.setId(rs.getInt("id")); menu.setIcon(rs.getString("icon")); menu.setUrl(rs.getString("url")); menu.setChecked(rs.getString("checked")); menu.setName(rs.getString("name")); menu.setParent_id(rs.getInt("parent_id")); menus.add(menu); } } catch(Exception e) { e.printStackTrace(); }finally{ JDBCUtils.close(rs, ps, conn); } return menus; } /** * 根据菜单的主键查找当前菜单对象 */ public Menu findMenuById(int id) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; Menu menu=null; try { conn=JDBCUtils.getConnection(); ps=conn.prepareStatement("select * from menu whereid")); menu.setIcon(rs.getString("icon")); menu.setUrl(rs.getString("url")); menu.setChecked(rs.getString("checked")); menu.setName(rs.getString("name")); menu.setParent_id(rs.getInt("parent_id")); } } catch(Exception e) { e.printStackTrace(); }finally{ JDBCUtils.close(rs, ps, conn); } return menu; } public void update(Menu menu) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn=JDBCUtils.getConnection(); ps=conn.prepareStatement("update menu set name=?,url=?,checked=?,icon=?,parent_id=? where id=?"); ps.setString(1, menu.getName()); ps.setString(2, menu.getUrl()); ps.setString(3, menu.getChecked()); ps.setString(4, menu.getIcon()); ps.setInt(5, menu.getParent_id()); ps.setInt(6, menu.getId()); ps.executeUpdate(); } catch(Exception e) { e.printStackTrace(); }finally{ JDBCUtils.close(rs, ps, conn); } } /** * 根据主键删除当前菜单对象 */ public void delete(int id) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn=JDBCUtils.getConnection(); ps=conn.prepareStatement("delete from menu wheredelete from menu where parent_id="+parentId); System.out.println("========="+ps.toString()); ps.executeUpdate(); } catch(Exception e) { e.printStackTrace(); }finally{ JDBCUtils.close(rs, ps, conn); } } } </span>
6、创建Servlet 并配置映射文件
MenuServlet.java