Mybatis source code parsing (1) (June 11, 2015), mybatis 2015
I. Introduction
First, let's take a look at the source code structure of Mybatis. There are 19 packages in Mybatis3.2.7, and there may be fewer packages in other versions.
Public SqlSessionFactory build (Reader reader, String environment, Properties properties ){
SqlSessionFactory var5;
Try {
XMLConfigBuilder e = new XMLConfigBuilder (reader, environment, properties );
Var5 = this. build (Configuration) e. parse ());
} Catch (Exception var14 ){
Throw ExceptionFactory. wrapException ("Error building SqlSession.", var14 );
} Finally {
ErrorContext. instance (). reset ();
Try {
Reader. close ();
} Catch (IOException var13 ){
;
}
}
Return var5;
}
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
SqlSessionFactory var5;
try {
XMLConfigBuilder e = new XMLConfigBuilder(inputStream, environment, properties);
var5 = this.build((Configuration)e.parse());
} catch (Exception var14) {
throw ExceptionFactory.wrapException("Error building SqlSession.", var14);
} finally {
ErrorContext.instance().reset();
try {
inputStream.close();
} catch (IOException var13) {
;
}
}
return var5;
}
public SqlSessionFactory build(Configuration config) {
return new DefaultSqlSessionFactory(config);
}
2. SqlSessionFactory
SqlSessionFactory is an interface. In fact, its implementation class defasqlsqlsessionfactory is actually working. As the name suggests, SqlSessionFactory is a factory class of SqlSession, and its function is to provide SqlSession externally, another function is to obtain the Configuration object.
public interface SqlSessionFactory {
SqlSession openSession();
SqlSession openSession(boolean var1);
SqlSession openSession(Connection var1);
SqlSession openSession(TransactionIsolationLevel var1);
SqlSession openSession(ExecutorType var1);
SqlSession openSession(ExecutorType var1, boolean var2);
SqlSession openSession(ExecutorType var1, TransactionIsolationLevel var2);
SqlSession openSession(ExecutorType var1, Connection var2);
Configuration getConfiguration();
}
Provides the method for obtaining SqlSession in Step 8. The parameters involved mainly include: Custom connection, transaction isolation level, ExecutorType (Statement type [Normal, preprocessing, batch processing ])
Note that the openSession method does not automatically submit a transaction by default.
3. SqlSession
SqlSession is also an interface. What actually works is its implementation class defasqlsqlsession, which is its class structure.
We can see that the role of SqlSession is mainly to provide some basic operations of the database.