Example usage for org.apache.ibatis.mapping MappedStatement getSqlSource

List of usage examples for org.apache.ibatis.mapping MappedStatement getSqlSource

Introduction

In this page you can find the example usage for org.apache.ibatis.mapping MappedStatement getSqlSource.

Prototype

public SqlSource getSqlSource() 

Source Link

Usage

From source file:SqlUtil.java

License:Open Source License

/**
 * ???sql//from w  w w.jav a2  s  .c om
 *
 * @param ms
 * @return
 */
public boolean isDynamic(MappedStatement ms) {
    return ms.getSqlSource() instanceof DynamicSqlSource;
}

From source file:cn.org.awcp.core.mybatis.mapper.MapperHelper.java

License:Open Source License

/**
 * ????? <br>/*from w w w .  ja  va  2s  .  c o m*/
 * ?configurationMappedStatement
 * 
 * @param configuration
 */
public void processConfiguration(Configuration configuration) {
    Collection<MappedStatement> collection = configuration.getMappedStatements();
    // ????
    if (collectionSet.contains(collection)) {
        return;
    } else {
        collectionSet.add(collection);
    }
    int size = collection.size();
    Iterator<?> iterator = collection.iterator();
    while (iterator.hasNext()) {
        Object object = iterator.next();
        if (object instanceof MappedStatement) {
            MappedStatement ms = (MappedStatement) object;
            if (isMapperMethod(ms.getId())) {
                if (ms.getSqlSource() instanceof ProviderSqlSource) {
                    setSqlSource(ms);
                }
            }
        }
        // ??selectKeyms??
        if (collection.size() != size) {
            size = collection.size();
            iterator = collection.iterator();
        }
    }
}

From source file:cn.org.awcp.core.mybatis.mapper.MapperInterceptor.java

License:Open Source License

@Override
public Object intercept(Invocation invocation) throws Throwable {
    Object[] objects = invocation.getArgs();
    MappedStatement ms = (MappedStatement) objects[0];
    String msId = ms.getId();//  ww  w.j  a v a 2  s  .  c o  m
    //??
    if (mapperHelper.isMapperMethod(msId)) {
        //????ProviderSqlSource??????
        if (ms.getSqlSource() instanceof ProviderSqlSource) {
            mapperHelper.setSqlSource(ms);
        }
    }
    Object result = invocation.proceed();
    return result;
}

From source file:com.appleframework.orm.mybatis.pagehelper.MSUtils.java

License:Open Source License

/**
 * countMappedStatement/*from   w  ww.java  2  s.  com*/
 *
 * @param ms
 * @return
 */
public static MappedStatement newCountMappedStatement(MappedStatement ms) {
    MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId() + "_COUNT",
            ms.getSqlSource(), ms.getSqlCommandType());
    builder.resource(ms.getResource());
    builder.fetchSize(ms.getFetchSize());
    builder.statementType(ms.getStatementType());
    builder.keyGenerator(ms.getKeyGenerator());
    if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) {
        StringBuilder keyProperties = new StringBuilder();
        for (String keyProperty : ms.getKeyProperties()) {
            keyProperties.append(keyProperty).append(",");
        }
        keyProperties.delete(keyProperties.length() - 1, keyProperties.length());
        builder.keyProperty(keyProperties.toString());
    }
    builder.timeout(ms.getTimeout());
    builder.parameterMap(ms.getParameterMap());
    //countint
    List<ResultMap> resultMaps = new ArrayList<ResultMap>();
    ResultMap resultMap = new ResultMap.Builder(ms.getConfiguration(), ms.getId(), int.class,
            EMPTY_RESULTMAPPING).build();
    resultMaps.add(resultMap);
    builder.resultMaps(resultMaps);
    builder.resultSetType(ms.getResultSetType());
    builder.cache(ms.getCache());
    builder.flushCacheRequired(ms.isFlushCacheRequired());
    builder.useCache(ms.isUseCache());

    return builder.build();
}

From source file:com.appleframework.orm.mybatis.pagehelper.parser.impl.AbstractParser.java

License:Open Source License

