最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Mybatis框架的作用有哪些

今天就跟大家聊聊有關Mybatis框架的作用有哪些,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據(jù)這篇文章可以有所收獲。

我們提供的服務有:成都網(wǎng)站建設、網(wǎng)站設計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、昌江ssl等。為千余家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的昌江網(wǎng)站制作公司

mybatis基本架構

mybatis的源碼應該算是比較容易閱讀的,首先mybatis核心功能就是執(zhí)行Sql語句,但在其基礎上又有許多增強的地方(動態(tài)Sql,ORM等)??匆粋€框架的時候,第一步是對整個框架有一個大體的了解。例如mybatis,我們可以從初始化到完成一個sql請求為主線,看一下涉及了哪些類。我個人總結了一下,mybatis的框架主要的核心類有4個

Mybatis框架的作用有哪些

Configuration

Configuration就是用于解析、保存、處理Mybatis的配置內容,包括了

  • mybatis基本配置,例如支持數(shù)據(jù)庫中的字段支持下標轉駝峰mapUnderscoreToCamelCase=true等等,參看Mybatis配置說明

  • SqlMapper管理,也就是通過xml或者注解寫的一些sql映射。相關的類可以查看源碼中MappedStatement類。

  • 創(chuàng)建類,Configuration還有一些創(chuàng)建類的功能,例如Executor、StatementHandler。這個2個類后面還會說到

小節(jié)Configuration

總結Configuration的功能,當然,如何讀取和解析相關文件是Configuration中大部分代碼做的事。這些都是為了準備后面mybatis運行的基本條件。Configuration中創(chuàng)建類是因為創(chuàng)建的這些類都依賴于Configuration(但這樣做數(shù)據(jù)和邏輯沒有做到分離)。

SqlSession

SqlSession可能是mybatis中我們最常用的類,其實他是一個門面類,直接對外提供服務

public interface SqlSession extends Closeable {
  T selectOne(String statement);
  List selectList(String statement, Object parameter);
 int delete(String statement);
 void rollback();
 void commit();
 ...

}

這些方法都是直接提供給外部調用的??吹竭@些方法是不是很親切。(我個人在看源碼的時候看到一些自己用過的一些類或方法的時候都有種莫名的親近感。感覺終于和我的認知世界有交集了)

SqlSession的創(chuàng)建

SqlSessionFactor是用于創(chuàng)建SqlSession建造者,提供給外部快速創(chuàng)建一個SqlSession。是一個工廠類,而SqlSessionFactor的創(chuàng)建則是由SqlSessionFactorBuilder。

Mybatis框架的作用有哪些

Executor

前面說了SqlSession只是一個門面類,Executor才是負責Sql語句執(zhí)行的。因此Executor才是整個mybatis核心。Executor的實現(xiàn)類有

Mybatis框架的作用有哪些

  • BaseExecutor:看名字知道是最基礎Executor,其他的Executor都和這個類有一定的關系

  • CachingExecutor:每次查詢的時候會先從緩存中獲取,每次有增刪改的時候會讓緩存失效。CachingExecutor其實是一個代理內,內部代理了BaseExecutor(或其子類)。在BaseExecutor基礎上增加了緩存操作。

相關類

我們看一個Executor參數(shù)最多的一個方法

 List query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException;

這些類都對執(zhí)行Sql有一定關系

MappedStatement

具體點來理解就是我們定義的Sql映射語句,例如我們xml定義的:


 select count(1) FROM config
 WHERE path = #{path}

paramter

這個就是傳遞給sql映射的參數(shù),用于生成和填充動態(tài)sql語句

RowBound

限定一次查詢數(shù)據(jù)量,類很簡單,看代碼就明白,不多說

public class RowBounds {
 public static final int NO_ROW_OFFSET = 0;
 public static final int NO_ROW_LIMIT = Integer.MAX_VALUE;
 public static final RowBounds DEFAULT = new RowBounds();
 private int offset;
 private int limit;
 public RowBounds() {
 this.offset = NO_ROW_OFFSET;
 this.limit = NO_ROW_LIMIT;
 }
 public RowBounds(int offset, int limit) {
 this.offset = offset;
 this.limit = limit;
 }
}

ResultHandler

這個和本地緩存有關,用于保存一個查詢語句的緩存對象,下次有相同的查詢語句的時候就會先嘗試從本地緩存中獲取。 注意:

,mybatis有2級緩存,第一級是CachingExecutor,第二級緩存就是mybatis的本地緩存,也就是和ResultHandler

