All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.iih5.smartorm.kit.SqlXmlKit Maven / Gradle / Ivy

Go to download

这是一个ORM模型组件,可快速实现SQL操作,目前支持mysql,NoSQL库目前支持Redis

There is a newer version: 3.1
Show newest version
/**
 * Copyright (c) 2011-2016, James Zhan 詹波 ([email protected]).
 *
 * 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>
    private static   HashMap> resourcesMap = new HashMap>();
    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 files = new ArrayList();
        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 methods= new HashMap();
                    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, Listfiles){
        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 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);
    }
}







© 2015 - 2025 Weber Informatics LLC | Privacy Policy