Mybatis操作主体流程

    xiaoxiao2026-02-11  23

    完整的时序图可以打开我的云笔记进行查看,对浏览器查看比例进行缩放为250%即可

    1 Mybatis操作主体流程

    定位配置文件的位置构建sessionFactory获取数据库操作会话建立数据库连接并执行数据库操作

    1.1 定位配置文件位置

    使用org.apache.ibatis.io.Resources.getResourceAsReader(String)定位mybatis配置文件的位置,获得文件的输入流。

    Created with Raphaël 2.1.0ReaderReaderResourcesResourcesInputStreamReaderInputStreamReaderInputStreamInputStreamClassLoaderWrapperClassLoaderWrapperClassLoaderClassLoadergetResourceAsReader()getResourceAsReader()''getResourceAsStream()getResourceAsStream()getResourceAsStream()''getResourceAsReadernew InputStreamReader()

    1.2构建SqlSessionFactory

    构建SqlSessionFactory的时序图,使用SqlSessionFactoryBuilder创建SqlSessionFactory对象

    Created with Raphaël 2.1.0SqlSessionFactoryBuilderSqlSessionFactoryBuilderXMLConfigBuilderXMLConfigBuilderXPathParserXPathParserDocumentBuilderDocumentBuilderDOMParserDOMParserDocumentDocumentConfigurationConfigurationSqlSessionFactorySqlSessionFactorybuild(reader)XMLConfigBuilder(XPathParser)createDocument()parse()getDocument()new XPathParser()new XMLConfigBuilder()parse()parseConfiguration()build(Configuration)

    根据时序图可以看得出,创建sessionFactory需要

    将读入的配置文件流解析为Document对象将Document对象解析为Configuration对象使用Configuration对象,通过build()方法完成对SqlSessionFactory对象的创建。

    1.3 获取数据库操作的会话

    Created with Raphaël 2.1.0SqlSessionFactorySqlSessionFactoryDefaultSqlSessionFactoryDefaultSqlSessionFactorySqlSessionSqlSessionopenSession()openSessionFromDataSource()new DefaultSqlSession()

    1.4 获取接口实例

    Created with Raphaël 2.1.0SqlSessionSqlSessionDefaultSqlSessionDefaultSqlSessionConfigurationConfigurationMapperRegistryMapperRegistryProxyProxyUserMapperUserMappergetMapper()getMapper()getMapper()newInstance()Proxy.newProxyInstance() Created with Raphaël 2.1.0UserMapperUserMapperMapperProxyMapperProxyMapperMethodMapperMethodDefaultSqlSessionDefaultSqlSessionCachingExecutorCachingExecutorSimpleExecutorSimpleExecutorBaseExecutorBaseExecutorPreparedStatementHandlerPreparedStatementHandlerPreparedStatementPreparedStatementDefaultResultSetHandlerDefaultResultSetHandlergetUserById()execute()selectOne()selectList()query()query()queryFromDatabase()doQuery()prepareStatement()getConnection()prepare()query()execute()handleResultSets()''

    在执行具体的 org.apache.ibatis.executor.SimpleExecutor.prepareStatement(StatementHandler, Log)处连接数据库,检查是否能连接成功

    数据库连接时序图

    Created with Raphaël 2.1.0SimpleExecutorSimpleExecutorBaseExecutorBaseExecutorJdbcTransactionJdbcTransactionDataSourceDataSourceUnpooledDataSourceUnpooledDataSourceDriverManagerDriverManagerDriverInfoDriverInfogetConnection()getConnection()getConnection()openConnection()getConnection()popConnection()new PooledConnection()doGetConnection()getConnection()connect()newInstance()''
    最新回复(0)