综合查询(二)--后台工具类准备
dom4j帮助类
/** * dom4j帮助类 * @author yxkong */ public class Dom4jUtil { /** * 获取文档的document对象 * @param xmlPath 文档路径 * @return * @throws Exception */ public static Document getDocument(String xmlPath) throws Exception{ return new SAXReader().read(new File(xmlPath)); } /** * 将内存中的文档对象Documnet写入到对应的xml中 * @param document 内存中的Document对象 * @param xmlPath xml文档路径 * @throws Exception */ public static void write2xml(Document document,String xmlPath) throws Exception{ OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(new FileOutputStream(xmlPath), format); writer.write(document); writer.close(); } }
操作xml帮助类
/** * xml帮助工具类 * @author yxkong * */ public class SearchUtil { //系统数据查询来源配置初始化路径,在服务器启动的时候获取 private static String path; public static String getPath() { return path; } public static void setPath(String path) { SearchUtil.path = path; } //个人模板路径 private static String myTPath; /** * 个人模板真实路径 * @return */ public static String getMyTPath() { return myTPath; } //在用户登录的时候获取 public static void setMyTPath(String myTPath) { SearchUtil.myTPath = myTPath; } /** * 根据模板id查询指定的模板,将模板回显到条件定制页面 * @param id 模板id */ public static Map<String, Object> findTemplateById(String id){ Map<String, Object> map=null; try { Document doc=Dom4jUtil.getDocument(myTPath); //Document doc=Dom4jUtil.getDocument("D:\\javaServer\\tomcat\\Tomacte5.5\\webapps\\dwyglsvn\\WEB-INF\\userSearchTemplate\\admin1.xml"); String xpath="/searchCondition/searchTemplate[@id='"+id+"']"; //获取到指定的节点 Node node = doc.selectSingleNode(xpath); if (node != null) { map=new HashMap<String, Object>(); // 判断node是否是一个元素 if (node.getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) node; //将属性放入map map.put("id", id); map.put("name", e.attributeValue("name")); map.put("fromTable", e.attributeValue("fromTable")); map.put("tableHead", e.attributeValue("tableHead")); List<Map<String,String>> list=null; //遍历每个tr将每个tr集合放入list List<Element> elements = e.elements(); if(elements!=null){ list=new ArrayList<Map<String,String>>(); Map<String,String> m=null; for(Element el:elements){ m=new HashMap<String, String>(); m.put("ljys", el.attributeValue("ljys")); m.put("zkh", el.attributeValue("zkh")); m.put("tableName", el.attributeValue("tableName")); m.put("columnName", el.attributeValue("columnName")); m.put("gx", el.attributeValue("gx")); m.put("cxtjz", el.attributeValue("cxtjz")); m.put("ykh", el.attributeValue("ykh")); list.add(m); } } map.put("trs", list); } } } catch (Exception e) { e.printStackTrace(); } return map; } /** * 查找所有的定制条件 * @param xmlPath 传入指定的xml文档路径 * @return */ public static List<Map<String,String>> findAllSearchCondition(String xmlPath){ List<Map<String,String>> list=null; try { Document doc=Dom4jUtil.getDocument(xmlPath); String xpath="/searchCondition/searchTemplate"; List<Node> nodes=doc.selectNodes(xpath); Map<String,String> m=null; list=new ArrayList<Map<String,String>>(); for(Node n:nodes){ if(n.getNodeType()==Node.ELEMENT_NODE){ Element e=(Element)n; m=new HashMap<String, String>(); m.put(e.attributeValue("id"), e.attributeValue("name")); list.add(m); } } } catch (Exception e) { e.printStackTrace(); } return list; } /** * 获取当前用户保存的查询条件 * @param xmlPath 传入指定的xml文档 * @return */ public static int countSearchConditon(){ List<Map<String,String>> list=findAllSearchCondition(myTPath); if(list!=null) return list.size(); else return 0; } /** * 获取当前用户的保存条件的最大id * @param xmlPath 指定的xml的路径 * @return */ public static int findMaxId(String xmlPath){ int max=0; try { Document doc = Dom4jUtil.getDocument(xmlPath); String xpath="/searchCondition/searchTemplate"; List<Node> nodes=doc.selectNodes(xpath); for(Node n:nodes){ if(n.getNodeType()==Node.ELEMENT_NODE){ Element e=(Element)n; String strId=e.attributeValue("id"); int id = Integer.parseInt(strId); if(id>max) max=id; } } } catch (Exception e) { e.printStackTrace(); } return max; } /** * 删除指定的模板 * @param id 删除模板的id * @param xmlPath 指定的xml文档地址 * @return */ public static Boolean deleteConById(String id,String xmlPath){ Boolean resultFlag=false; try { Document doc=Dom4jUtil.getDocument(xmlPath); String xpath="/searchCondition/searchTemplate[@id='"+id+"']"; //获取到指定的节点 Node node = doc.selectSingleNode(xpath); //获取根节点 Element root=doc.getRootElement(); root.remove(node); Dom4jUtil.write2xml(doc, xmlPath); resultFlag=true; } catch (Exception e) { e.printStackTrace(); } return resultFlag; } /** * 获取所有的表对象 * @param xmlpath xml的路径 * @return */ @SuppressWarnings("unchecked") public static List<Table> getAllTables(String xmlpath){ List<Table> tables=null; try{ Document doc=Dom4jUtil.getDocument(xmlpath); String xpath="/conditions/table"; List<Node> nodes=doc.selectNodes(xpath); Table table=null; tables=new ArrayList<Table>(); for(Node n:nodes){ if(n.getNodeType()==Node.ELEMENT_NODE){ Element e=(Element)n; table=new Table(); table.setName(e.attributeValue("name")); table.setValue(e.attributeValue("value")); tables.add(table); } } }catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } return tables; } /** * 将xml对应的table中的Column封装到list集合中 * @param xmlpath xml路径 * @param tableName 要取出哪个表的数据 * @return */ @SuppressWarnings("unchecked") public static List<Column> getColumnsByTable(String xmlpath, String tableName) { List<Column> columns = null; try { Document doc = Dom4jUtil.getDocument(xmlpath); String xpath = "/conditions/table[@name='" + tableName + "']"; Node node = doc.selectSingleNode(xpath); if (node != null) { // 判断node是否是一个元素 if (node.getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) node; List<Element> elements = e.elements("column"); // 实例化对象 columns = new ArrayList<Column>(); Column column = null; // 将xml中对应的table中的column中的属性封装到list中 for (Iterator<Element> iter = elements.iterator(); iter .hasNext();) { Element columnelement = iter.next(); column = new Column(); column.setName(columnelement.attributeValue("name")); column.setShowText(columnelement .attributeValue("showText")); column.setType(columnelement.attributeValue("type")); columns.add(column); } } } } catch (Exception e) { throw new RuntimeException(e); } return columns; } /** * 查询所有的可查询字段 * @param xmlPath * @return */ public static List<Column> getAllColumns(String xmlPath){ List<Column> columns = null; try { Document doc = Dom4jUtil.getDocument(xmlPath); String xpath = "//table/column"; List<Node> nodes = doc.selectNodes(xpath); columns=new ArrayList<Column>(); Column column=null; for(Node n:nodes){ if(n.getNodeType()==Node.ELEMENT_NODE){ Element e=(Element)n; column=new Column(); column.setName(e.attributeValue("name")); column.setShowText(e.attributeValue("showText")); column.setType(e.attributeValue("type")); columns.add(column); } } } catch (Exception e) { throw new RuntimeException(e); } return columns; } /** * 将查询类型为select的下拉选数据查找出来 * @param tableName 要查找的表名 * @param columnName 对应的列名 * @param xmlpath xml文档保存的路径 * @return */ @SuppressWarnings("unchecked") public static List<SearchValue> getSearchValueByXpath(String tableName,String columnName,String xmlpath){ List<SearchValue> searchValues = null; try { Document doc = Dom4jUtil.getDocument(xmlpath); String xpath="/conditions/table[@name='" + tableName + "']/column[@name='"+columnName+"']"; Node node = doc.selectSingleNode(xpath); if (node != null) { // 判断node是否是一个元素 if (node.getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) node; List<Element> elements = e.elements("searchValue"); // 实例化对象 searchValues = new ArrayList<SearchValue>(); SearchValue searchValue = null; // 将xml中对应的table中的column中的属性封装到list中 for (Iterator<Element> iter = elements.iterator(); iter.hasNext();) { Element searchlement = iter.next(); searchValue = new SearchValue(); searchValue.setName(searchlement.attributeValue("name")); searchValue.setValue(searchlement.attributeValue("value")); searchValues.add(searchValue); } } } } catch (Exception e) { throw new RuntimeException(e); } return searchValues; } }
操作xml的action(使用struts1)
/** * 操做xml的action */ public class ConfigSearchAction extends StrutsEntityAction { /** * 获取所有的模板 */ public ActionForward findAllTemplate(ActionMapping actionMapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { try { //获取登录用户所在区代码 Map infoMap = new GetUserInfo().getUserInfoMap(request); request.setAttribute(com.buffalo.web.framework.Constants.KEY_FORWARD_SINGLE,Boolean.TRUE); //获取用户登录名 String loginName = (String) infoMap.get("loginName"); if(StringUtils.isNotBlank(loginName)){ //获取项目的根路径 String rootPath=request.getSession().getServletContext().getRealPath("/"); //获取当前用户的模板的真实路径,可以直接使用searchUtil中的个人模板路径 String realPath=rootPath+"WEB-INF/userSearchTemplate/"+loginName+".xml"; List<Map<String,String>> allMb = SearchUtil.findAllSearchCondition(realPath); request.setAttribute("allMb", allMb); } }catch(Exception e){ e.printStackTrace(); return actionMapping.findForward("allMbPage"); } return actionMapping.findForward("allMbPage"); } /** * 根据模板id删除对应的模板 */ public void deleteConById(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws IOException { String id=request.getParameter("id"); response.setCharacterEncoding("GBK");// 设置编码 try { //获取登录用户所在区代码 Map infoMap = new GetUserInfo().getUserInfoMap(request); String loginName = (String) infoMap.get("loginName"); if(StringUtils.isNotBlank(loginName)){ Boolean b=SearchUtil.deleteConById(id, SearchUtil.getMyTPath()); String result="no"; if(b){ result="ok"; } response.getWriter().print(result); } }catch(Exception e){ e.printStackTrace(); } } /** * 根据模板id获取模板 */ public void findTemplateById(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws IOException { String id=request.getParameter("id"); response.setCharacterEncoding("GBK");// 设置编码 try { //获取登录用户所在区代码 Map infoMap = new GetUserInfo().getUserInfoMap(request); String loginName = (String) infoMap.get("loginName"); if(StringUtils.isNotBlank(loginName)){ Map<String, Object> map = SearchUtil.findTemplateById(id); JSONArray jsonObject=JSONArray.fromObject(map); response.getWriter().print(jsonObject); } }catch(Exception e){ e.printStackTrace(); } } /** * 查询根据表名查询所有的字段 */ public void findColumnsByTableName(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException { String tableName=request.getParameter("tableName"); System.out.println("请求的表名是"+tableName); response.setCharacterEncoding("GBK");// 设置编码 List<Column> columns=SearchUtil.getColumnsByTable(SearchUtil.getPath(), tableName); //将list集合转成json JSONArray jsonObject=JSONArray.fromObject(columns); response.getWriter().print(jsonObject); } /** * 查找当前字段类型是select的下拉选的内容 */ public void findSeachValueByTableNameAndColumnName(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException { String tableName=request.getParameter("tableName"); String columnName=request.getParameter("columnName"); System.out.println("请求的表名是"+tableName); response.setCharacterEncoding("GBK");// 设置编码 List<SearchValue> searchValues=SearchUtil.getSearchValueByXpath(tableName, columnName, SearchUtil.getPath()); //将list集合转成json JSONArray jsonObject=JSONArray.fromObject(searchValues); response.getWriter().print(jsonObject); } }
jdbc帮助类
/** * 数据库操作帮助类 */ public class JdbcUtils { private static Properties dbConfig=new Properties(); private static String driverClass; private static String url; private static String user; private static String password; static{ InputStream in=JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); try { dbConfig.load(in); driverClass=dbConfig.getProperty("driverClass"); url=dbConfig.getProperty("url"); user=dbConfig.getProperty("user"); password=dbConfig.getProperty("password"); Class.forName(driverClass); } catch (Exception e) { throw new RuntimeException("找不到jdbc的配置文件"); } } /** * 获取JDBC数据库连接 * @return */ public static Connection getConnection(){ Connection conn=null; try { conn = DriverManager.getConnection(url, user, password); } catch (Exception e) { throw new RuntimeException(e); } if(conn==null){ throw new RuntimeException("获取连接失败"); } return conn; } /** * 关闭连接 * @param conn * @param stm * @param rs */ public static void closeSource(Connection conn,Statement stm,ResultSet rs){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { rs=null; throw new RuntimeException(e); }finally{ if(stm!=null){ try { stm.close(); } catch (SQLException e) { stm=null; throw new RuntimeException(e); }finally{ if(conn!=null){ try { conn.close(); } catch (SQLException e) { conn=null; throw new RuntimeException(e); } } } } } } } }
文章评论