最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SQL語句解析執(zhí)行的過程及原理

 更新時(shí)間:2022年03月29日 14:54:05   作者:zfcq  
這篇文章主要介紹了SQL語句解析執(zhí)行的過程及原理,文章主要通過獲得SqlSession對象后就能執(zhí)行各種CRUD方法展開全文內(nèi)容需要的小伙伴可以參考一下,希望對你有所幫助

一、sqlSession簡單介紹

  • 拿到SqlSessionFactory對象后,會調(diào)用SqlSessionFactoryopenSesison方法,這個(gè)方法會創(chuàng)建一個(gè)Sql執(zhí)行器(Executor),這個(gè)Sql執(zhí)行器會代理你配置的攔截器方法。
  • 獲得上面的Sql執(zhí)行器后,會創(chuàng)建一個(gè)SqlSession(默認(rèn)使用DefaultSqlSession),這個(gè)SqlSession中也包含了Configration對象,所以通過SqlSession也能拿到全局配置;
  • 獲得SqlSession對象后就能執(zhí)行各種CRUD方法了。

sqlSession獲取流程.png

二、獲得sqlSession對象源碼分析

/**
 * 通過sqlSessionFactory.openSession進(jìn)行獲取sqlSession對象
 * 源碼位置:org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession()
 */
public SqlSession openSession() {
	return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
}

/**
 * 通過數(shù)據(jù)源去獲取SqlSession 
 * 源碼位置:org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(ExecutorType, TransactionIsolationLevel, boolean)
 */
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level,
		boolean autoCommit) {
	Transaction tx = null;
	try {
		// 獲取環(huán)境變量
		final Environment environment = configuration.getEnvironment();
		// 獲取事務(wù)工廠
		final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
		// 獲取一個(gè)事務(wù)
		tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
		// 獲取執(zhí)行器,這邊獲得的執(zhí)行器已經(jīng)代理攔截器的功能
		final Executor executor = configuration.newExecutor(tx, execType);
		// 根據(jù)獲取的執(zhí)行器創(chuàng)建SqlSession
		return new DefaultSqlSession(configuration, executor, autoCommit);
	} catch (Exception e) {
		closeTransaction(tx); // may have fetched a connection so lets call close()
		throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
	} finally {
		ErrorContext.instance().reset();
	}
}

/**
 * 獲取執(zhí)行器
 * 源碼位置:org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(ExecutorType, TransactionIsolationLevel, boolean)
 */
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
	// 默認(rèn)使用SIMPLE的執(zhí)行器
	executorType = executorType == null ? defaultExecutorType : executorType;
	executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
	Executor executor;

	if (ExecutorType.BATCH == executorType) {
		// 批量的執(zhí)行器
		executor = new BatchExecutor(this, transaction);
	} else if (ExecutorType.REUSE == executorType) {
		// 可重復(fù)使用的執(zhí)行器
		executor = new ReuseExecutor(this, transaction);
	} else {
		// 簡單的sql執(zhí)行器
		executor = new SimpleExecutor(this, transaction);
	}

	// 判斷Mybatis的全局配置文件是否開啟二級緩存
	if (cacheEnabled) {
		// 開啟緩存,吧executor包裝為CachingExecutor
		executor = new CachingExecutor(executor);
	}
	
	// 插件的調(diào)用:責(zé)任鏈模式
	executor = (Executor) interceptorChain.pluginAll(executor);
	return executor;
}

三、SQL執(zhí)行流程,以查詢?yōu)槔?/h2>

sql執(zhí)行流程.png

/**
 * 查詢的入口方法
 * 源碼位置:org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(String, Object)
 */
public <T> T selectOne(String statement, Object parameter) {
	// Popular vote was to return null on 0 results and throw exception on too many.
	// 查詢數(shù)據(jù)
	List<T> list = this.<T>selectList(statement, parameter);

	// 長度為1,拿第一個(gè)
	if (list.size() == 1) {
		return list.get(0);
	} else if (list.size() > 1) { 
		// 長度大于一,拋異常
		throw new TooManyResultsException(
				"Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
	} else {
		// 沒有拿到返回null
		return null;
	}
}

/**
 * 查詢數(shù)據(jù)
 * 源碼位置:org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(String, Object, RowBounds)
 */
public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
	try {
		// 通過statement去全局配置文件中獲取MappedStatement(得到mapper中增刪改查的節(jié)點(diǎn))
		MappedStatement ms = configuration.getMappedStatement(statement);

		// 通過執(zhí)行器去執(zhí)行SQL
		return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
	} catch (Exception e) {
		throw ExceptionFactory.wrapException("Error querying database.  Cause: " + e, e);
	} finally {
		ErrorContext.instance().reset();
	}
}

/**
 * 執(zhí)行查詢操作的準(zhǔn)備工作
 * 源碼位置:org.apache.ibatis.executor.CachingExecutor.query(MappedStatement, Object, RowBounds, ResultHandler)
 */
