Java Code Examples for org.apache.ibatis.mapping.MappedStatement#Builder

The following examples show how to use org.apache.ibatis.mapping.MappedStatement#Builder . 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: MapperBuilderAssistant.java    From mybaties with Apache License 2.0 6 votes vote down vote up
private void setStatementParameterMap(
    String parameterMap,
    Class<?> parameterTypeClass,
    MappedStatement.Builder statementBuilder) {
  parameterMap = applyCurrentNamespace(parameterMap, true);

  if (parameterMap != null) {
    try {
      statementBuilder.parameterMap(configuration.getParameterMap(parameterMap));
    } catch (IllegalArgumentException e) {
      throw new IncompleteElementException("Could not find parameter map " + parameterMap, e);
    }
  } else if (parameterTypeClass != null) {
    List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>();
    ParameterMap.Builder inlineParameterMapBuilder = new ParameterMap.Builder(
        configuration,
        statementBuilder.id() + "-Inline",
        parameterTypeClass,
        parameterMappings);
    statementBuilder.parameterMap(inlineParameterMapBuilder.build());
  }
}
 
Example 2
Source File: PaginationPlugin.java    From easyooo-framework with Apache License 2.0 6 votes vote down vote up
private MappedStatement cloneMappedStatement(MappedStatement old, BoundSql boundSql){
	MappedStatement.Builder builder = new MappedStatement.Builder(
			old.getConfiguration(), old.getId(), new AlwaySqlSource(
					boundSql), old.getSqlCommandType());
       builder.cache(old.getCache());
       builder.databaseId(old.getDatabaseId());
       builder.fetchSize(old.getFetchSize());
       builder.flushCacheRequired(old.isFlushCacheRequired());
       builder.keyGenerator(old.getKeyGenerator());
       builder.keyProperty(join(old.getKeyProperties()));
       builder.keyColumn(join(old.getKeyColumns()));
       builder.lang(old.getLang());
       builder.resource(old.getResource());
       builder.resultMaps(old.getResultMaps());
       builder.resultSetType(old.getResultSetType());
       builder.parameterMap(old.getParameterMap());
       builder.statementType(old.getStatementType());
       builder.timeout(old.getTimeout());
       builder.useCache(old.isUseCache());
       builder.resultOrdered(old.isResultOrdered());
       builder.resulSets(join(old.getResulSets()));

       return builder.build();
}
 
Example 3
Source File: PaginationInterceptor.java    From Shop-for-JavaWeb with MIT License 6 votes vote down vote up
private MappedStatement copyFromMappedStatement(MappedStatement ms,
                                                SqlSource newSqlSource) {
    MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(),
            ms.getId(), newSqlSource, ms.getSqlCommandType());
    builder.resource(ms.getResource());
    builder.fetchSize(ms.getFetchSize());
    builder.statementType(ms.getStatementType());
    builder.keyGenerator(ms.getKeyGenerator());
    if (ms.getKeyProperties() != null) {
        for (String keyProperty : ms.getKeyProperties()) {
            builder.keyProperty(keyProperty);
        }
    }
    builder.timeout(ms.getTimeout());
    builder.parameterMap(ms.getParameterMap());
    builder.resultMaps(ms.getResultMaps());
    builder.cache(ms.getCache());
    return builder.build();
}
 
Example 4
Source File: DeleteBuilder.java    From azeroth with Apache License 2.0 6 votes vote down vote up
/**
 * @param configuration
 * @param entity
 */
public static void build(Configuration configuration, LanguageDriver languageDriver, EntityInfo entity) {
    String msId = entity.getMapperClass().getName() + "." + GeneralSqlGenerator.methodDefines.deleteName();

    // 从参数对象里提取注解信息
    EntityMapper entityMapper = EntityHelper.getEntityMapper(entity.getEntityClass());
    // 生成sql
    String sql = buildDeleteSql(entityMapper);

    SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, entity.getEntityClass());

    MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, msId, sqlSource, SqlCommandType.DELETE);

    MappedStatement statement = statementBuilder.build();
    configuration.addMappedStatement(statement);
}
 
