//Resources类/*
* Returns a resource on the classpath as a Stream object
*
* @param resource The resource to find
* @return The resource
* @throws java.io.IOException If the resource cannot be found or read
*/publicstaticInputStreamgetResourceAsStream(Stringresource)throwsIOException{returngetResourceAsStream(null,resource);}
getResourceAsStream()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
* Returns a resource on the classpath as a Stream object
*
* @param loader The classloader used to fetch the resource
* @param resource The resource to find
* @return The resource
* @throws java.io.IOException If the resource cannot be found or read
*/publicstaticInputStreamgetResourceAsStream(ClassLoaderloader,Stringresource)throwsIOException{InputStreamin=classLoaderWrapper.getResourceAsStream(resource,loader);if(in==null){thrownewIOException("Could not find resource "+resource);}returnin;}
//ClassLoaderWrapper /*
* Get a resource from the classpath, starting with a specific class loader
*
* @param resource - the resource to find
* @param classLoader - the first class loader to try
* @return the stream or null
*/publicInputStreamgetResourceAsStream(Stringresource,ClassLoaderclassLoader){returngetResourceAsStream(resource,getClassLoaders(classLoader));}/*
* Try to get a resource from a group of classloaders
*
* @param resource - the resource to get
* @param classLoader - the classloaders to examine
* @return the resource or null
*/InputStreamgetResourceAsStream(Stringresource,ClassLoader[]classLoader){for(ClassLoadercl:classLoader){if(null!=cl){// try to find the resource as passedInputStreamreturnValue=cl.getResourceAsStream(resource);// now, some class loaders want this leading "/", so we'll add it and try again if we didn't find the resourceif(null==returnValue){returnValue=cl.getResourceAsStream("/"+resource);}if(null!=returnValue){returnreturnValue;}}}returnnull;}
privateSqlSessionopenSessionFromDataSource(ExecutorTypeexecType,TransactionIsolationLevellevel,booleanautoCommit){Transactiontx=null;try{//根据Configuration的Environment属性来创建事务工厂finalEnvironmentenvironment=configuration.getEnvironment();finalTransactionFactorytransactionFactory=getTransactionFactoryFromEnvironment(environment);//通过事务工厂创建事务,默认level=null autoCommit=falsetx=transactionFactory.newTransaction(environment.getDataSource(),level,autoCommit);//创建执行器 真正执行sql语句的对象finalExecutorexecutor=configuration.newExecutor(tx,execType);//根据执行器返回对象 SqlSessreturnnewDefaultSqlSession(configuration,executor,autoCommit);}catch(Exceptione){closeTransaction(tx);// may have fetched a connection so lets call close()throwExceptionFactory.wrapException("Error opening session. Cause: "+e,e);}finally{ErrorContext.instance().reset();}}
@Overridepublic<T>TselectOne(Stringstatement,Objectparameter){// Popular vote was to return null on 0 results and throw exception on too many.List<T>list=this.<T>selectList(statement,parameter);if(list.size()==1){returnlist.get(0);}elseif(list.size()>1){thrownewTooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: "+list.size());}else{returnnull;}}
@Overridepublic<E>List<E>query(MappedStatementms,ObjectparameterObject,RowBoundsrowBounds,ResultHandlerresultHandler)throwsSQLException{BoundSqlboundSql=ms.getBoundSql(parameterObject);CacheKeykey=createCacheKey(ms,parameterObject,rowBounds,boundSql);returnquery(ms,parameterObject,rowBounds,resultHandler,key,boundSql);}@Overridepublic<E>List<E>query(MappedStatementms,ObjectparameterObject,RowBoundsrowBounds,ResultHandlerresultHandler,CacheKeykey,BoundSqlboundSql)throwsSQLException{Cachecache=ms.getCache();if(cache!=null){flushCacheIfRequired(ms);if(ms.isUseCache()&&resultHandler==null){ensureNoOutParams(ms,boundSql);@SuppressWarnings("unchecked")List<E>list=(List<E>)tcm.getObject(cache,key);if(list==null){list=delegate.<E>query(ms,parameterObject,rowBounds,resultHandler,key,boundSql);tcm.putObject(cache,key,list);// issue #578 and #116}returnlist;}}//BaseExecutor.query()returndelegate.<E>query(ms,parameterObject,rowBounds,resultHandler,key,boundSql);}
@SuppressWarnings("unchecked")@Overridepublic<E>List<E>query(MappedStatementms,Objectparameter,RowBoundsrowBounds,ResultHandlerresultHandler,CacheKeykey,BoundSqlboundSql)throwsSQLException{ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId());if(closed){thrownewExecutorException("Executor was closed.");}if(queryStack==0&&ms.isFlushCacheRequired()){clearLocalCache();}List<E>list;try{queryStack++;list=resultHandler==null?(List<E>)localCache.getObject(key):null;if(list!=null){handleLocallyCachedOutputParameters(ms,key,parameter,boundSql);}else{list=queryFromDatabase(ms,parameter,rowBounds,resultHandler,key,boundSql);}}finally{queryStack--;}if(queryStack==0){for(DeferredLoaddeferredLoad:deferredLoads){deferredLoad.load();}// issue #601deferredLoads.clear();if(configuration.getLocalCacheScope()==LocalCacheScope.STATEMENT){// issue #482clearLocalCache();}}returnlist;}
// MetaObject类publicvoidsetValue(Stringname,Objectvalue){PropertyTokenizerprop=newPropertyTokenizer(name);if(prop.hasNext()){MetaObjectmetaValue=metaObjectForProperty(prop.getIndexedName());if(metaValue==SystemMetaObject.NULL_META_OBJECT){if(value==null&&prop.getChildren()!=null){// don't instantiate child path if value is nullreturn;}else{metaValue=objectWrapper.instantiatePropertyValue(name,prop,objectFactory);}}metaValue.setValue(prop.getChildren(),value);}else{objectWrapper.set(prop,value);}}