MyBatis详细的执行流程
2021-04-11 01:18:31 0 举报
非常详细。 一步一步读懂源码。
作者其他创作
大纲/内容
实例化 SqlSessionFactorySqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);// 实例化时,使用了上面读取的 mybatis-config 中的,存放于configuration 中 的配置参数 public SqlSessionFactory build(Configuration config) { return new DefaultSqlSessionFactory(config);}public DefaultSqlSessionFactory(Configuration configuration) { this.configuration = configuration;}
获取执行器类型configuration.getDefaultExecutorType()protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;默认是SIMPLE// 知识扩展mybatis提供三种sql执行器,分别是SIMPLE、REUSE、BATCH。SIMPLE 是默认执行器,根据对应的sql直接执行,不会做一些额外的操作。REUSE 是可重用执行器,重用对象是Statement(即该执行器会缓存同一个sql的Statement,省去Statement的重新创建,优化性能)(即会重用预处理语句)BATCH 执行器会重用预处理语句,并执行批量更新。SIMPLE、REUSE,可通过insert、update、delete方法的返回值判断sql是否执行成功,返回非0表示执行sql成功的条数,返回0表示sql执行失败BATCH,insert、update、delete方法返回值一直会是负数-2147482646,在该模式下insert、update、delete返回值将无任何意义,不能作为判断sql执行成功的判断依据
实例化 SqlSessionFactoryBuilder 构造器new SqlSessionFactoryBuilder();
获取 TransactionFactorytransactionFactory 是上面读取到的,存放在 configuration.getEnvironment(); 中的信息。mybatis-config 配置的是 JdbcTransactionFactory<transactionManager type=\"JDBC\"/>final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);private TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) { if (environment == null || environment.getTransactionFactory() == null) { return new ManagedTransactionFactory(); } return environment.getTransactionFactory();}
从 configuration 中获取 Environmentenvironment 信息是 上面从mybatis-config读取到的,存放在 configuration 中的信息。final Environment environment = configuration.getEnvironment();class Environment { private final String id; private final TransactionFactory transactionFactory; private final DataSource dataSource;}
关闭 sqlSessionsqlSession.close()public void close() { try { executor.close(isCommitOrRollbackRequired(false)); closeCursors(); dirty = false; } finally { ErrorContext.instance().reset(); } }
通过 Resources 加载全局配置文件String resource = \"org/mybatis/example/mybatis-config.xml\
是
成功
提交事务sqlSession.commit()/** * Flushes batch statements and commits database connection. * @param force forces connection commit false */public void commit(boolean force) { try { executor.commit(isCommitOrRollbackRequired(force)); dirty = false; } catch (Exception e) { throw ExceptionFactory.wrapException(\"Error committing transaction. Cause: \
实现 CRUD
将xml配置文件中 <configuration></configuration> 标签中所有配置信息加载到到 Configuration 类中// 获取 mybatis-config.xml 文件中 <configuration></configuration>标签中的内容parseConfiguration(parser.evalNode(\"/configuration\"));// 将读出的各种类型的元素值,放入 configuration 中propertiesElement(root.evalNode(\"properties\"));Properties settings = settingsAsProperties(root.evalNode(\"settings\"));loadCustomVfs(settings);loadCustomLogImpl(settings);typeAliasesElement(root.evalNode(\"typeAliases\"));pluginElement(root.evalNode(\"plugins\"));objectFactoryElement(root.evalNode(\"objectFactory\"));objectWrapperFactoryElement(root.evalNode(\"objectWrapperFactory\"));reflectorFactoryElement(root.evalNode(\"reflectorFactory\"));settingsElement(settings);// read it after objectFactory and objectWrapperFactory issue #631environmentsElement(root.evalNode(\"environments\"));databaseIdProviderElement(root.evalNode(\"databaseIdProvider\"));typeHandlerElement(root.evalNode(\"typeHandlers\"));mapperElement(root.evalNode(\"mappers\"));
否
收藏
0 条评论
下一页