Java Code Examples for org.apache.ibatis.mapping.MappedStatement#getSqlSource()

The following examples show how to use org.apache.ibatis.mapping.MappedStatement#getSqlSource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PaginationHandler.java    From sqlhelper with GNU Lesser General Public License v3.0 6 votes vote down vote up
private MappedStatement customOrderByStatement(final MappedStatement ms, final String orderByStatementId) {
    final MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), orderByStatementId, ms.getSqlSource(), ms.getSqlCommandType());
    builder.resource(ms.getResource());
    builder.fetchSize(ms.getFetchSize());
    builder.statementType(ms.getStatementType());
    builder.keyGenerator(ms.getKeyGenerator());
    if (Emptys.isNotEmpty(ms.getKeyProperties())) {
        final StringBuilder keyProperties = new StringBuilder();
        for (final 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());
    builder.resultMaps(ms.getResultMaps());
    builder.resultSetType(ms.getResultSetType());
    builder.cache(ms.getCache());
    builder.flushCacheRequired(ms.isFlushCacheRequired());
    builder.useCache(ms.isUseCache());
    return builder.build();
}
 
Example 2
Source File: MapperHelper.java    From tk-mybatis with MIT License 6 votes vote down vote up
/**
 * 配置指定的接口
 *
 * @param configuration
 * @param mapperInterface
 */
public void processConfiguration(Configuration configuration, Class<?> mapperInterface) {
    String prefix;
    if (mapperInterface != null) {
        prefix = mapperInterface.getCanonicalName();
    } else {
        prefix = "";
    }
    for (Object object : new ArrayList<Object>(configuration.getMappedStatements())) {
        if (object instanceof MappedStatement) {
            MappedStatement ms = (MappedStatement) object;
            if (ms.getId().startsWith(prefix) && isMapperMethod(ms.getId())) {
                if (ms.getSqlSource() instanceof ProviderSqlSource) {
                    setSqlSource(ms);
                }
            }
        }
    }
}
 
Example 3
Source File: PaginationHandler.java    From sqlhelper with GNU Lesser General Public License v3.0 5 votes vote down vote up
private MappedStatement customCountStatement(final MappedStatement ms, final String countStatementId, String querySql, PagingRequest pagingRequest) {
    MappedStatement countStatement = paginationConfig.enableCountCache() ? this.countStatementCache.getIfPresent(querySql) : null;
    if (countStatement == null) {
        final MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), countStatementId, ms.getSqlSource(), ms.getSqlCommandType());
        builder.resource(ms.getResource());
        builder.fetchSize(ms.getFetchSize());
        builder.statementType(ms.getStatementType());
        builder.keyGenerator(ms.getKeyGenerator());
        if (Emptys.isNotEmpty(ms.getKeyProperties())) {
            final StringBuilder keyProperties = new StringBuilder();
            for (final 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());
        final List<ResultMap> resultMaps = new ArrayList<ResultMap>();
        final ResultMap resultMap = new ResultMap.Builder(ms.getConfiguration(), ms.getId(), Long.class, new ArrayList<ResultMapping>()).build();
        resultMaps.add(resultMap);
        builder.resultMaps(resultMaps);
        builder.resultSetType(ms.getResultSetType());
        builder.cache(ms.getCache());
        builder.flushCacheRequired(ms.isFlushCacheRequired());
        boolean useCache = Objects.isNull(pagingRequest.getCacheCount()) ? ms.isUseCache() : pagingRequest.getCacheCount();
        builder.useCache(useCache);

        countStatement = builder.build();
        if (paginationConfig.enableCountCache() && useCache) {
            this.countStatementCache.set(querySql, countStatement);
        }
    }
    return countStatement;
}
 
Example 4
Source File: MyBatisSqlReader.java    From jig with Apache License 2.0 5 votes vote down vote up
private Query getQuery(MappedStatement mappedStatement) throws NoSuchFieldException, IllegalAccessException {
    SqlSource sqlSource = mappedStatement.getSqlSource();

    if (!(sqlSource instanceof DynamicSqlSource)) {
        return new Query(mappedStatement.getBoundSql(null).getSql());
    }

    // 動的クエリ(XMLで組み立てるもの)をエミュレート
    DynamicSqlSource dynamicSqlSource = (DynamicSqlSource) sqlSource;

    Field rootSqlNode = DynamicSqlSource.class.getDeclaredField("rootSqlNode");
    rootSqlNode.setAccessible(true);
    SqlNode sqlNode = (SqlNode) rootSqlNode.get(dynamicSqlSource);


    if (sqlNode instanceof MixedSqlNode) {
        StringBuilder sql = new StringBuilder();
        MixedSqlNode mixedSqlNode = (MixedSqlNode) sqlNode;
        Field contents = mixedSqlNode.getClass().getDeclaredField("contents");
        contents.setAccessible(true);
        List list = (List) contents.get(mixedSqlNode);

        for (Object content : list) {
            if (content instanceof StaticTextSqlNode) {
                StaticTextSqlNode staticTextSqlNode = (StaticTextSqlNode) content;
                Field text = StaticTextSqlNode.class.getDeclaredField("text");
                text.setAccessible(true);
                String textSql = (String) text.get(staticTextSqlNode);
                sql.append(textSql);
            }
        }

        String sqlText = sql.toString().trim();
        LOGGER.debug("動的SQLの組み立てをエミュレートしました。ID={}", mappedStatement.getId());
        LOGGER.debug("組み立てたSQL: [{}]", sqlText);
        return new Query(sqlText);
    }
    return new Query(mappedStatement.getBoundSql(null).getSql());
}
 
