MyBatisMyBatis源码分析-SQL告句子执行之整体流程

  MyBatis 是支撑定制化
SQL、存储过程与高档映射的精美的持久层框架。MyBatis 避免了几有的
JDBC 代码和手动设置参数和取得结果集。MyBatis
可以本着配置以及原生Map使用简单的 XML 或注解,将接口及 Java 的 POJOs(Plain
Old Java Objects,普通的
Java对象)映射成数据库被的笔录。如何新建MyBatis源码工要点击MyBatis源码分析-IDEA新建MyBatis源码工程。

  MyBatis框架主要完成的凡以下2件事情:

  1. 依据JDBC规范建立与数据库的连年。
  2. 由此反射打通Java对象以及数据库参数交互中互相转换的干。

  MyBatis框架是如出一辙种典型的交互式框架,先准备好互相的必要条件,然后构建一个互相的条件,在彼此环境面临分会话,在对话中以及数据库进行互动数据。

1 MyBatis主要的切近

  • Configuration      
     MyBatis所有的安排信息还保持以Configuration对象中。
  • SqlSession          
     作为MyBatis工作的要顶层API,表示与数据库交互的对话,完成必要数据库增删改查功能
  • Executor               MyBatis执行器,是MyBatis
    调度的核心,负责SQL语句之扭转和查询缓存的保护
  • StatementHandler 封装了JDBC Statement操作,负责对JDBC statement
    的操作,如设置参数、将Statement结果集转换成为List集合。
  • ParameterHandler  负责对用户传递的参数转换成JDBC Statement
    所待的参数,
  • ResultSetHandler  
    负责将JDBC返回的ResultSet结果集对象转换成List类型的集;
  • TypeHandler          负责java数据类型和jdbc数据类型之间的炫耀和换
  • MappedStatement
     MappedStatement维护了扳平长条<select|update|delete|insert>节点的包,
  • SqlSource            
     负责根据用户传递的parameterObject,动态地生成SQL语句,将消息打包到BoundSql对象被,并回
  • BoundSql              表示动态变化的SQL语句以及对应的参数信息

  以上几乎独八九不离十在SQL操作着都见面涉嫌,在SQL操作着根本关注下SQL参数什么时候写入和结果集怎么换为Java对象,这片独过程恰好对应之类似是PreparedStatementHandler和ResultSetHandler类。

MyBatis 1

(图片来源《深入明mybatis原理》
MyBatis的架构设计以及实例分析)

2 SQL执行流程

  MyBatis主要设计目的或为吃咱当尽SQL时对输入输出的数量的管住更加便民,所以方便之叫咱写有SQL和便民的获得SQL的尽结果是MyBatis的中心竞争力。下面就是因此一个事例来打源码角度看一下SQL的完好执行流程。

新建配置文件conf.xml:

MyBatis 2MyBatis 3

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

    <settings>
        <setting name="cacheEnabled" value="true"/>
        <setting name="lazyLoadingEnabled" value="false"/>
        <!--setting name="logImpl" value="STDOUT_LOGGING"/--> <!-- 日志 -->
    </settings>

    <typeAliases>
        <typeAlias type="com.luoxn28.dao.User" alias="User"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" /> <!-- 声明使用那种事务管理机制 JDBC/MANAGED -->
            <!-- 配置数据库连接信息 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://192.168.1.150:3306/xxx" />
                <property name="username" value="xxx" />
                <property name="password" value="xxx" />
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="userMapper.xml"/>
    </mappers>

</configuration>

conf.xml

先是成立数据表,这里虽盖user表为例

DROP TABLE IF EXISTS user;
CREATE TABLE user (
  id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(32) NOT NULL,
  password VARCHAR(32) NOT NULL,
  sex int,
  email VARCHAR(32),
  phone VARCHAR(16),
  admin VARCHAR(16)
);

下一场新建及数据表对应之类User:

MyBatis 4MyBatis 5

/**
 * User - 用户类
 */
public class User {

    public static final int MAN  = 0;   // 男生
    public static final int WOMAN = 1;  // 女生
    public static final int OTHER = 2;  // 其他

    private int id;             // 用户id
    private String name;        // 用户名
    private String password;    // 用户密码
    private int sex;            // 用户性别
    private String email;       // 用户邮箱
    private String phone;       // 用户手机
    private String admin;       // 用户是否是管理员,"admin"表示是管理员,其他为普通用户

