jfinal集成类似myBaits的sqlKit工具类

// XML配置SQL语句的插件
me.add(new SqlInXmlPlugin(true);
package com.utils.plugin.sqlinxml;

import java.io.File;
import java.io.InputStream;
import java.io.StringReader;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.Maps;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.utils.exception.CommonException;
import com.utils.kit.ClassUtil;
import com.utils.kit.JaxbKit;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectBody;


public class SqlKit {

    private static Map<String, Map<String, String>> sqlMap = Maps.newHashMap();
    
    private static final String CONFIG_REGEX = "*sql.xml";
    
    private static final CCJSqlParserManager manager = new CCJSqlParserManager();
    
    public static Statement parse(String sql) {
    	if(StringUtils.isEmpty(sql)) return null;
    	try {
    		return manager.parse(new StringReader(sql));
    	} catch(Exception ex) {
    		return null;
    	}
    }
    
    public static String singleWhere(Statement statement) {
    	if(statement instanceof Select) {
    		SelectBody body = ((Select) statement).getSelectBody();
    		if(body instanceof PlainSelect) {
    			Expression expression = ((PlainSelect) body).getWhere();
    			if(expression != null) {
    				return expression.toString();
    			}
    		}
    	}
    	return null;
    }
    
    public static String find(String groupToken, String sqlToken) {
    	if(groupToken == null || sqlToken == null) {
    		throw new CommonException(" groupToken and sqlToken need to set value . ");
    	}
    	
    	if(sqlMap.isEmpty()) {
    		throw new CommonException("SqlInXmlPlugin not start or no xml files . ");
    	}
    	
    	Map<String, String> items = sqlMap.get(groupToken);
    	String sql = null;
    	if(items != null) {
    		sql = items.get(sqlToken);
    	}
        return sql;
    }
    
    public static String buildArgsForCondition_IN(Object [] sources) {
    	StringBuilder param = new StringBuilder("");
    	if(sources != null && sources.length > 0) {
    		if(sources instanceof String []) {
    			String [] source_string = (String[]) sources;
    			for(int i=0 ; i<source_string.length ; i++) {
    				if(i > 0) param.append(" , ");
    				param.append("'" + source_string[i] + "'");
    			}
    		} else if(sources instanceof Number []) {
    			Number [] source_number = (Number[]) sources;
    			param.append(StringUtils.join(source_number, " , "));
    		}
    	}
    	return param.append("") .toString();
    }
    
    public static void clearSqlMap() {
        sqlMap.clear();
    }
    
    static void init(boolean includeAllJarsInLib,List<String> includeJars) { //初始化方法  先执行 包括解析xml文档
    	/**********先加载本地,如果需要jar则再加载远程文件************/
    	initLocal();
    	if(includeAllJarsInLib){//第三方
    		initJars(includeJars);
    	}
    }
    /**
     * @description TODO 加载本地
     * @author Tony
     * @date 2017年8月9日下午5:14:08
   */
    public static void initLocal(){
    	List<File> files = ClassUtil.listFiles(null, CONFIG_REGEX);
        for (File xmlfile : files) {
            SqlGroup group = JaxbKit.unmarshal(xmlfile, SqlGroup.class);
            String name = group.name;
            if (StringUtils.isBlank(name)) {
                name = StringUtils.substringAfterLast(xmlfile.getName(), ".");
            }
            
            Map<String, String> sqlList = sqlMap.get(name);
            if(sqlList == null) {
            	sqlList = Maps.newHashMap();
            	sqlMap.put(name, sqlList);
            }
            for (SqlItem sqlItem : group.sqlItems) {
            	sqlList.put(sqlItem.id, sqlItem.value);
            }
        }
    }
    /**
     * @description TODO 加载jar包
     * @author Tony
     * @date 2017年8月9日下午5:14:08
   */
    public static void initJars(List<String> includeJars){
	    List<InputStream> files = ClassUtil.of(Controller.class).
	    		includeAllJarsInLib(true).inJars(includeJars)
	    		.search(null,CONFIG_REGEX.replace("*", ""));//流格式调用,为了支持jar包调用
	    for (InputStream xmlfile : files) {
	        SqlGroup group = JaxbKit.unmarshal(xmlfile, SqlGroup.class);
	        String name = group.name;
	        Map<String, String> sqlList = sqlMap.get(name);
	        if(sqlList == null) {
	        	sqlList = Maps.newHashMap();
	        	sqlMap.put(name, sqlList);
	        }
	        for (SqlItem sqlItem : group.sqlItems) {
	        	sqlList.put(sqlItem.id, sqlItem.value);
	        }
	    }
    }
	/**
	  * @Title formatSqlIn
	  * @Description 格式化sql中in字段 转化为or
	  * @param columnName 字段名
	  * @param size 个数
	  * @param sql 原始sql
	  * @return  设定文件
	  * @return String    返回类型
	  * @date 2015年12月29日 下午5:42:35
	 */
	public static String formatSqlIn(String columnName,int size,String sql) {
		// 重新组装参数
		if(size>0 && StrKit.notBlank(columnName,sql)){
			if (size == 1) {
				sql += " and "+columnName+" = ?";
			} else {
				sql += " and (";
				for (int i = 0; i <size; i++) {
					sql += " "+columnName+" = ? or";
				}
				sql = sql.substring(0, sql.length() - 2);
				sql += ") ";
			}
		}
		return sql;
	}
}

 

转载于:https://my.oschina.net/u/1265949/blog/1591481

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值