public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds,
		ResultHandler resultHandler) throws SQLException {

	// 通過參數(shù)進(jìn)行sql解析
	BoundSql boundSql = ms.getBoundSql(parameterObject);
	CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);
	return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}

/**
 * 執(zhí)行查詢操作的準(zhǔn)備工作
 * 源碼位置:org.apache.ibatis.executor.CachingExecutor.query(MappedStatement, Object, RowBounds, ResultHandler, CacheKey, BoundSql)
 */
public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds,
		ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
	// 判斷sql是否開啟了緩存 <cache></cache>
	Cache cache = ms.getCache();
	
	// 有緩存
	if (cache != null) {
		// 判斷是否需要刷新緩存
		flushCacheIfRequired(ms);
		if (ms.isUseCache() && resultHandler == null) {
			ensureNoOutParams(ms, boundSql);
			@SuppressWarnings("unchecked")

			// 去二級緩存中獲?。ㄑb飾者模式)
			List<E> list = (List<E>) tcm.getObject(cache, key);

			// 二級緩存沒有找到
			if (list == null) {

				// 查詢數(shù)據(jù),并放入緩存
				list = delegate.<E>query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
				tcm.putObject(cache, key, list); // issue #578 and #116
			}
			return list;
		}
	}
	// 查詢數(shù)據(jù)
	return delegate.<E>query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}

/**
 * 一級緩存查詢的調(diào)用
 * 源碼位置:org.apache.ibatis.executor.BaseExecutor.query(MappedStatement, Object, RowBounds, ResultHandler, CacheKey, BoundSql)
 */
public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler,
		CacheKey key, BoundSql boundSql) throws SQLException {
	ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId());

	// 已經(jīng)關(guān)閉了,拋異常
	if (closed) {
		throw new ExecutorException("Executor was closed.");
	}

	// 清空本地緩存
	if (queryStack == 0 && ms.isFlushCacheRequired()) {
		clearLocalCache();
	}
	List<E> list;
	try {
		// 從一級緩存中獲取數(shù)據(jù)
		queryStack++;
		list = resultHandler == null ? (List<E>) localCache.getObject(key) : null;
		if (list != null) {
			// 緩存里面有,進(jìn)行處理
			handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);
		} else {
			// 緩存沒有,進(jìn)行查詢
			list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
		}
	} finally {
		queryStack--;
	}
	if (queryStack == 0) {
		for (DeferredLoad deferredLoad : deferredLoads) {
			deferredLoad.load();
		}
		// issue #601
		deferredLoads.clear();
		if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {
			// issue #482
			clearLocalCache();
		}
	}
	return list;
}

/**
 * 在數(shù)據(jù)庫中查詢
 * 源碼位置:org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(MappedStatement, Object, RowBounds, ResultHandler, CacheKey, BoundSql)
 */
private <E> List<E> queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds,
		ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
	List<E> list;
	localCache.putObject(key, EXECUTION_PLACEHOLDER);
	try {
		// 去數(shù)據(jù)庫查詢
		list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql);
	} finally {
		localCache.removeObject(key);
	}

	// 一級緩存進(jìn)行緩存
	localCache.putObject(key, list);
	if (ms.getStatementType() == StatementType.CALLABLE) {
		localOutputParameterCache.putObject(key, parameter);
	}
	return list;
}

/**
 * 查詢邏輯
 * 源碼位置:org.apache.ibatis.executor.SimpleExecutor.doQuery(MappedStatement, Object, RowBounds, ResultHandler, BoundSql)
 */
public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler,
		BoundSql boundSql) throws SQLException {
	Statement stmt = null;
	try {
		// 得到整體的配置對象
		Configuration configuration = ms.getConfiguration();

		// 內(nèi)部封裝了ParameterHandler和ResultSetHandler
		StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds,
				resultHandler, boundSql);
		stmt = prepareStatement(handler, ms.getStatementLog());

		// 執(zhí)行查詢
		return handler.<E>query(stmt, resultHandler);
	} finally {
		closeStatement(stmt);
	}
}

/**
 * 執(zhí)行查詢語句
 * 源碼位置:org.apache.ibatis.executor.statement.SimpleStatementHandler.query(Statement, ResultHandler)
 */
public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
	// 得到要執(zhí)行的sql
	String sql = boundSql.getSql();

	// 執(zhí)行sql
	statement.execute(sql);

	// 處理結(jié)果集
	return resultSetHandler.<E>handleResultSets(statement);
}