Example 5
Source File: PagePluging.java    From aaden-pay with Apache License 2.0 6 votes vote down vote up
private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
	Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource,
			ms.getSqlCommandType());
	builder.resource(ms.getResource());
	builder.fetchSize(ms.getFetchSize());
	builder.statementType(ms.getStatementType());
	builder.keyGenerator(ms.getKeyGenerator());
	// builder.keyProperty((ms.getKeyProperty()));
	builder.keyProperty(arrayToStr(ms.getKeyProperties()));
	// setStatementTimeout()
	builder.timeout(ms.getTimeout());
	// setStatementResultMap()
	builder.parameterMap(ms.getParameterMap());
	// setStatementResultMap()
	builder.resultMaps(ms.getResultMaps());
	builder.resultSetType(ms.getResultSetType());
	// setStatementCache()
	builder.cache(ms.getCache());
	builder.flushCacheRequired(ms.isFlushCacheRequired());
	builder.useCache(ms.isUseCache());
	return builder.build();
}
 
Example 6
Source File: MybatisSqlInterceptor.java    From taoshop with Apache License 2.0 6 votes vote down vote up
private MappedStatement newMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
    MappedStatement.Builder builder =
            new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, 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());
    builder.resultMaps(ms.getResultMaps());
    builder.resultSetType(ms.getResultSetType());
    builder.cache(ms.getCache());
    builder.flushCacheRequired(ms.isFlushCacheRequired());
    builder.useCache(ms.isUseCache());
 
    return builder.build();
}
 
Example 7
Source File: SundialInterceptor.java    From Milkomeda with MIT License 6 votes vote down vote up
private MappedStatement copyFrom(MappedStatement ms, SqlSource newSqlSource) {
    MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, 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) {
        builder.keyProperty(org.apache.commons.lang3.StringUtils.join(ms.getKeyProperties(), ','));
    }
    builder.timeout(ms.getTimeout());
    builder.parameterMap(ms.getParameterMap());
    builder.resultMaps(ms.getResultMaps());
    builder.resultOrdered(ms.isResultOrdered());
    builder.resultSetType(ms.getResultSetType());
    if (ms.getKeyColumns() != null && ms.getKeyColumns().length > 0) {
        builder.keyColumn(org.apache.commons.lang3.StringUtils.join(ms.getKeyColumns(), ','));
    }
    builder.databaseId(ms.getDatabaseId());
    builder.lang(ms.getLang());
    builder.cache(ms.getCache());
    builder.flushCacheRequired(ms.isFlushCacheRequired());
    builder.useCache(ms.isUseCache());
    return builder.build();
}
 
Example 8
Source File: MapperBuilderAssistant.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private void setStatementResultMap(
    String resultMap,
    Class<?> resultType,
    ResultSetType resultSetType,
    MappedStatement.Builder statementBuilder) {
  resultMap = applyCurrentNamespace(resultMap, true);

  List<ResultMap> resultMaps = new ArrayList<ResultMap>();
  if (resultMap != null) {
    //2.1 resultMap是高级功能
    String[] resultMapNames = resultMap.split(",");
    for (String resultMapName : resultMapNames) {
      try {
        resultMaps.add(configuration.getResultMap(resultMapName.trim()));
      } catch (IllegalArgumentException e) {
        throw new IncompleteElementException("Could not find result map " + resultMapName, e);
      }
    }
  } else if (resultType != null) {
    //2.2 resultType,一般用这个足矣了
    //<select id="selectUsers" resultType="User">
    //这种情况下,MyBatis 会在幕后自动创建一个 ResultMap,基于属性名来映射列到 JavaBean 的属性上。
    //如果列名没有精确匹配,你可以在列名上使用 select 字句的别名来匹配标签。
    //创建一个inline result map, 把resultType设上就OK了,
    //然后后面被DefaultResultSetHandler.createResultObject()使用
    //DefaultResultSetHandler.getRowValue()使用
    ResultMap.Builder inlineResultMapBuilder = new ResultMap.Builder(
        configuration,
        statementBuilder.id() + "-Inline",
        resultType,
        new ArrayList<ResultMapping>(),
        null);
    resultMaps.add(inlineResultMapBuilder.build());
  }
  statementBuilder.resultMaps(resultMaps);

  statementBuilder.resultSetType(resultSetType);
}
 
