国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

詳解Mybatis是如何解析配置文件的

瀏覽:65日期:2023-10-20 16:01:16
緣起

經過前面三章的入門,我們大概了解了Mybatis的主線邏輯是什么樣子的,在本章中,我們將正式進入Mybatis的源碼海洋。

Mybatis是如何解析xml的構建Configuration

詳解Mybatis是如何解析配置文件的

我們調用new SqlSessionFactoryBuilder().build()方法的最終目的就是構建 Configuration對象,那么Configuration何許人也?Configuration對象是一個配置管家, Configuration對象之中維護著所有的配置信息。Configuration的代碼片段如下

public class Configuration { //環境 protected Environment environment; protected boolean safeRowBoundsEnabled; protected boolean safeResultHandlerEnabled = true; protected boolean mapUnderscoreToCamelCase; protected boolean aggressiveLazyLoading; protected boolean multipleResultSetsEnabled = true; protected boolean useGeneratedKeys; protected boolean useColumnLabel = true; protected boolean cacheEnabled = true; protected boolean callSettersOnNulls; protected boolean useActualParamName = true; protected boolean returnInstanceForEmptyRow; //日志信息的前綴 protected String logPrefix; //日志接口 protected Class<? extends Log> logImpl; //文件系統接口 protected Class<? extends VFS> vfsImpl; //本地Session范圍 protected LocalCacheScope localCacheScope = LocalCacheScope.SESSION; //數據庫類型 protected JdbcType jdbcTypeForNull = JdbcType.OTHER; //延遲加載的方法 protected Set<String> lazyLoadTriggerMethods = new HashSet<String>( Arrays.asList(new String[] { 'equals', 'clone', 'hashCode', 'toString' })); //默認執行語句超時 protected Integer defaultStatementTimeout; //默認的執行器 protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE; //數據庫ID protected String databaseId; //mapper注冊表 protected final MapperRegistry mapperRegistry = new MapperRegistry(this); //攔截器鏈 protected final InterceptorChain interceptorChain = new InterceptorChain(); //類型處理器 protected final TypeHandlerRegistry typeHandlerRegistry = new TypeHandlerRegistry(); //類型別名 protected final TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry(); //語言驅動 protected final LanguageDriverRegistry languageRegistry = new LanguageDriverRegistry(); //mapper_id 和 mapper文件的映射 protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>( 'Mapped Statements collection'); //mapper_id和緩存的映射 protected final Map<String, Cache> caches = new StrictMap<Cache>('Caches collection'); //mapper_id和返回值的映射 protected final Map<String, ResultMap> resultMaps = new StrictMap<ResultMap>('Result Maps collection'); //mapper_id和參數的映射 protected final Map<String, ParameterMap> parameterMaps = new StrictMap<ParameterMap>('Parameter Maps collection'); //資源列表 protected final Set<String> loadedResources = new HashSet<String>(); 未完.......}構建MappedStatement

在Configuration中,有個mappedStatements的屬性,這是個MappedStatement對象Map的集合,其key是這個mapper的namespace+對應節點的id,而value是一個MappedStatement對象。在構建Configuration的時候,會去解析我們的配置文件。解析配置文件的關鍵代碼如下

private void parseConfiguration(XNode root) { try { //issue #117 read properties first propertiesElement(root.evalNode('properties')); Properties settings = settingsAsProperties(root.evalNode('settings')); loadCustomVfs(settings); loadCustomLogImpl(settings); typeAliasesElement(root.evalNode('typeAliases')); pluginElement(root.evalNode('plugins')); objectFactoryElement(root.evalNode('objectFactory')); objectWrapperFactoryElement(root.evalNode('objectWrapperFactory')); reflectorFactoryElement(root.evalNode('reflectorFactory')); settingsElement(settings); // read it after objectFactory and objectWrapperFactory issue #631 environmentsElement(root.evalNode('environments')); databaseIdProviderElement(root.evalNode('databaseIdProvider')); typeHandlerElement(root.evalNode('typeHandlers')); mapperElement(root.evalNode('mappers')); } catch (Exception e) { throw new BuilderException('Error parsing SQL Mapper Configuration. Cause: ' + e, e); }}

上訴代碼段倒數第三行mapperElement(root.evalNode('mappers'));就是解析mappers處就是把我們的mapper文件封裝成MappedStatement對象,然后保存到Configuration的mappedStatements屬性中,其中key是這個mapper的namespace+對應節點的id,而value是一個MappedStatement對象。保存的地方關鍵代碼如下

configuration.addMappedStatement(statement);

addMappedStatement()方法代碼如下

protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>( 'Mapped Statements collection');public void addMappedStatement(MappedStatement ms) {mappedStatements.put(ms.getId(), ms);}

那么這個MappedStatement的又是何許人也?我們可以簡單的把MapperStatement理解為對sql的一個封裝,在MappedStatement中保存著一個SqlSource對象,其中就存有SQL的信息。相關代碼如下

public final class MappedStatement {private SqlSource sqlSource;}

SqlSource 代碼如下

public interface SqlSource { BoundSql getBoundSql(Object parameterObject);}

BoundSql代碼如下

public class BoundSql { private final String sql; private final List<ParameterMapping> parameterMappings;}

關于二級緩存我們在Configuration中看到了一個caches屬性

protected final Map<String, Cache> caches = new StrictMap<>('Caches collection');

這個東西的作用是什么呢?其實是關于Mybatis的二級緩存的。在解析配置文件的過程中,如果用到了二級緩存,便會把這個ID和對象也保存到configuration的caches中,相關代碼如下

public void addCache(Cache cache) { caches.put(cache.getId(), cache);}構建SqlSessionFactory

在Configuration對象構建完畢之后,就該依賴Configuration對象去構建SqlSessionFactory對象了,相關代碼如下

public SqlSessionFactory build(Configuration config) { return new DefaultSqlSessionFactory(config);}

我們暫且把SqlSessionFactory稱為SqlSession工廠吧,SqlSessionFactory中有兩個方法,openSession()和getConfiguration()SqlSessionFactory代碼如下

public interface SqlSessionFactory { SqlSession openSession(); //其余openSession重載方法略… Configuration getConfiguration();}構建SqlSession

openSession()方法會返回一個SqlSession對象,SqlSession又是何許人也?SqlSession可以理解為程序與數據庫打交道的一個工具,通過它,程序可以往數據庫發送SQL執行。SqlSession代碼如下

public interface SqlSession extends Closeable { <T> T selectOne(String statement); <T> T selectOne(String statement, Object parameter); <E> List<E> selectList(String statement); <E> List<E> selectList(String statement, Object parameter); //其余增刪查改方法略…}總結

想必你已經明白了,Mybatis解析xml最主要的目的其實是構建Configuration對象,這個對象中可以說包含著Mybatis的所有配置信息。其中有一個mappedStatements屬性,這是一個Map,其中key是這個mapper的namespace+對應節點的id,而value是一個MappedStatement對象,而MappedStatement中保存著一個SqlSource對象,這個對象中保存著我們要執行的SQL語句。

那么在下一章,我們將一起探究Mybatis是如何執行我們的SQL語句的。

到此這篇關于詳解Mybatis是如何解析配置文件的的文章就介紹到這了,更多相關Mybatis 解析配置文件內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 2022国产精品自拍 | 国产成人亚洲综合无 | 波多野结衣在线视频观看 | 神马午夜-午夜片 | a级成人高清毛片 | 在线观看一级 | 亚洲伊人久久综合影院2021 | 91国内在线 | 成人国产亚洲 | 国产精品成人在线播放 | 免费一级特黄特色黄大任片 | 国产成人精品亚洲一区 | 黄网站色视频免费观看w | 欧美aaaaaaaaa| 国产成人精品三级 | 亚洲国产精品自在现线让你爽 | 国产综合亚洲专区在线 | 韩国本免费一级毛片免费 | 看国产一级毛片 | 日本三级在线观看中文字 | 岛国午夜精品视频在线观看 | 成人国产一区二区 | 性夜影院爽黄a爽免费看网站 | 最新步兵社区在线观看 | 男女男在线精品网站免费观看 | 国产成人高清亚洲一区久久 | 国产免费a级片 | 国产一级做a爰片久久毛片 国产一级做a爰片久久毛片99 | 好叼操这里只有精品 | 亚州一级毛片在线 | 久草在线在线 | 国产人成免费视频 | 毛片免费高清免费 | 看性过程三级视频在线观看 | 欧美一级在线观看视频 | 深夜福利国产福利视频 | 久久综合九九 | 成人在线播放 | 亚洲欧美精品成人久久91 | 狠狠久久综合 | 国产a级特黄的片子视频 |