MyBatis encapsulates the JDBC code to operate the database and executes sql statements through SqlSession. So first, let’s take a look at how MyBatis creates SqlSession.
When MyBatis is not hosted to spring, the database configuration information is configured in the Configuration.xml file. The test code is as follows
1 Reader reader = Resources.getResourceAsReader("Configuration.xml");
Mybatis passes SqlSessionFactoryBuilder The .build(Reader reader) method creates a SqlSessionFactory object. The parameter of the build method is the reader object just now, which contains all the information of the configuration file. The build method has many overloaded methods
public SqlSessionFactory build(Reader reader, String environment, Properties properties) { try { //委托XMLConfigBuilder来解析xml文件,并构建 XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties); return build(parser.parse()); } catch (Exception e) { throw ExceptionFactory.wrapException("Error building SqlSession.", e); } finally { ErrorContext.instance().reset(); try { reader.close(); } catch (IOException e) { } public SqlSessionFactory build(Configuration config) { return new DefaultSqlSessionFactory(config); }
Finally, a DefaultSqlSessionFactory object is returned, and a SqlSession object is returned through DefaultSqlSessionFactory's openSession()
public SqlSession openSession() { return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false); } private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { Transaction tx = null; try { final Environment environment = configuration.getEnvironment(); final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); //通过事务工厂来产生一个事务 tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); //生成一个执行器(事务包含在执行器里) final Executor executor = configuration.newExecutor(tx, execType); //然后产生一个DefaultSqlSession return new DefaultSqlSession(configuration, executor, autoCommit); } catch (Exception e) { //如果打开事务出错,则关闭它 closeTransaction(tx); // may have fetched a connection so lets call close() throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e); } finally { //最后清空错误上下文 ErrorContext.instance().reset(); } }
You can see that a DefaultSqlSession, which is SqlSession, is finally returned. Object, selectOne(…) selectList(…) in DefaultSqlSession
selectMap(…) update(…) and other methods are the methods that actually execute sql
The specific execution is performed by the executor object
public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) { try { MappedStatement ms = configuration.getMappedStatement(statement); executor.query(ms, wrapCollection(parameter), rowBounds, handler); } catch (Exception e) { throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }
The above is the detailed content of MyBatis source code learning SqlSession creation. For more information, please follow other related articles on the PHP Chinese website!