緩存失效策略是和一級緩存一樣,任何增刪改都會清空本地緩存

CacheKey

一個查詢語句的在本地緩存中的key,根據(jù)sql語句,參數(shù)等等組成

BoundSql

這個對象就是本次實際需要執(zhí)行的sql語句有關的信息,

public class BoundSql {
 private String sql;
 private List parameterMappings;
 private Object parameterObject;
 private Map additionalParameters;
 private MetaObject metaParameters;
 ...

如果說parameter參數(shù)是實際傳入的參數(shù),那么BoundSql就是根據(jù)傳入?yún)?shù)進行相關解析后的結果。他的創(chuàng)建在MappedStatement中,根據(jù)parameter和當前執(zhí)行MappedStatement生成

public BoundSql getBoundSql(Object parameterObject) {
 BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
 List parameterMappings = boundSql.getParameterMappings();
 if (parameterMappings == null || parameterMappings.isEmpty()) {
  boundSql = new BoundSql(configuration, boundSql.getSql(), parameterMap.getParameterMappings(), parameterObject);
 }
 // check for nested result maps in parameter mappings (issue #30)
 for (ParameterMapping pm : boundSql.getParameterMappings()) {
  String rmId = pm.getResultMapId();
  if (rmId != null) {
  ResultMap rm = configuration.getResultMap(rmId);
  if (rm != null) {
   hasNestedResultMaps |= rm.hasNestedResultMaps();
  }
  }
 }
 return boundSql;
}

Interceptor

Mybatis提供了Interceptor用于在執(zhí)行Executor之前進行一些操作,mybatis是怎么使用Interceptor。其實就是在創(chuàng)建Executor時候,會

public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
 executorType = executorType == null ? defaultExecutorType : executorType;
 executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
 Executor executor;
 if (ExecutorType.BATCH == executorType) {
  executor = new BatchExecutor(this, transaction);
 } else if (ExecutorType.REUSE == executorType) {
  executor = new ReuseExecutor(this, transaction);
 } else {
  executor = new SimpleExecutor(this, transaction);
 }
 if (cacheEnabled) {
  executor = new CachingExecutor(executor);
 }
 //看這里?。?!
 executor = (Executor) interceptorChain.pluginAll(executor);
 return executor;
 }

這里主要是通過jdk動態(tài)代理實現(xiàn)的

public class Plugin implements InvocationHandler {
 ...
 public static Object wrap(Object target, Interceptor interceptor) {
 Map, Set> signatureMap = getSignatureMap(interceptor);
 Class type = target.getClass();
 Class[] interfaces = getAllInterfaces(type, signatureMap);
 if (interfaces.length > 0) {
  return Proxy.newProxyInstance(
   type.getClassLoader(),
   interfaces,
   new Plugin(target, interceptor, signatureMap));
 }
 return target;
 }
 
 ...
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 try {
  Set methods = signatureMap.get(method.getDeclaringClass());
  if (methods != null && methods.contains(method)) {
  return interceptor.intercept(new Invocation(target, method, args));
  }
  return method.invoke(target, args);
 } catch (Exception e) {
  throw ExceptionUtil.unwrapThrowable(e);
 }
 }

這樣在調用Executor的時候就會先判斷是否滿足Interceptor的執(zhí)行條件,滿足則會先執(zhí)行Intercepter#intercept()方法

最底層的Handler

要說直接和Jdbc打交道的就是各種Handler類,例如

  • StatementHandler: 處理java.sql.Statement

  • ParameterHandler: 向PreparedStatement中設置參數(shù)

  • ResultSetHandler:處理sql執(zhí)行結果,并轉換成指定的類對象 上面的這些其實都不復雜,所以代碼還是比較好理解的

Transaction

每個Executor生成的時候都會把Transaction傳入,在BaseExecutor中Transaction是其成員變量,那Transaction的作用是什么呢?

public interface Transaction {
 Connection getConnection() throws SQLException;
 void commit() throws SQLException;
 void rollback() throws SQLException;
 void close() throws SQLException;
 Integer getTimeout() throws SQLException;
}

其實之前一直都沒提到過Connect誰來管理,這里可以看出來,Transaction負責了Connection的獲取,以及對這次Connect的提交和回滾等操作。這個類也是比較好理解的。Executor的commit或者rollback最后都是調用Transaction的

看完上述內容,你們對Mybatis框架的作用有哪些有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。


分享文章:Mybatis框架的作用有哪些
網(wǎng)頁URL:http://fisionsoft.com.cn/article/jjiosg.html