Main方法,mybatis版本为3.5.0
返回DefaultSQlSession对象,包括执行程序和配置
inputstream inputstream=resources.getresourceasstream (' my batis-config.XML );
sqlsessionfactorysqlsessionfactory=newsqlsessionfactorybuilder ().build ) Inputstream );
session=sqlsessionfactory.open session (;
sqlSessionFactory.openSession (
org.Apache.ibatis.session.defaults.defaultsqlsessionfactory
专用sqlsessionopensessionfromdatasource (executortypeexectype,TransactionIsolationLevel level,布尔自动提交) {
传输tx=null; try { finalenvironmentenvironment=configuration.get environment (finaltransactionfactorytransactionfactory=gettransaction ) 通过办公工厂产生一个事务
tx=transaction factory.new transaction (environment.get data source ),level,autoCommit ); //生成一个执行器(事务包含在执行器中) )。
finalexecutorexecutor=configuration.new executor (tx,execType ); 生成默认sqlsession
returnnewdefaultsqlsession (配置、执行程序、自动提交);
}catch(exceptione )//如果事务打开失败,请关闭
关闭事务(tx; //mayhavefetchedaconnectionsoletscallclose (
throwexceptionfactory.wrap exception (erroropeningsession.cause : ) e,e );
}finally{//清空错误上下文
ErrorContext.instance ().reset );
}
}
配置. new executor (tx,execType ) )。
org.Apache.ibatis.session.configuration
//使执行机构发生
publicexecutornewexecutor (事务处理事务处理,执行程序类型执行程序类型) {
执行程序类型=执行程序类型==null? efaultexecutortype : executor type; 要不要将defaultExecutorType设为null?
执行程序类型=执行程序类型==null? executor type.simple : executor type;
执行程序执行程序; //在三个简单的分支中,生成三种类型的执行器batch executor/reuse executor/simple executor并执行CURD操作
if (executor type.batch==executor type ) {
执行程序=newbatchexecutor (this,transaction );
} else if (executor type.reuse==executor type ) {
执行程序=newreuseexecutor (this,transaction );
}else{
执行程序=newsimpleexecutor (this,transaction );
对于//l2缓存,默认情况下返回CachingExecutor,因为它处于另一个CachingExecutor (默认情况下有l1缓存)、装饰者模式
启用缓存(if ) {
执行程序=newcachingexecutor (执行程序);
//现在调用插件后,插件可以改变执行程序的行为
执行程序=(执行程序) interceptorchain.pluginall )执行程序; 返回执行程序;
}
newcachingexecutor (执行程序) )。
org.Apache.ibatis.executor.caching executor
/***辅助缓存执行器*
publicclasscachingexecutorimplementsexecutor { privatefinalexecutordelegate; privatefinaltransactionalcachemanagertcm=newtransactionalcachemanager (; publiccachingexecutor (executor delegate ) {this.delegate=delegate;
ele gate.setexecutorwrapper (this;
}
returnnewdefaultsqlsession (配置、执行程序、自动提交) )。
org.Apache.ibatis.session.defaults.defaultsqlsession
/**默认SqlSession实现*
publicclassdefaultsqlsessionimplementssqlsession {专用配置配置; 私有财务执行程序; //是否自动提交
私密金融布尔自动提交; 私密布尔日期; 私有列表; publicdefaultsqlsession (配置配置,执行器执行器,布尔自动提交) this.configuration=configuration this.dirty=false; this.autoCommit=autoCommit;
}
时间图