本文主要是介绍Mybatis操作主体流程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
完整的时序图可以打开我的云笔记进行查看,对浏览器查看比例进行缩放为250%即可
1 Mybatis操作主体流程
定位配置文件的位置 构建sessionFactory 获取数据库操作会话 建立数据库连接并执行数据库操作
1.1 定位配置文件位置
使用org.apache.ibatis.io.Resources.getResourceAsReader(String)
定位mybatis配置文件的位置,获得文件的输入流。
Created with Raphaël 2.1.0 Reader Reader Resources Resources InputStreamReader InputStreamReader InputStream InputStream ClassLoaderWrapper ClassLoaderWrapper ClassLoader ClassLoader getResourceAsReader() getResourceAsReader() '' getResourceAsStream() getResourceAsStream() getResourceAsStream() '' getResourceAsReader new InputStreamReader()
1.2构建SqlSessionFactory
构建SqlSessionFactory的时序图,使用SqlSessionFactoryBuilder
创建SqlSessionFactory
对象
Created with Raphaël 2.1.0 SqlSessionFactoryBuilder SqlSessionFactoryBuilder XMLConfigBuilder XMLConfigBuilder XPathParser XPathParser DocumentBuilder DocumentBuilder DOMParser DOMParser Document Document Configuration Configuration SqlSessionFactory SqlSessionFactory build(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.0 SqlSessionFactory SqlSessionFactory DefaultSqlSessionFactory DefaultSqlSessionFactory SqlSession SqlSession openSession() openSessionFromDataSource() new DefaultSqlSession()
1.4 获取接口实例
Created with Raphaël 2.1.0 SqlSession SqlSession DefaultSqlSession DefaultSqlSession Configuration Configuration MapperRegistry MapperRegistry Proxy Proxy UserMapper UserMapper getMapper() getMapper() getMapper() newInstance() Proxy.newProxyInstance()
Created with Raphaël 2.1.0 UserMapper UserMapper MapperProxy MapperProxy MapperMethod MapperMethod DefaultSqlSession DefaultSqlSession CachingExecutor CachingExecutor SimpleExecutor SimpleExecutor BaseExecutor BaseExecutor PreparedStatementHandler PreparedStatementHandler PreparedStatement PreparedStatement DefaultResultSetHandler DefaultResultSetHandler getUserById() 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.0 SimpleExecutor SimpleExecutor BaseExecutor BaseExecutor JdbcTransaction JdbcTransaction DataSource DataSource UnpooledDataSource UnpooledDataSource DriverManager DriverManager DriverInfo DriverInfo getConnection() getConnection() getConnection() openConnection() getConnection() popConnection() new PooledConnection() doGetConnection() getConnection() connect() newInstance() ''
这篇关于Mybatis操作主体流程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!