Mybatis mapper获取sql


今天花了点时间,在网上找了mybatis获取执行sql的工具类,觉得工具类写的挺好的。获取sql的目的是想做异步的导入导出,在页面点击导入导出时,根据页面的参数和后台mybaitis要执行的sql组合成一个完整的执行sql入库,另外一个服务则读取这个sql去做导入导出的相关操作。
如果已经有消息服务的基础,这两个服务可以通过消息中间件来通讯。页面导入导出时,保存sql及发送消息到另外的服务,导入导出服务收到消息执行处理,完成后通过消息反馈给页面,导入服务则反馈导入成功与否,导出服务则执行生成下载文件的链接,反馈给页面。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MyBatisSql {

    /**
     * 运行期 sql
     */
    private String sql;

    /**
     * 参数 数组
     */
    private Object[] parameters;

    public void setSql(String sql) {
        this.sql = sql;
    }

    public String getSql() {
        return sql;
    }

    public void setParameters(Object[] parameters) {
        this.parameters = parameters;
    }

    public Object[] getParameters() {
        return parameters;
    }

    @Override
    public String toString() {
        if (parameters == null || sql == null) {
            return "";
        }
        List parametersArray = Arrays.asList(parameters);
        List list = new ArrayList(parametersArray);
        while (sql.indexOf("?") != -1 && list.size() > 0 && parameters.length > 0) {
            sql = sql.replaceFirst("\\?", list.get(0).toString());
            list.remove(0);
        }
        return sql.replaceAll("(\r?\n(\\s*\r?\n)+)", "\r\n");
    }

} 
  
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.ParameterMode;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.property.PropertyTokenizer;
import org.apache.ibatis.scripting.xmltags.ForEachSqlNode;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;
import java.util.Map;

public class MyBatisSqlUtils {
    /**
     * 运行期获取MyBatis执行的SQL及参数
     *
     * @param id                Mapper xml 文件里的select Id
     * @param parameterMap      参数
     * @param sqlSessionFactory
     * @return
     */
    public static MyBatisSql getMyBatisSql(String id, Map parameterMap, SqlSessionFactory sqlSessionFactory) {
        MyBatisSql ibatisSql = new MyBatisSql();
        MappedStatement ms = sqlSessionFactory.getConfiguration().getMappedStatement(id);
        BoundSql boundSql = ms.getBoundSql(parameterMap);
        ibatisSql.setSql(boundSql.getSql());
        List parameterMappings = boundSql.getParameterMappings();
        Configuration configuration = sqlSessionFactory.getConfiguration();
        if (parameterMappings != null) {
            Object[] parameterArray = new Object[parameterMappings.size()];
            ParameterMapping parameterMapping = null;
            Object value = null;
            Object parameterObject = null;
            MetaObject metaObject = null;
            PropertyTokenizer prop = null;
            String propertyName = null;
            String[] names = null;
            for (int i = 0; i < parameterMappings.size(); i++) {
                parameterMapping = parameterMappings.get(i);
                if (parameterMapping.getMode() != ParameterMode.OUT) {
                    propertyName = parameterMapping.getProperty();
                    names = propertyName.split("\\.");
                    if (propertyName.indexOf(".") != -1 && names.length == 2) {
                        parameterObject = parameterMap.get(names[0]);
                        propertyName = names[1];
                    } else if (propertyName.indexOf(".") != -1 && names.length == 3) {
                        parameterObject = parameterMap.get(names[0]); // map
                        if (parameterObject instanceof Map) {
                            parameterObject = ((Map) parameterObject).get(names[1]);
                        }
                        propertyName = names[2];
                    } else {
                        parameterObject = parameterMap.get(propertyName);
                    }
                    metaObject = parameterMap == null ? null : MetaObject.forObject(parameterObject,configuration.getObjectFactory(),configuration.getObjectWrapperFactory(), configuration.getReflectorFactory());
                    prop = new PropertyTokenizer(propertyName);
                    if (parameterObject == null) {
                        value = null;
                    } else if (ms.getConfiguration().getTypeHandlerRegistry().hasTypeHandler(parameterObject.getClass())) {
                        value = parameterObject;
                    } else if (boundSql.hasAdditionalParameter(propertyName)) {
                        value = boundSql.getAdditionalParameter(propertyName);
                    } else if (propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX) && boundSql.hasAdditionalParameter(prop.getName())) {
                        value = boundSql.getAdditionalParameter(prop.getName());
                        if (value != null) {
                            value = MetaObject.forObject(value,configuration.getObjectFactory(),configuration.getObjectWrapperFactory(), configuration.getReflectorFactory()).getValue(propertyName.substring(prop.getName().length()));
                        }
                    } else {
                        value = metaObject == null ? null : metaObject.getValue(propertyName);
                    }
                    parameterArray[i] = value;
                }
            }
            ibatisSql.setParameters(parameterArray);
        }
        return ibatisSql;
    }

}

上面就是两个主要的实现获取mybatis 执行sql的代码,也是在网上搜索的,这里要感谢作者,我忘了贴原文链接了,后面记得补上,呵呵。。

你可能感兴趣的:(mybatis,异步,mapper,sql,java,mybatis)