Example 9
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 10
Source File: MapperBuilderAssistant.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private void setStatementResultMap(
    String resultMap,
    Class<?> resultType,
    ResultSetType resultSetType,
    MappedStatement.Builder statementBuilder) {
  resultMap = applyCurrentNamespace(resultMap, true);

  List<ResultMap> resultMaps = new ArrayList<ResultMap>();
  if (resultMap != null) {
    //2.1 resultMap是高级功能
    String[] resultMapNames = resultMap.split(",");
    for (String resultMapName : resultMapNames) {
      try {
        resultMaps.add(configuration.getResultMap(resultMapName.trim()));
      } catch (IllegalArgumentException e) {
        throw new IncompleteElementException("Could not find result map " + resultMapName, e);
      }
    }
  } else if (resultType != null) {
    //2.2 resultType,一般用这个足矣了
    //<select id="selectUsers" resultType="User">
    //这种情况下,MyBatis 会在幕后自动创建一个 ResultMap,基于属性名来映射列到 JavaBean 的属性上。
    //如果列名没有精确匹配,你可以在列名上使用 select 字句的别名来匹配标签。
    //创建一个inline result map, 把resultType设上就OK了,
    //然后后面被DefaultResultSetHandler.createResultObject()使用
    //DefaultResultSetHandler.getRowValue()使用
    ResultMap.Builder inlineResultMapBuilder = new ResultMap.Builder(
        configuration,
        statementBuilder.id() + "-Inline",
        resultType,
        new ArrayList<ResultMapping>(),
        null);
    resultMaps.add(inlineResultMapBuilder.build());
  }
  statementBuilder.resultMaps(resultMaps);

  statementBuilder.resultSetType(resultSetType);
}
 
Example 11
Source File: InsertBuilder.java    From azeroth with Apache License 2.0 5 votes vote down vote up
/**
 * @param configuration
 * @param entity
 */
public static void build(Configuration configuration, LanguageDriver languageDriver, EntityInfo entity) {

    String[] names = GeneralSqlGenerator.methodDefines.insertName().split(",");
    for (String name : names) {
        String msId = entity.getMapperClass().getName() + "." + name;

        // 从参数对象里提取注解信息
        EntityMapper entityMapper = EntityHelper.getEntityMapper(entity.getEntityClass());
        // 生成sql
        String sql = buildInsertSql(entityMapper, name.endsWith("Selective"));

        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, entity.getEntityClass());

        MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, msId, sqlSource, SqlCommandType.INSERT);

        KeyGenerator keyGenerator = entityMapper.autoId() ? new Jdbc3KeyGenerator() : new NoKeyGenerator();
        statementBuilder.keyGenerator(keyGenerator)//
                .keyProperty(entityMapper.getIdColumn().getProperty())//
                .keyColumn(entityMapper.getIdColumn().getColumn());

        MappedStatement statement = statementBuilder.build();

        configuration.addMappedStatement(statement);
    }

}
 
Example 12
Source File: AbstractMethodBuilder.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
public void build(Configuration configuration, LanguageDriver languageDriver,EntityInfo entity) {
	
	for (String name : methodNames()) {			
		String msId = entity.getMapperClass().getName() + "." + name;
		
		// 从参数对象里提取注解信息
		EntityMapper entityMapper = EntityHelper.getEntityMapper(entity.getEntityClass());
		// 生成sql
		String sql = buildSQL(entityMapper,name.endsWith("Selective"));
		
		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, entity.getEntityClass());
		
		MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, msId, sqlSource,sqlCommandType());
		
		//主键策略
		if(sqlCommandType() == SqlCommandType.INSERT){
			KeyGenerator keyGenerator = entityMapper.autoId() ? new Jdbc3KeyGenerator() : new NoKeyGenerator();
			statementBuilder.keyGenerator(keyGenerator)//
	                        .keyProperty(entityMapper.getIdColumn().getProperty())//
	                        .keyColumn(entityMapper.getIdColumn().getColumn());
		}
		
		MappedStatement statement = statementBuilder.build();
		//
		setResultType(configuration, statement, entity.getEntityClass());
		configuration.addMappedStatement(statement);
	}
	
}
 
Example 13
Source File: PageInterceptor.java    From Zebra with Apache License 2.0 5 votes vote down vote up
public MappedStatement buildMappedStatement(MappedStatement ms, SqlSource newSqlSource, String id,
		List<ResultMap> resultMaps) {
	MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), id, newSqlSource,
			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());
	builder.resultMaps(resultMaps);
	builder.resultSetType(ms.getResultSetType());
	builder.cache(ms.getCache());
	builder.flushCacheRequired(ms.isFlushCacheRequired());
	builder.useCache(ms.isUseCache());

	return builder.build();
}
 
