MyBatis源码部分简单地解析

.

一、解析xml:
> org.apache.ibatis.session.SqlSessionFactoryBuilder.build(java.io.InputStream, java.lang.String, java.util.Properties)
    > org.apache.ibatis.builder.xml.XMLConfigBuilder.parse
        > org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration
            > org.apache.ibatis.builder.xml.XMLConfigBuilder.environmentsElement "environments"
            > org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement "mappers" "package/resource/url/class"
                > org.apache.ibatis.builder.xml.XMLMapperBuilder.parse
                    > org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement
                        > org.apache.ibatis.builder.xml.XMLMapperBuilder.parameterMapElement "/mapper/parameterMap"
                        > org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements "/mapper/resultMap"
                        > org.apache.ibatis.builder.xml.XMLMapperBuilder.sqlElement(java.util.List<org.apache.ibatis.parsing.XNode>) "/mapper/sql"
                        > org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(java.util.List<org.apache.ibatis.parsing.XNode>) "select|insert|update|delete" 构建statement
                            > org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode 将mapper里写的sql转化为statement
                                ↓创建statement sql语句RawSqlSource对象,将#{}转为?,并将#{}里的参数名存在对象中
                                ↓关于${}:封装为DynamicSqlSource
                                > org.apache.ibatis.scripting.LanguageDriver.createSqlSource(org.apache.ibatis.session.Configuration, org.apache.ibatis.parsing.XNode, java.lang.Class<?>)
                                    > org.apache.ibatis.scripting.xmltags.XMLScriptBuilder.parseScriptNode

                                > org.apache.ibatis.builder.MapperBuilderAssistant.addMappedStatement(java.lang.String, org.apache.ibatis.mapping.SqlSource, org.apache.ibatis.mapping.StatementType, org.apache.ibatis.mapping.SqlCommandType, java.lang.Integer, java.lang.Integer, java.lang.String, java.lang.Class<?>, java.lang.String, java.lang.Class<?>, org.apache.ibatis.mapping.ResultSetType, boolean, boolean, boolean, org.apache.ibatis.executor.keygen.KeyGenerator, java.lang.String, java.lang.String, java.lang.String, org.apache.ibatis.scripting.LanguageDriver, java.lang.String)
                        ↑以上是解析xml文件,解析出statement,SqlSource

                    ↓以下是将mapper接口和命名空间绑定
                    > org.apache.ibatis.builder.xml.XMLMapperBuilder.bindMapperForNamespace
                        >

二、openSession操作:
> org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource
    > org.apache.ibatis.transaction.TransactionFactory.newTransaction(javax.sql.DataSource, org.apache.ibatis.session.TransactionIsolationLevel, boolean)
    > org.apache.ibatis.session.Configuration.newExecutor(org.apache.ibatis.transaction.Transaction, org.apache.ibatis.session.ExecutorType)
    > org.apache.ibatis.session.defaults.DefaultSqlSession new

三、getMapper:
> org.apache.ibatis.binding.MapperRegistry.getMapper
    > org.apache.ibatis.binding.MapperProxyFactory.newInstance(org.apache.ibatis.session.SqlSession)
        ↓jdk动态代理
        > org.apache.ibatis.binding.MapperProxy
        > org.apache.ibatis.binding.MapperProxyFactory.newInstance(org.apache.ibatis.binding.MapperProxy<T>)

四、查询:
> org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(java.lang.String, java.lang.Object)
    > org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(java.lang.String, java.lang.Object, org.apache.ibatis.session.RowBounds)
        > org.apache.ibatis.session.defaults.DefaultSqlSession.wrapCollection
        ↑封装参数
        ↓关于绑定参数名@Param:
        > org.apache.ibatis.session.Configuration.getMappedStatement(java.lang.String)
        > org.apache.ibatis.executor.BaseExecutor.query(org.apache.ibatis.mapping.MappedStatement, java.lang.Object, org.apache.ibatis.session.RowBounds, org.apache.ibatis.session.ResultHandler, org.apache.ibatis.cache.CacheKey, org.apache.ibatis.mapping.BoundSql)
            ↓通过SqlSource,解析出sql,解析成?,得到参数
            > org.apache.ibatis.mapping.MappedStatement.getBoundSql
            > org.apache.ibatis.executor.BaseExecutor.queryFromDatabase
                > org.apache.ibatis.executor.SimpleExecutor.doQuery
                    > org.apache.ibatis.executor.SimpleExecutor.prepareStatement 拼装sql的parameter查询条件 ****
                        > org.apache.ibatis.scripting.defaults.DefaultParameterHandler.setParameters 将得到的参数设置到对应的顺序,还包括JdbcType等 ****
                    > org.apache.ibatis.executor.statement.StatementHandler.query 执行查询
                        > java.sql.Statement.execute(java.lang.String) 提交查询
                        > org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets 处理结果集

 

.

posted on 2020-02-21 11:04  lyjlyjlyj  阅读(189)  评论(0编辑  收藏  举报

导航