Java tutorial
/** * Copyright (c) 2011-2016, James Zhan (jfinal@126.com). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.iih5.smartorm.kit; import org.apache.log4j.Logger; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.io.File; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * xml?sql?? */ public class SqlXmlKit { // map<className,<method,sql>> private static HashMap<String, Map<String, String>> resourcesMap = new HashMap<String, Map<String, String>>(); public SqlXmlKit() { try { URL url = Thread.currentThread().getContextClassLoader().getResource("sql"); File dataDir = new File(url.toURI()); init(dataDir); } catch (Exception e) { e.printStackTrace(); Logger.getLogger(SqlXmlKit.class).error("?sql xml "); } } public SqlXmlKit(String path) { init(new File(path)); } private void init(File dataDir) { try { List<File> files = new ArrayList<File>(); listDirectory(dataDir, files); for (File file : files) { if (file.getName().contains(".xml")) { SAXReader reader = new SAXReader(); Document document = reader.read(file); Element xmlRoot = document.getRootElement(); for (Object e : xmlRoot.elements("class")) { Map<String, String> methods = new HashMap<String, String>(); Element clasz = (Element) e; for (Object ebj : clasz.elements("sql")) { Element sql = (Element) ebj; methods.put(sql.attribute("method").getValue(), sql.getText()); } resourcesMap.put(clasz.attributeValue("name"), methods); } } } } catch (Exception e) { e.printStackTrace(); } } /** * ????? * @param path * @param files */ private void listDirectory(File path, List<File> files) { if (path.exists()) { if (path.isFile()) { files.add(path); } else { File[] list = path.listFiles(); for (int i = 0; i < list.length; i++) { listDirectory(list[i], files); } } } } /** * ?sql? * @param classPath package * @param methodName ?? * @return ?sql? */ public static String getSQL(String classPath, String methodName) { Map<String, String> m = resourcesMap.get(classPath); return m.get(methodName); } /** * ?sql? * @return */ public static String getCurrentSql() { //? String clazz = Thread.currentThread().getStackTrace()[2].getClassName(); //?getSQL String method = Thread.currentThread().getStackTrace()[2].getMethodName(); return SpringKit.getApplicationContext().getBean(SqlXmlKit.class).getSQL(clazz, method); } }