【Mybatis源码阅读之MappedStatement 】

本文解析了 MyBatis 中 MappedStatement 类的作用及其构造过程。MappedStatement 是 MyBatis 中执行 SQL 语句的核心类,通过 Builder 模式进行构建,包含了资源路径、配置信息、ID、参数映射等属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



 

源码阅读:

package org.apache.ibatis.mapping;

 

import org.apache.ibatis.cache.Cache;

import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;

import org.apache.ibatis.executor.keygen.KeyGenerator;

import org.apache.ibatis.executor.keygen.NoKeyGenerator;

import org.apache.ibatis.session.Configuration;

 

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

 

public class MappedStatement {

 

  private String resource;

  private Configuration configuration;

  private String id;

  private Integer fetchSize;

  private Integer timeout;

  private StatementType statementType;

  private ResultSetType resultSetType;

  private SqlSource sqlSource;

  private Cache cache;

  private ParameterMap parameterMap;

  private List<ResultMap> resultMaps;

  private boolean flushCacheRequired;

  private boolean useCache;

  private SqlCommandType sqlCommandType;

  private KeyGenerator keyGenerator;

  private String keyProperty;

  private boolean hasNestedResultMaps;

 

  private MappedStatement() {

  }

 

  public static class Builder {

    private MappedStatement mappedStatement = new MappedStatement();

 

    public Builder(Configuration configuration, String id, SqlSource sqlSource,

                SqlCommandType sqlCommandType) {

      mappedStatement.configuration = configuration;

      mappedStatement.id = id;

      mappedStatement.sqlSource = sqlSource;

      mappedStatement.statementType = StatementType.PREPARED;

      mappedStatement.parameterMap = new ParameterMap.Builder(configuration,

              "defaultParameterMap", Object.class, new ArrayList<ParameterMapping>()).build();

      mappedStatement.resultMaps = new ArrayList<ResultMap>();

      mappedStatement.timeout = configuration.getDefaultStatementTimeout();

      mappedStatement.sqlCommandType = sqlCommandType;

      mappedStatement.keyGenerator = configuration.isUseGeneratedKeys()

          && SqlCommandType.INSERT.equals(sqlCommandType) ? new Jdbc3KeyGenerator() : new NoKeyGenerator();

    }

 

    public Builder resource(String resource) {

      mappedStatement.resource = resource;

      return this;

    }

 

    public String id() {

      return mappedStatement.id;

    }

 

    public Builder parameterMap(ParameterMap parameterMap) {

      mappedStatement.parameterMap = parameterMap;

      return this;

    }

 

    public Builder resultMaps(List<ResultMap> resultMaps) {

      mappedStatement.resultMaps = resultMaps;

      for (ResultMap resultMap : resultMaps) {

        mappedStatement.hasNestedResultMaps = mappedStatement.hasNestedResultMaps || resultMap.hasNestedResultMaps();

      }

      return this;

    }

 

    public Builder fetchSize(Integer fetchSize) {

      mappedStatement.fetchSize = fetchSize;

      return this;

    }

 

    public Builder timeout(Integer timeout) {

      mappedStatement.timeout = timeout;

      return this;

    }

 

    public Builder statementType(StatementType statementType) {

      mappedStatement.statementType = statementType;

      return this;

    }

 

    public Builder resultSetType(ResultSetType resultSetType) {

      mappedStatement.resultSetType = resultSetType;

      return this;

    }

 

    public Builder cache(Cache cache) {

      mappedStatement.cache = cache;

      return this;

    }

 

    public Builder flushCacheRequired(boolean flushCacheRequired) {

      mappedStatement.flushCacheRequired = flushCacheRequired;

      return this;

    }

 

    public Builder useCache(boolean useCache) {

      mappedStatement.useCache = useCache;

      return this;

    }

 

    public Builder keyGenerator(KeyGenerator keyGenerator) {

      mappedStatement.keyGenerator = keyGenerator;

      return this;

    }

 

    public Builder keyProperty(String keyProperty) {

      mappedStatement.keyProperty = keyProperty;

      return this;

    }

 

    public MappedStatement build() {

      assert mappedStatement.configuration != null;

      assert mappedStatement.id != null;

      assert mappedStatement.sqlSource != null;

      mappedStatement.resultMaps = Collections.unmodifiableList(mappedStatement.resultMaps);

      return mappedStatement;

    }

 

  }

 

  public String getKeyProperty() {

    return keyProperty;

  }

 

  public KeyGenerator getKeyGenerator() {

    return keyGenerator;

  }

 

  public SqlCommandType getSqlCommandType() {

    return sqlCommandType;

  }

 

  public String getResource() {

    return resource;

  }

 

  public Configuration getConfiguration() {

    return configuration;

  }

 

  public String getId() {

    return id;

  }

 

  public boolean hasNestedResultMaps() {

    return hasNestedResultMaps;

  }

 

  public Integer getFetchSize() {

    return fetchSize;

  }

 

  public Integer getTimeout() {

    return timeout;

  }

 

  public StatementType getStatementType() {

    return statementType;

  }

 

  public ResultSetType getResultSetType() {

    return resultSetType;

  }

 

  public SqlSource getSqlSource() {

    return sqlSource;

  }

 

  public ParameterMap getParameterMap() {

    return parameterMap;

  }

 

  public List<ResultMap> getResultMaps() {

    return resultMaps;

  }

 

  public Cache getCache() {

    return cache;

  }

 

  public boolean isFlushCacheRequired() {

    return flushCacheRequired;

  }

 

  public boolean isUseCache() {

    return useCache;

  }

 

  public BoundSql getBoundSql(Object parameterObject) {

    BoundSql boundSql = sqlSource.getBoundSql(parameterObject);

    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();

    if (parameterMappings == null || parameterMappings.size() <= 0) {

      boundSql = new BoundSql(configuration, boundSql.getSql(), parameterMap.getParameterMappings(), parameterObject);

    }

    

    // check for nested result maps in parameter mappings (issue #30)

    for (ParameterMapping pm : boundSql.getParameterMappings()) {

        String rmId = pm.getResultMapId();

        if (rmId != null) {

            ResultMap rm = configuration.getResultMap(rmId);

            if (rm != null) {

                hasNestedResultMaps |= rm.hasNestedResultMaps();

            }

        }

    }

    

    return boundSql;

  }

 

 

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值