    public User() { }

    public User(String name, String password, int sex, String email, String phone) {
        this.name = name;
        this.password = password;
        this.sex = sex;
        this.email = email;
        this.phone = phone;
        this.admin = "";
    }
    public User(String name, String password, String sex, String email, String phone) {
        this.name = name;
        this.password = password;
        setSex(sex); // this.sex = sex;
        this.email = email;
        this.phone = phone;
        this.admin = "";
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public void setSex(String sexStr) {
        int sex = Integer.valueOf(sexStr);
        switch (Integer.valueOf(sexStr)) {
            case 0: {
                this.sex = MAN;
                break;
            }
            case 1: {
                this.sex = WOMAN;
                break;
            }
            default: {
                this.sex = OTHER;
                break;
            }
        }
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAdmin() {
        return admin;
    }

    public void setAdmin(String admin) {
        this.admin = admin;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", sex=" + sex +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                ", admin='" + admin + '\'' +
                '}';
    }

}

User

双重新建usre表的安排文件:

MyBatis 6MyBatis 7

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.luoxn28.dao.UserDao">

    <select id="getById" parameterType="int" resultType="User">
        SELECT * FROM user WHERE id=#{id}; <!-- #{xxx} xxx为类中的数据域名称 -->
    </select>

    <select id="getAll" resultType="com.luoxn28.dao.User">
        SELECT * FROM user;
    </select>
</mapper>

userMapper.xml

最后新建测试类:

/**
 * MyBatis测试类
 */
public class TestMain {

    public static void main(String[] args) throws IOException {
        String resouce = "conf.xml";
        InputStream is = Resources.getResourceAsStream(resouce);

        // 构建sqlSession工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
                // 获取sqlSession
        SqlSession session = sqlSessionFactory.openSession();

        User user;

        try {
            /**
             * 第一种方式: 直接执行已映射的 SQL 语句
             */
            String statement = "com.luoxn28.dao.UserDao.getById";
            user = session.selectOne(statement, 1);
            System.out.println(user);
        }
        finally {
            session.close();
        }

        /**
         * 第二种方式: 执行更清晰和类型安全的代码
         */
//        UserDao userDao = session.getMapper(UserDao.class);
//        user = userDao.getById(1);
//        System.out.println(user);
    }

}

  由于我们解析的是SQL的行流程,那即便着重关注下 user = session.selectOne(statement, 1);
这行代码~ 注意,传进的参数是1。

  session是DefaultSqlSession类型的,因为sqlSessionFactory默认生成的SqlSession是DefaultSqlSession类型。selectOne()会调用selectList()。

// DefaultSqlSession类
public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
    try {
        MappedStatement ms = configuration.getMappedStatement(statement);
        // CURD操作是交给Excetor去处理的
        return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
    } catch (Exception e) {
        throw ExceptionFactory.wrapException("Error querying database.  Cause: " + e, e);
    } finally {
        ErrorContext.instance().reset();
    }
}

  在DefaultSqlSession.selectList中的各种CURD操作都是通多Executor进行的,这里executor的项目是CachingExecutor,接着跳反到其中的query方法被。

// CachingExecutor 类
public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
    BoundSql boundSql = ms.getBoundSql(parameterObject);  // 获取绑定的sql命令,比如"SELECT * FROM xxx"
    CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);
    return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}

  getBoundSql为了博取绑定的sql命令,在创造了cacheKey之后,就上到CachingExecutor
类中的别样一个query方法中。

// CachingExecutor 类
@Override
public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql)
        throws SQLException {
    Cache cache = ms.getCache();
    if (cache != null) {
        flushCacheIfRequired(ms);
        if (ms.isUseCache() && resultHandler == null) {
            ensureNoOutParams(ms, parameterObject, boundSql);
            @SuppressWarnings("unchecked")
            List<E> list = (List<E>) tcm.getObject(cache, key);
            if (list == null) {
                list = delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
                tcm.putObject(cache, key, list); // issue #578 and #116
            }
            return list;
        }
    }
    return delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}

  这里确确实实实施query操作的是SimplyExecutor代理来好的,接着便进来及了SimplyExecutor的父类BaseExecutor的query方法吃。