到此這篇關(guān)于SQL語句解析執(zhí)行的過程及原理的文章就介紹到這了,更多相關(guān)SQL語句解析原理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MySQL數(shù)據(jù)權(quán)限的實(shí)現(xiàn)詳情

    MySQL數(shù)據(jù)權(quán)限的實(shí)現(xiàn)詳情

    這篇文章主要介紹了MySQL數(shù)據(jù)權(quán)限的實(shí)現(xiàn)詳情,文章通過實(shí)際案例,從代碼實(shí)戰(zhàn)的角度來實(shí)現(xiàn)這樣的一個(gè)數(shù)據(jù)權(quán)限。具體詳細(xì)介紹,具有一定的參考價(jià)值
    2022-08-08
  • 服務(wù)器上的mysql實(shí)現(xiàn)讀取sql文件

    服務(wù)器上的mysql實(shí)現(xiàn)讀取sql文件

    這篇文章主要介紹了服務(wù)器上的mysql實(shí)現(xiàn)讀取sql文件方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • MySQL查詢表中重復(fù)數(shù)據(jù)的實(shí)現(xiàn)

    MySQL查詢表中重復(fù)數(shù)據(jù)的實(shí)現(xiàn)

    在數(shù)據(jù)庫中,我們經(jīng)常需要查詢重復(fù)字段來確保數(shù)據(jù)的準(zhǔn)確性,如果數(shù)據(jù)中有重復(fù)字段,則可能會導(dǎo)致查詢結(jié)果錯(cuò)誤,本文就想詳細(xì)的介紹了MySQL查詢表中重復(fù)數(shù)據(jù),感興趣的可以了解一下
    2023-08-08
  • MySQL主從復(fù)制原理解析與最佳實(shí)踐過程

    MySQL主從復(fù)制原理解析與最佳實(shí)踐過程

    這篇文章主要介紹了MySQL主從復(fù)制原理解析與最佳實(shí)踐過程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Windows?本地安裝?Mysql8.0圖文教程

    Windows?本地安裝?Mysql8.0圖文教程

    本文介紹了如何在Windows本地安裝Mysql8.0。從下載Mysql8.0安裝包,運(yùn)行安裝程序,配置初始設(shè)置到啟動Mysql服務(wù)等詳細(xì)步驟進(jìn)行了講解。
    2023-04-04
  • MySQL百萬級數(shù)據(jù)大分頁查詢優(yōu)化的實(shí)現(xiàn)

    MySQL百萬級數(shù)據(jù)大分頁查詢優(yōu)化的實(shí)現(xiàn)

    在數(shù)據(jù)庫開發(fā)過程中我們經(jīng)常會使用分頁,但是如果是百萬級數(shù)據(jù)呢,本文就詳細(xì)的介紹一下MySQL百萬級數(shù)據(jù)大分頁查詢優(yōu)化的實(shí)現(xiàn),感興趣的可以了解一下
    2022-01-01
  • MySQL中實(shí)現(xiàn)動態(tài)表單中JSON元素精準(zhǔn)匹配的方法示例

    MySQL中實(shí)現(xiàn)動態(tài)表單中JSON元素精準(zhǔn)匹配的方法示例

    本文主要介紹了MySQL中實(shí)現(xiàn)動態(tài)表單中JSON元素精準(zhǔn)匹配的方法示例,重點(diǎn)講解如何在將設(shè)計(jì)好的動態(tài)表單信息進(jìn)行提取,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-07-07
  • mysql通過my.cnf修改默認(rèn)字符集為utf-8的方法和注意事項(xiàng)

    mysql通過my.cnf修改默認(rèn)字符集為utf-8的方法和注意事項(xiàng)

    本文主要給大家介紹mysql通過my.cnf修改默認(rèn)字符集為utf-8的方法,當(dāng)然你也可以設(shè)置成別的,國際點(diǎn)還是utf-8好,以及在修改過程中要注意的一些事項(xiàng),有需要的朋友們可以參考借鑒。
    2016-09-09
  • MYSQL無法連接 提示10055錯(cuò)誤的解決方法

    MYSQL無法連接 提示10055錯(cuò)誤的解決方法

    這篇文章主要介紹了MYSQL無法連接 提示10055錯(cuò)誤的解決方法,需要的朋友可以參考下
    2016-12-12
  • MySQL 配置優(yōu)化(多個(gè)參數(shù))

    MySQL 配置優(yōu)化(多個(gè)參數(shù))

    安裝MySQL后,配置文件my.cnf在 /MySQL安裝目錄/share/mysql目錄中,該目錄中還包含多個(gè)配置文件可供參考,有my-large.cnf ,my-huge.cnf, my-medium.cnf,my-small.cnf,分別對應(yīng)大中小型數(shù)據(jù)庫應(yīng)用的配置。win環(huán)境下即存在于MySQL安裝目錄中的.ini文件
    2016-05-05

最新評論

台北市| 永顺县| 荣昌县| 都昌县| 繁峙县| 琼结县| 庐江县| 桦南县| 贡嘎县| 阜南县| 施秉县| 清水县| 德安县| 陕西省| 汾阳市| 高安市| 寻甸| 丰镇市| 霍城县| 丽水市| 泰宁县| 兴安县| 左权县| 雷山县| 中宁县| 乳山市| 塔城市| 富宁县| 辽宁省| 邳州市| 萍乡市| 平江县| 武威市| 尚志市| 平武县| 家居| 克东县| 金阳县| 广元市| 楚雄市| 平远县|