Example 14
Source File: MapperBuilderAssistant.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private void setStatementCache(
    boolean isSelect,
    boolean flushCache,
    boolean useCache,
    Cache cache,
    MappedStatement.Builder statementBuilder) {
  flushCache = valueOrDefault(flushCache, !isSelect);
  useCache = valueOrDefault(useCache, isSelect);
  statementBuilder.flushCacheRequired(flushCache);
  statementBuilder.useCache(useCache);
  statementBuilder.cache(cache);
}
 
Example 15
Source File: MapperBuilderAssistant.java    From mybaties with Apache License 2.0 4 votes vote down vote up
private void setStatementTimeout(Integer timeout, MappedStatement.Builder statementBuilder) {
  if (timeout == null) {
    timeout = configuration.getDefaultStatementTimeout();
  }
  statementBuilder.timeout(timeout);
}
 
Example 16
Source File: CacheHandler.java    From azeroth with Apache License 2.0 4 votes vote down vote up
private MappedStatement getQueryIdsMappedStatementForUpdateCache(MappedStatement mt,
                                                                 EntityInfo entityInfo) {
    String msId = mt.getId() + QUERY_IDS_SUFFIX;

    MappedStatement statement = null;
    Configuration configuration = mt.getConfiguration();
    try {
        statement = configuration.getMappedStatement(msId);
        if (statement != null) { return statement; }
    } catch (Exception e) {
    }

    synchronized (configuration) {
        if (configuration.hasStatement(msId)) { return configuration.getMappedStatement(msId); }

        String sql = entityInfo.getMapperSqls().get(mt.getId());

        if (StringUtils.isNotBlank(sql)) {
            if (!sql.toLowerCase().contains(entityInfo.getTableName().toLowerCase())) {
                return null;
            }
            sql = "select " + entityInfo.getIdColumn() + " from " + entityInfo.getTableName()
                    + " WHERE " + sql.split(WHERE_REGEX)[1];
            sql = String.format(SqlTemplate.SCRIPT_TEMAPLATE, sql);
        } else {
            sql = PARSE_SQL_ERROR_DEFAULT;
        }
        SqlSource sqlSource = configuration.getDefaultScriptingLanguageInstance()
                .createSqlSource(configuration, sql, Object.class);

        MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration,
                msId, sqlSource, SqlCommandType.SELECT);

        statementBuilder.resource(mt.getResource());
        statementBuilder.fetchSize(mt.getFetchSize());
        statementBuilder.statementType(mt.getStatementType());
        statementBuilder.parameterMap(mt.getParameterMap());
        statement = statementBuilder.build();

        List<ResultMap> resultMaps = new ArrayList<ResultMap>();

        String id = msId + "-Inline";
        ResultMap.Builder builder = new ResultMap.Builder(configuration, id,
                entityInfo.getIdType(), new ArrayList<ResultMapping>(), true);
        resultMaps.add(builder.build());
        MetaObject metaObject = SystemMetaObject.forObject(statement);
        metaObject.setValue("resultMaps", Collections.unmodifiableList(resultMaps));

        configuration.addMappedStatement(statement);

        return statement;
    }
}
 
Example 17
Source File: IndexingPlugin.java    From scaffold-cloud with MIT License 4 votes vote down vote up
protected void printWarn(Configuration configuration, MappedStatement mappedStatement, BoundSql boundSql,
                            Connection connection, Object parameter ) throws Exception {
	PreparedStatement stmt = null;
	ResultSet rs = null;
	try {
		StringBuilder explain = new StringBuilder("EXPLAIN ");
		explain.append(boundSql.getSql());
		String sqlExplain = explain.toString();
		StaticSqlSource sqlsource = new StaticSqlSource(configuration, sqlExplain, boundSql.getParameterMappings());
		MappedStatement.Builder builder = new MappedStatement.Builder(configuration, "explain_sql", sqlsource,
				SqlCommandType.SELECT);
		builder.resultMaps(mappedStatement.getResultMaps()).resultSetType(mappedStatement.getResultSetType())
				.statementType(mappedStatement.getStatementType());
		MappedStatement query_statement = builder.build();
		DefaultParameterHandler handler = new DefaultParameterHandler(query_statement, parameter, boundSql);
		stmt = connection.prepareStatement(sqlExplain);
		handler.setParameters(stmt);
		rs = stmt.executeQuery();
		int count = 0;
		while ( rs.next() ) {
			count++;
			String type = rs.getString("type");
			if ( "ALL".equals(type) ) {
				logger.error(boundSql.getSql());
				logger.error("使用了全表扫描的方式!");
			}
			if ( rs.getString("key") == null ) {
				logger.error(boundSql.getSql());
				logger.warn("没有使用索引,可能存在性能问题!");
			}
			int rows = rs.getInt("rows");
			if ( rows > 500 ) {
				logger.error(boundSql.getSql());
				logger.trace("影响行数为" + rows + ",可能存在性能问题!");
			}
			String extra = rs.getString("Extra");
			if ( extra != null && extra.contains("Using temporary") ) {
				logger.error(boundSql.getSql());
				logger.warn("使用临时表,可能存在性能问题!");
			}
			if ( extra != null && extra.contains("Using filesort") ) {
				logger.error(boundSql.getSql());
				logger.warn("使用文件排序,可能存在性能问题!");
			}
		}
		if ( count > 1 ) {
			logger.error(boundSql.getSql());
			logger.error("您的sql语句中用了子查询或者连接查询,为了保证性能和可扩展性,请不要使用子查询或者连接查询,尽量使用n+1查询或者字段冗余!");
		}

	} catch ( Exception e ) {

	} finally {
		if ( rs != null ) {
			rs.close();
			rs = null;
		}
		if ( stmt != null ) {
			stmt.close();
			stmt = null;
		}
	}
}
 