// SimplyExecutor的父类BaseExecutor类
@SuppressWarnings("unchecked")
@Override
public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
    ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId());
    if (closed) {
        throw new ExecutorException("Executor was closed.");
    }
    if (queryStack == 0 && ms.isFlushCacheRequired()) {
        clearLocalCache();
    }
    List<E> list;
    try {
        queryStack++;
        /**
         * localCache是一级缓存,如果找不到就调用queryFromDatabase从数据库中查找
         */
        list = resultHandler == null ? (List<E>) localCache.getObject(key) : null;
        if (list != null) {
            handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);
        } else {
            list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
        }
    } finally {
        queryStack--;
    }
    if (queryStack == 0) {
        for (DeferredLoad deferredLoad : deferredLoads) {
            deferredLoad.load();
        }
        // issue #601
        deferredLoads.clear();
        if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {
            // issue #482
            clearLocalCache();
        }
    }
    return list;
}

  因为是第一糟糕SQL查询操作,所以会见调用queryFromDatabase方法来执行查询。

// SimplyExecutor的父类BaseExecutor类
private <E> List<E> queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
    List<E> list;
    localCache.putObject(key, EXECUTION_PLACEHOLDER);
    try {
        list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql);
    } finally {
        localCache.removeObject(key);
    }
    localCache.putObject(key, list);
    if (ms.getStatementType() == StatementType.CALLABLE) {
        localOutputParameterCache.putObject(key, parameter);
    }
    return list;
}

  从数据库被查询数据,进入到SimplyExecutor中进行操作。

// SimplyExecutor类
public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
    Statement stmt = null;
    try {
        Configuration configuration = ms.getConfiguration();
        StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
        // 子流程1:SQL查询参数的设置
        stmt = prepareStatement(handler, ms.getStatementLog());
        // StatementHandler封装了Statement
        // 子流程2:SQL查询操作和结果集的封装
        return handler.<E>query(stmt);
    } finally {
        closeStatement(stmt);
    }
}

  注意,在prepareStatement方法吃会进展SQL查询参数的设置,也就算是咱最初步传递进入的参数,其值为1。handler.<E>query(stmt)方法被会进展实际的SQL查询操作及结果集的包装(封装成Java对象)。当流程走及此地经常,程序已经压栈有得深度了,因为属下程序分析会兵分点儿路程,一方面深入到SQL查询以及结果集的设置子流程1蒙,然后还深刻到SQL查询操作与结果集的封装子流程2,因为还会见回到这里,所以便来平等布置调用栈的特写吧:

MyBatis 8

子流程1:SQL查询参数的设置

// SimplyExecutor类
private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
    Statement stmt;
    // 获取一个Connection
    Connection connection = getConnection(statementLog);
    stmt = handler.prepare(connection, transaction.getTimeout());
    handler.parameterize(stmt); // 设置SQL查询中的参数值
    return stmt;
}

  通过getConnection方法来获得一个Connection,调用prepare方法来赢得一个Statement(这里的handler类型是RoutingStatementHandler,RoutingStatementHandler的prepare方法调用的是PrepareStatementHandler的prepare方法,因为PrepareStatementHandler并没覆盖其父类的prepare方法,其实说到底调用的是BaseStatementHandler中之prepare方法。是休是绕晕了,那就重新看一样周吧
🙂 )。调用parameterize方法来安SQL的参数值(这里最后调用的是PrepareStatementHandler中的parameterize方法,而PrepareStatementHandler.parameterize方法调用的是DefaultParameterHandler中之setParameters方法)。

// PrepareStatementHandler类
@Override
public void parameterize(Statement statement) throws SQLException {
    parameterHandler.setParameters((PreparedStatement) statement);
}

// DefaultParameterHandler类
@Override
public void setParameters(PreparedStatement ps) {
    ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    if (parameterMappings != null) {
        for (int i = 0; i < parameterMappings.size(); i++) {
            ParameterMapping parameterMapping = parameterMappings.get(i);
            if (parameterMapping.getMode() != ParameterMode.OUT) {
                Object value;
                String propertyName = parameterMapping.getProperty();
                if (boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional params
                    value = boundSql.getAdditionalParameter(propertyName);
                } else if (parameterObject == null) {
                    value = null;
                } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
                    value = parameterObject;
                } else {
                    MetaObject metaObject = configuration.newMetaObject(parameterObject);
                    value = metaObject.getValue(propertyName);
                }
                TypeHandler typeHandler = parameterMapping.getTypeHandler();
                JdbcType jdbcType = parameterMapping.getJdbcType();
                if (value == null && jdbcType == null) {
                    jdbcType = configuration.getJdbcTypeForNull();
                }
                try {
                    typeHandler.setParameter(ps, i + 1, value, jdbcType);
                } catch (TypeException e) {
                    throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
                } catch (SQLException e) {
                    throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
                }
            }
        }
    }
}

  到这里了,已经让Statement设置了最初传递进入的参数(值吗1)了,那么就分析流程2:

流程2:SQL查询以及结果集的装

// RoutingStatementHandler类
@Override
public <E> List<E> query(Statement statement) throws SQLException {
    return delegate.<E>query(statement);
}

// RoutingStatementHandler类
@Override
public <E> List<E> query(Statement statement) throws SQLException {
    // 这里就到了熟悉的PreparedStatement了
    PreparedStatement ps = (PreparedStatement) statement;
    // 执行SQL查询操作
    ps.execute();
    // 结果交给ResultHandler来处理
    return resultSetHandler.<E> handleResultSets(ps);
}

// DefaultResultSetHandler类(封装返回值,将查询结果封装成Object对象)
@Override
public List<Object> handleResultSets(Statement stmt) throws SQLException {
    ErrorContext.instance().activity("handling results").object(mappedStatement.getId());

    final List<Object> multipleResults = new ArrayList<Object>();

    int resultSetCount = 0;
    ResultSetWrapper rsw = getFirstResultSet(stmt);

    List<ResultMap> resultMaps = mappedStatement.getResultMaps();
    int resultMapCount = resultMaps.size();
    validateResultMapsCount(rsw, resultMapCount);
    while (rsw != null && resultMapCount > resultSetCount) {
        ResultMap resultMap = resultMaps.get(resultSetCount);
        handleResultSet(rsw, resultMap, multipleResults, null);
        rsw = getNextResultSet(stmt);
        cleanUpAfterHandlingResultSet();
        resultSetCount++;
    }

    String[] resultSets = mappedStatement.getResultSets();
    if (resultSets != null) {
        while (rsw != null && resultSetCount < resultSets.length) {
            ResultMapping parentMapping = nextResultMaps.get(resultSets[resultSetCount]);
            if (parentMapping != null) {
                String nestedResultMapId = parentMapping.getNestedResultMapId();
                ResultMap resultMap = configuration.getResultMap(nestedResultMapId);
                handleResultSet(rsw, resultMap, null, parentMapping);
            }
            rsw = getNextResultSet(stmt);
            cleanUpAfterHandlingResultSet();
            resultSetCount++;
        }
    }

    return collapseSingleResultList(multipleResults);
}

  ResultSetWrapper是ResultSet的卷入类,调用getFirstResultSet方法赢得第一个ResultSet,同时获取数据库的MetaData数据,包括数据表列名、列的类别、类序号等,这些消息还存储在ResultSetWrapper类中了。然后调用handleResultSet方法来来展开结果集的包。

// DefaultResultSetHandler类
private void handleResultSet(ResultSetWrapper rsw, ResultMap resultMap, List<Object> multipleResults, ResultMapping parentMapping) throws SQLException {
    try {
        if (parentMapping != null) {
            handleRowValues(rsw, resultMap, null, RowBounds.DEFAULT, parentMapping);
        } else {
            if (resultHandler == null) {
                DefaultResultHandler defaultResultHandler = new DefaultResultHandler(objectFactory);
                handleRowValues(rsw, resultMap, defaultResultHandler, rowBounds, null);
                multipleResults.add(defaultResultHandler.getResultList());
            } else {
                handleRowValues(rsw, resultMap, resultHandler, rowBounds, null);
            }
        }
    } finally {
        // issue #228 (close resultsets)
        closeResultSet(rsw.getResultSet());
    }
}

  这里调用handleRowValues方法来上行值的安装:

// DefaultResultSetHandler类
public void handleRowValues(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, ResultMapping parentMapping) throws SQLException {
    if (resultMap.hasNestedResultMaps()) {
        ensureNoRowBounds();
        checkResultHandler();
        handleRowValuesForNestedResultMap(rsw, resultMap, resultHandler, rowBounds, parentMapping);
    } else {
        // 封装数据
        handleRowValuesForSimpleResultMap(rsw, resultMap, resultHandler, rowBounds, parentMapping);
    }
}

