MyBatis source code learning (1) SqlSession creation, mybatissqlsession

Source: Internet
Author: User

MyBatis source code learning (1) SqlSession creation, mybatissqlsession

MyBatis encapsulates the code for JDBC operations on the database and uses SqlSession to execute SQL statements. First, let's take a look at how MyBatis creates a SqlSession.
When MyBatis is not managed to spring, the Configuration information of the database is configured in the Configuration. xml file. The test code is as follows:

1 Reader reader =  Resources.getResourceAsReader("Configuration.xml");

Mybatis uses the SqlSessionFactoryBuilder. build (Reader reader) method to create 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 overload methods.

1 public SqlSessionFactory build (Reader reader, String environment, Properties properties) {2 try {3 // delegate XMLConfigBuilder to parse xml files and build 4 XMLConfigBuilder parser = new XMLConfigBuilder (reader, environment, properties); 5 return build (parser. parse (); 6} catch (Exception e) {7 throw ExceptionFactory. wrapException ("Error building SqlSession. ", e); 8} finally {9 ErrorContext. instance (). reset (); 10 try {11 reader. close (); 12} catch (IOException e) {13} 14 public SqlSessionFactory build (Configuration config) {15 return new DefaultSqlSessionFactory (config); 16}

Finally, a defasqlsqlsessionfactory object is returned, and a SqlSession object is returned through the openSession () of defasqlsqlsessionfactory.

Public SqlSession openSession () {return openSessionFromDataSource (configuration. getDefaultExecutorType (), null, false);} private SqlSession openSessionFromDataSource (ExecutorType execType, TransactionIsolationLevel, boolean autoCommit) {Transaction tx = null; try {final Environment environment = configuration. getEnvironment (); final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment (environment); // generate a transaction tx = transactionFactory through the transaction factory. newTransaction (environment. getDataSource (), level, autoCommit); // generates an Executor (the transaction is included in the executor) final Executor executor = configuration. newExecutor (tx, execType); // then a DefaultSqlSession return new defasqlsqlsession (configuration, executor, autoCommit);} catch (Exception e) {// if an error occurs when a transaction is opened, close closeTransaction (tx); // may have fetched a connection so lets call close () throw ExceptionFactory. wrapException ("Error opening session. cause: "+ e, e) ;}finally {// finally clear the error context ErrorContext. instance (). reset ();}}

You can see that the last returned defasqlsqlsession is the SqlSession object, selectOne (…) In defasqlsqlsession (...) SelectList (...)
SelectMap (...) Update (...) And so on.
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();    }  }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.