@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map<String, Object> processParameter(MappedStatement ms, Object parameterObject,
        BoundSql boundSql) {//from ww  w  .  j a va  2 s. c o  m
    Map<String, Object> paramMap = null;
    if (parameterObject == null) {
        paramMap = new HashMap<String, Object>();
    } else if (parameterObject instanceof Map) {
        //???Map
        paramMap = new HashMap<String, Object>();
        paramMap.putAll((Map) parameterObject);
    } else {
        paramMap = new HashMap<String, Object>();
        //?sql??ParameterMapping?getter
        //TypeHandlerRegistry???
        boolean hasTypeHandler = ms.getConfiguration().getTypeHandlerRegistry()
                .hasTypeHandler(parameterObject.getClass());
        MetaObject metaObject = SystemMetaObject.forObject(parameterObject);
        //??MyProviderSqlSource?
        if (ms.getSqlSource() instanceof PageProviderSqlSource) {
            paramMap.put(PROVIDER_OBJECT, parameterObject);
        }
        if (!hasTypeHandler) {
            for (String name : metaObject.getGetterNames()) {
                paramMap.put(name, metaObject.getValue(name));
            }
        }
        //????
        if (boundSql.getParameterMappings() != null && boundSql.getParameterMappings().size() > 0) {
            for (ParameterMapping parameterMapping : boundSql.getParameterMappings()) {
                String name = parameterMapping.getProperty();
                if (!name.equals(PAGEPARAMETER_FIRST) && !name.equals(PAGEPARAMETER_SECOND)
                        && paramMap.get(name) == null) {
                    if (hasTypeHandler || parameterMapping.getJavaType().equals(parameterObject.getClass())) {
                        paramMap.put(name, parameterObject);
                        break;
                    }
                }
            }
        }
    }
    //?
    paramMap.put(ORIGINAL_PARAMETER_OBJECT, parameterObject);
    return paramMap;
}

From source file:com.appleframework.orm.mybatis.pagehelper.SqlUtil.java

License:Open Source License

/**
 * ???/* w  w  w . ja v  a2  s .  c  o  m*/
 *
 * @param ms
 * @return
 */
public boolean isPageSqlSource(MappedStatement ms) {
    if (ms.getSqlSource() instanceof PageSqlSource) {
        return true;
    }
    return false;
}

From source file:com.appleframework.orm.mybatis.pagehelper.SqlUtil.java

License:Open Source License

/**
 * SqlSource//  w  w w  .ja v  a 2  s  . c  o m
 *
 * @param ms
 * @throws Throwable
 */
public void processMappedStatement(MappedStatement ms) throws Throwable {
    SqlSource sqlSource = ms.getSqlSource();
    MetaObject msObject = SystemMetaObject.forObject(ms);
    SqlSource pageSqlSource;
    if (sqlSource instanceof StaticSqlSource) {
        pageSqlSource = new PageStaticSqlSource((StaticSqlSource) sqlSource);
    } else if (sqlSource instanceof RawSqlSource) {
        pageSqlSource = new PageRawSqlSource((RawSqlSource) sqlSource);
    } else if (sqlSource instanceof ProviderSqlSource) {
        pageSqlSource = new PageProviderSqlSource((ProviderSqlSource) sqlSource);
    } else if (sqlSource instanceof DynamicSqlSource) {
        pageSqlSource = new PageDynamicSqlSource((DynamicSqlSource) sqlSource);
    } else {
        throw new RuntimeException("?[" + sqlSource.getClass() + "]SqlSource");
    }
    msObject.setValue("sqlSource", pageSqlSource);
    //count??CountMS
    msCountMap.put(ms.getId(), MSUtils.newCountMappedStatement(ms));
}

From source file:com.appleframework.orm.mybatis.pagehelper.SqlUtil.java

License:Open Source License

/**
 * Mybatis/*from  w  ww  .  j  av  a  2  s. c  o m*/
 *
 * @param invocation ?
 * @return 
 * @throws Throwable 
 */
private Page doProcessPage(Invocation invocation, Page page, Object[] args) throws Throwable {
    //?RowBounds?
    RowBounds rowBounds = (RowBounds) args[2];
    //?ms
    MappedStatement ms = (MappedStatement) args[0];
    //?PageSqlSource
    if (!isPageSqlSource(ms)) {
        processMappedStatement(ms);
    }
    //?parser????setThreadLocal???
    ((PageSqlSource) ms.getSqlSource()).setParser(parser);
    try {
        //RowBounds-?Mybatis
        args[2] = RowBounds.DEFAULT;
        //??  pageSizeZero
        if (isQueryOnly(page)) {
            return doQueryOnly(page, invocation);
        }

        //?total??count
        if (page.isCount()) {
            page.setCountSignal(Boolean.TRUE);
            //?MS
            args[0] = msCountMap.get(ms.getId());
            //
            Object result = invocation.proceed();
            //ms
            args[0] = ms;
            //
            page.setTotal((Integer) ((List) result).get(0));
            if (page.getTotal() == 0) {
                return page;
            }
        } else {
            page.setTotal(-1l);
        }
        //pageSize>0pageSize<=0???count
        if (page.getPageSize() > 0 && ((rowBounds == RowBounds.DEFAULT && page.getPageNum() > 0)
                || rowBounds != RowBounds.DEFAULT)) {
            //?MappedStatement?qs
            page.setCountSignal(null);
            BoundSql boundSql = ms.getBoundSql(args[1]);
            args[1] = parser.setPageParameter(ms, args[1], boundSql, page);
            page.setCountSignal(Boolean.FALSE);
            //
            Object result = invocation.proceed();
            //?
            page.addAll((List) result);
        }
    } finally {
        ((PageSqlSource) ms.getSqlSource()).removeParser();
    }

    //
    return page;
}