// DefaultResultSetHandler类
// 封装数据
private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, ResultMapping parentMapping)
        throws SQLException {
    DefaultResultContext<Object> resultContext = new DefaultResultContext<Object>();
    skipRows(rsw.getResultSet(), rowBounds);
    while (shouldProcessMoreRows(resultContext, rowBounds) && rsw.getResultSet().next()) {
        ResultMap discriminatedResultMap = resolveDiscriminatedResultMap(rsw.getResultSet(), resultMap, null);
        Object rowValue = getRowValue(rsw, discriminatedResultMap);
        storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
    }
}

// DefaultResultSetHandler类
private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap) throws SQLException {
    final ResultLoaderMap lazyLoader = new ResultLoaderMap();
    // createResultObject为新创建的对象,数据表对应的类
    Object resultObject = createResultObject(rsw, resultMap, lazyLoader, null);
    if (resultObject != null && !hasTypeHandlerForResultObject(rsw, resultMap.getType())) {
        final MetaObject metaObject = configuration.newMetaObject(resultObject);
        boolean foundValues = !resultMap.getConstructorResultMappings().isEmpty();
        if (shouldApplyAutomaticMappings(resultMap, false)) {
            // 这里把数据填充进去,metaObject中包含了resultObject信息
            foundValues = applyAutomaticMappings(rsw, resultMap, metaObject, null) || foundValues;
        }
        foundValues = applyPropertyMappings(rsw, resultMap, metaObject, lazyLoader, null) || foundValues;
        foundValues = lazyLoader.size() > 0 || foundValues;
        resultObject = foundValues ? resultObject : null;
        return resultObject;
    }
    return resultObject;
}

// DefaultResultSetHandler类(把ResultSet中查询结果填充到JavaBean中)
private boolean applyAutomaticMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String columnPrefix) throws SQLException {
    List<UnMappedColumnAutoMapping> autoMapping = createAutomaticMappings(rsw, resultMap, metaObject, columnPrefix);
    boolean foundValues = false;
    if (autoMapping.size() > 0) {
                // 这里进行for循环调用,因为user表中总共有7项,所以也就调用7次
        for (UnMappedColumnAutoMapping mapping : autoMapping) {
            // 这里将esultSet中查询结果转换为对应的实际类型
            final Object value = mapping.typeHandler.getResult(rsw.getResultSet(), mapping.column);
            if (value != null) {
                foundValues = true;
            }
            if (value != null || (configuration.isCallSettersOnNulls() && !mapping.primitive)) {
                // gcode issue #377, call setter on nulls (value is not 'found')
                metaObject.setValue(mapping.property, value);
            }
        }
    }
    return foundValues;
}

  mapping.typeHandler.getResult会获取查询结果值的实际类型,比如我们user表中id字段为int类型,那么它就针对应Java中的Integer类型,然后经调用statement.getInt(“id”)来抱其int值,其种类为Integer。metaObject.setValue方法会把收获到之Integer值设置到Java类吃的应和字段。

// MetaObject类
public void setValue(String name, Object value) {
    PropertyTokenizer prop = new PropertyTokenizer(name);
    if (prop.hasNext()) {
        MetaObject metaValue = metaObjectForProperty(prop.getIndexedName());
        if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
            if (value == null && prop.getChildren() != null) {
                // don't instantiate child path if value is null
                return;
            } else {
                metaValue = objectWrapper.instantiatePropertyValue(name, prop, objectFactory);
            }
        }
        metaValue.setValue(prop.getChildren(), value);
    } else {
        objectWrapper.set(prop, value);
    }
}

  metaValue.setValue方法最后会调用到Java类吃针对许数据域的set方法,这样呢就算得了SQL查询结果集的Java类包装过程。最后贴平张调用栈到达Java类的set方法中之快照:

MyBatis 9

 

参考:

  1、MyBatis源码

  2、《深入剖析Java Web技术内幕》的iBatis章节

  3、《深入明mybatis原理》
MyBatis的架构设计以及实例分析

  4、luoxn28/tuiku

  5、Java
JDBC基础学习小结

网站地图xml地图