Example 18
Source File: GetByPrimaryKeyBuilder.java    From azeroth with Apache License 2.0 4 votes vote down vote up
/**
 * @param configuration
 * @param entity
 */
public static void build(Configuration configuration, LanguageDriver languageDriver, EntityInfo entity) {
    String msId = entity.getMapperClass().getName() + "." + GeneralSqlGenerator.methodDefines.selectName();

    EntityMapper entityMapper = EntityHelper.getEntityMapper(entity.getEntityClass());

    String sql = buildGetByIdSql(entityMapper);

    SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, entity.getEntityClass());

    MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, msId, sqlSource, SqlCommandType.SELECT);

    // 将返回值修改为实体类型
    MappedStatement statement = statementBuilder.build();
    setResultType(configuration, statement, entity.getEntityClass());

    configuration.addMappedStatement(statement);
}
 
Example 19
Source File: MapperBuilderAssistant.java    From mybatis with Apache License 2.0 4 votes vote down vote up
public MappedStatement addMappedStatement(
    String id,
    SqlSource sqlSource,
    StatementType statementType,
    SqlCommandType sqlCommandType,
    Integer fetchSize,
    Integer timeout,
    String parameterMap,
    Class<?> parameterType,
    String resultMap,
    Class<?> resultType,
    ResultSetType resultSetType,
    boolean flushCache,
    boolean useCache,
    boolean resultOrdered,
    KeyGenerator keyGenerator,
    String keyProperty,
    String keyColumn,
    String databaseId,
    LanguageDriver lang,
    String resultSets) {
  
  if (unresolvedCacheRef) {
    throw new IncompleteElementException("Cache-ref not yet resolved");
  }
  
  //为id加上namespace前缀
  id = applyCurrentNamespace(id, false);
  //是否是select语句
  boolean isSelect = sqlCommandType == SqlCommandType.SELECT;

  //又是建造者模式
  MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, id, sqlSource, sqlCommandType);
  statementBuilder.resource(resource);
  statementBuilder.fetchSize(fetchSize);
  statementBuilder.statementType(statementType);
  statementBuilder.keyGenerator(keyGenerator);
  statementBuilder.keyProperty(keyProperty);
  statementBuilder.keyColumn(keyColumn);
  statementBuilder.databaseId(databaseId);
  statementBuilder.lang(lang);
  statementBuilder.resultOrdered(resultOrdered);
  statementBuilder.resulSets(resultSets);
  setStatementTimeout(timeout, statementBuilder);

  //1.参数映射
  setStatementParameterMap(parameterMap, parameterType, statementBuilder);
  //2.结果映射
  setStatementResultMap(resultMap, resultType, resultSetType, statementBuilder);
  setStatementCache(isSelect, flushCache, useCache, currentCache, statementBuilder);

  MappedStatement statement = statementBuilder.build();
  //建造好调用configuration.addMappedStatement
  configuration.addMappedStatement(statement);
  return statement;
}
 
Example 20
Source File: MapperBuilderAssistant.java    From mybatis with Apache License 2.0 4 votes vote down vote up
private void setStatementTimeout(Integer timeout, MappedStatement.Builder statementBuilder) {
  if (timeout == null) {
    timeout = configuration.getDefaultStatementTimeout();
  }
  statementBuilder.timeout(timeout);
}