From source file:com.badminton.interceptors.mySqlHelper.pagehelper.util.MSUtils.java

License:Open Source License

/**
 * countMappedStatement/*from  w  w w  .j a  va  2 s  .  c  om*/
 *
 * @param ms
 * @return
 */
public static MappedStatement newCountMappedStatement(MappedStatement ms) {
    MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId() + "_COUNT",
            ms.getSqlSource(), ms.getSqlCommandType());
    builder.resource(ms.getResource());
    builder.fetchSize(ms.getFetchSize());
    builder.statementType(ms.getStatementType());
    builder.keyGenerator(ms.getKeyGenerator());
    if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) {
        StringBuilder keyProperties = new StringBuilder();
        for (String keyProperty : ms.getKeyProperties()) {
            keyProperties.append(keyProperty).append(",");
        }
        keyProperties.delete(keyProperties.length() - 1, keyProperties.length());
        builder.keyProperty(keyProperties.toString());
    }
    builder.timeout(ms.getTimeout());
    builder.parameterMap(ms.getParameterMap());
    //countint
    List<ResultMap> resultMaps = new ArrayList<ResultMap>();
    ResultMap resultMap = new ResultMap.Builder(ms.getConfiguration(), ms.getId(), Long.class,
            EMPTY_RESULTMAPPING).build();
    resultMaps.add(resultMap);
    builder.resultMaps(resultMaps);
    builder.resultSetType(ms.getResultSetType());
    builder.cache(ms.getCache());
    builder.flushCacheRequired(ms.isFlushCacheRequired());
    builder.useCache(ms.isUseCache());

    return builder.build();
}

From source file:com.cheyipai.platformservice.thirdparty.core.pager.PageHelper.java

License:Open Source License

/**
 * ?sqlSource//from   www . j av a2  s  .  c o m
 *
 * @param ms
 * @param newSqlSource
 * @param suffix
 * @return
 */
private SqlSource getNewSqlSource(MappedStatement ms, BoundSqlSqlSource newSqlSource, String suffix) {
    SqlSource sqlSource = ms.getSqlSource();
    //XMLLanguageDriver.javaXMLScriptBuilder.java???SqlSource
    if (sqlSource instanceof DynamicSqlSource) {
        MetaObject msObject = forObject(ms);
        List<SqlNode> contents = (List<SqlNode>) msObject.getValue(SQL_NODES);
        List<SqlNode> newSqlNodes = new ArrayList<SqlNode>(contents.size() + 2);
        //?
        if (suffix == SUFFIX_PAGE) {
            newSqlNodes.add(new TextSqlNode(getPageSqlBefore()));
            newSqlNodes.addAll(contents);
            newSqlNodes.add(new TextSqlNode(getPageSqlAfter()));
            return new MyDynamicSqlSource(ms.getConfiguration(), new MixedSqlNode(newSqlNodes));
        } else {
            newSqlNodes.add(new TextSqlNode(getCountSqlBefore()));
            newSqlNodes.addAll(contents);
            newSqlNodes.add(new TextSqlNode(getCountSqlAfter()));
            return new DynamicSqlSource(ms.getConfiguration(), new MixedSqlNode(newSqlNodes));
        }
    } else {
        //RawSqlSource
        //?
        if (suffix == SUFFIX_PAGE) {
            //sql
            MetaObject sqlObject = forObject(newSqlSource);
            sqlObject.setValue(BOUND_SQL, getPageSql((String) sqlObject.getValue(BOUND_SQL)));
            //?
            List<ParameterMapping> newParameterMappings = new ArrayList<ParameterMapping>();
            newParameterMappings.addAll(newSqlSource.getBoundSql().getParameterMappings());
            newParameterMappings
                    .add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_FIRST, Integer.class)
                            .build());
            newParameterMappings.add(
                    new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, Integer.class)
                            .build());
            sqlObject.setValue("boundSql.parameterMappings", newParameterMappings);
        } else {
            //count sql
            MetaObject sqlObject = forObject(newSqlSource);
            sqlObject.setValue(BOUND_SQL, getCountSql((String) sqlObject.getValue(BOUND_SQL)));
        }
        return newSqlSource;
    }
}