Example 5
Source File: MapperHelper.java    From Mapper with MIT License 5 votes vote down vote up
/**
 * 处理 MappedStatement
 *
 * @param ms
 */
public void processMappedStatement(MappedStatement ms){
    MapperTemplate mapperTemplate = isMapperMethod(ms.getId());
    if(mapperTemplate != null && ms.getSqlSource() instanceof ProviderSqlSource) {
        setSqlSource(ms, mapperTemplate);
    }
}
 
Example 6
Source File: MSUtils.java    From Mybatis-PageHelper with MIT License 5 votes vote down vote up
/**
 * 新建count查询的MappedStatement
 *
 * @param ms
 * @param newMsId
 * @return
 */
public static MappedStatement newCountMappedStatement(MappedStatement ms, String newMsId) {
    MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), newMsId, 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());
    //count查询返回值int
    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();
}
 
Example 7
Source File: ListParameterEnhancement.java    From mybatis-boost with MIT License 4 votes vote down vote up
private boolean filter(MappedStatement mappedStatement) {
    return !(mappedStatement.getSqlSource() instanceof DynamicSqlSource) ||
            filterCache.computeIfAbsent(mappedStatement.getId(), k -> filter(Collections.singletonList((SqlNode)
                    MyBatisUtils.getMetaObject(mappedStatement.getSqlSource()).getValue("rootSqlNode"))));
}
 
Example 8
Source File: PaginationHandler.java    From azeroth with Apache License 2.0 4 votes vote down vote up
/**
 * 新建count查询的MappedStatement
 *
 * @param ms
 * @return
 */
public MappedStatement getCountMappedStatement(MappedStatement ms) {

    String newMsId = ms.getId() + PAGE_COUNT_SUFFIX;

    MappedStatement statement = null;
    Configuration configuration = ms.getConfiguration();

    try {
        statement = configuration.getMappedStatement(newMsId);
        if (statement != null) { return statement; }
    } catch (Exception e) {
    }

    synchronized (configuration) {

        if (configuration.hasStatement(newMsId)) { return configuration.getMappedStatement(newMsId); }

        MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(),
                newMsId, 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());
        //count查询返回值int
        List<ResultMap> resultMaps = new ArrayList<ResultMap>();
        String id = newMsId + "-Inline";
        ResultMap resultMap = new ResultMap.Builder(configuration, id, Long.class,
                new ArrayList<ResultMapping>(0)).build();
        resultMaps.add(resultMap);
        builder.resultMaps(resultMaps);

        builder.resultSetType(ms.getResultSetType());
        builder.cache(ms.getCache());
        builder.flushCacheRequired(ms.isFlushCacheRequired());
        builder.useCache(ms.isUseCache());

        statement = builder.build();
        configuration.addMappedStatement(statement);
        return statement;
    }

}
 
Example 9
Source File: PaginationHandler.java    From jeesuite-libs with Apache License 2.0 4 votes vote down vote up
/**
   * 新建count查询的MappedStatement
   *
   * @param ms
   * @return
   */
  public MappedStatement getCountMappedStatement(MappedStatement ms) {
  	
  	String newMsId = ms.getId() + PAGE_COUNT_SUFFIX;
  	
  	MappedStatement statement = null;
  	Configuration configuration = ms.getConfiguration();
	
  	try {
  		statement = configuration.getMappedStatement(newMsId);
  		if(statement != null)return statement;
} catch (Exception e) {}
  	
  	synchronized (configuration) {   
  		
  		if(configuration.hasStatement(newMsId))return configuration.getMappedStatement(newMsId);
  		
  		MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), newMsId, 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());
  		//count查询返回值int
  		 List<ResultMap> resultMaps = new ArrayList<ResultMap>();
           String id = newMsId + "-Inline";
           ResultMap resultMap = new ResultMap.Builder(configuration, id, Long.class, new ArrayList<ResultMapping>(0)).build();
           resultMaps.add(resultMap);
           builder.resultMaps(resultMaps);
           
  		builder.resultSetType(ms.getResultSetType());
  		builder.cache(ms.getCache());
  		builder.flushCacheRequired(ms.isFlushCacheRequired());
  		builder.useCache(ms.isUseCache());
  		
  		statement = builder.build();
  		configuration.addMappedStatement(statement);
  		return statement;
  	}
  	
  }