mybatis源碼學習(一) 原生mybatis源碼學習


最近這一周,主要在學習mybatis相關的源碼,所以記錄一下吧,算是一點學習心得

個人覺得,mybatis的源碼,大致可以分為兩部分,一是原生的mybatis,二是和spring整合之后的mybatis源碼學習(也就是mybatis-spring這個jar包的相關源碼),這邊筆記,主要來學習原生mybatis;

還是先用描述一下,原生mybatis從解析xml到執行SQL的一個流程:

  1.第一步:首先會通過sqlSessionFactoryBuilder來build一個SQLSessionFactory,在build的過程中,會對mybatis的配置文件、mapper.xml文件進行解析,

     1.1將mapper.xml中對應的select/update/delete/insert節點,包裝成mappedStatement對象,然后把這個類對象,存方到了一個map中(mappedStatements),key值是namespace的value+select節點的id,value就是mappedStatement;

     1.2然后通過反射,根據當前namespace,獲取到一個class,將class存到了另外一個map中,knownMappers中,key值是通過反射生成的class對象,value是根據class,生成的MapperProxyFactory對象,這兩個map,在執行查詢的時候,有用到

 

 2.根據sqlSessionFactoryBuilder生成SqlSessionFactory,再根據sqlSesionFactory,創建sqlSession,這時候,就可以調用slqSession的selectOne();selectList()來執行查詢,或者通過sqlSession.getMapper()來獲取到接口的代理對象,在執行增刪改查sql

 

下面,我們根據上述的步驟來解析mybatis源碼,主要來說如何解析mybatis配置文件

 1 String resource = "mybatis-config.xml";
 2 InputStream inputStream = Resources.getResourceAsStream(resource);
 3 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
 4 SqlSession sqlSession = sqlSessionFactory.openSession();
 5 //        List<OperChannel> list = sqlSession.selectList("com.springsource.study.mybatis.OperChannelMapper.selectAll");
 6 //        OperChannel operChannel = sqlSession.selectOne("com.springsource.study.mybatis.OperChannelMapper.selectAll",1);
 7 //        System.out.println(operChannel.toString());
 8 
 9 System.out.println("+++++++++++++++++");
10 OperChannelMapper operChannelMapper = sqlSession.getMapper(OperChannelMapper.class);
11 System.out.println(operChannelMapper.selectAll(1));

這是我自己寫的一個測試類,首先我們從sqlSessionFactoryBuilder().buidl(inputStream)說起:

 1.首先會獲取到一個XMLConfigBuilder(),然后調用XMLConfigBuilder的parse()方法來解析mybatis的配置文件;需要知道的是,對mybatis配置文件的解析,最后會存放到一個configuration.class中,可以簡單理解為和配置文件對應的一個類,在后面,生成的environment、mappedStatements都是configuration的一個屬性

 

 1 public Configuration parse() {
 2   //在new XMLConfigBuilder的時候,默認置為了false,表示當前xml只能被解析一次
 3 if (parsed) {
 4   throw new BuilderException("Each XMLConfigBuilder can only be used once.");
 5 }
 6   parsed = true;
 7   //將配置文件解析成了configuration對象,在該方法中完成
 8   parseConfiguration(parser.evalNode("/configuration"));
 9   return configuration;
10 }
11 
12 /**
13 * @param root
14 * 原生mybatis在執行的時候,解析mybatis配置文件
15 *  這里解析的節點,都是配置文件中配置的xml信息
16 */
17 private void parseConfiguration(XNode root) {
18 try {
19   //issue #117 read properties first
20   propertiesElement(root.evalNode("properties"));
21   Properties settings = settingsAsProperties(root.evalNode("settings"));
22   loadCustomVfs(settings);
23   loadCustomLogImpl(settings);
24   typeAliasesElement(root.evalNode("typeAliases"));
25   pluginElement(root.evalNode("plugins"));
26   objectFactoryElement(root.evalNode("objectFactory"));
27   objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
28   reflectorFactoryElement(root.evalNode("reflectorFactory"));
29   settingsElement(settings);
30   // read it after objectFactory and objectWrapperFactory issue #631
31   //在這里解析environment信息,獲取到數據源
32   environmentsElement(root.evalNode("environments"));
33   databaseIdProviderElement(root.evalNode("databaseIdProvider"));
34   typeHandlerElement(root.evalNode("typeHandlers"));
35   //解析mapper配置信息,將SQL封裝成mapperstatement
36   mapperElement(root.evalNode("mappers"));
37 } catch (Exception e) {
38   throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
39   }
40 }

在parseConfiguration里面,主要是對配置文件中各個節點屬性來進行解析;我們着重來說mapper節點的解析,其中對environment的解析也提一下吧,在獲取數據源的時候,會先獲取到<DataSource>節點中type,根據type創建一個DataSourceFactory,然后把DataSource節點中配置的property屬性的value和name,存到properties中,然后從dataSourceFactory中獲取到一個數據源;

我們來說對mappers的解析:

 在mybatis配置文件中,對mapper.xml的配置方式有四種,分別是package、resource、URL、mapperClass,這四種優先級就是寫的前后順序,因為在源碼中,是按照這四個順序來進行解析的

 解析的mappers節點之后,會對節點中的每個節點進行遍歷,我們本次,只以resource為例:

  

 1 private void mapperElement(XNode parent) throws Exception {
 2 if (parent != null) {
 3   for (XNode child : parent.getChildren()) {
 4     //在配置文件中,配置mapper.xml文件有四種方式,這里按照優先級記進行解析
 5     if ("package".equals(child.getName())) {
 6       String mapperPackage = child.getStringAttribute("name");
 7       configuration.addMappers(mapperPackage);
 8     } else {
 9       String resource = child.getStringAttribute("resource");
10       String url = child.getStringAttribute("url");
11       String mapperClass = child.getStringAttribute("class");
12       if (resource != null && url == null && mapperClass == null) {
13         ErrorContext.instance().resource(resource);
14         InputStream inputStream = Resources.getResourceAsStream(resource);
15         //實例化一個mapper解析器
16         XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
17         //解析SQL語句
18         mapperParser.parse();
19       } else if (resource == null && url != null && mapperClass == null) {
20         ErrorContext.instance().resource(url);
21         InputStream inputStream = Resources.getUrlAsStream(url);
22         XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());
23         mapperParser.parse();
24       } else if (resource == null && url == null && mapperClass != null) {
25         Class<?> mapperInterface = Resources.classForName(mapperClass);
26         configuration.addMapper(mapperInterface);
27       } else {
28         //配置文件的配置只能是這四種,否則會報錯
29         throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");
30       }
31     }
32   }
33 }
34 }

 

 在mapperParse.parse()方法中,會對<mapper>節點進行解析,然后獲取到mapper節點對應xml文件中的所有屬性

 1 private void configurationElement(XNode context) {
 2     try {
 3       String namespace = context.getStringAttribute("namespace");
 4       if (namespace == null || namespace.equals("")) {
 5         throw new BuilderException("Mapper's namespace cannot be empty");
 6       }
 7       builderAssistant.setCurrentNamespace(namespace);
 8       cacheRefElement(context.evalNode("cache-ref"));
 9       cacheElement(context.evalNode("cache"));
10       parameterMapElement(context.evalNodes("/mapper/parameterMap"));
11       resultMapElements(context.evalNodes("/mapper/resultMap"));
12       sqlElement(context.evalNodes("/mapper/sql"));
13       //這里是解析增刪改查語句的
14       buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
15     } catch (Exception e) {
16       throw new BuilderException("Error parsing Mapper XML. The XML location is '" + resource + "'. Cause: " + e, e);
17     }
18   }

這個方法就是對mapper.xml文件中各個節點的解析,其中,比較重要的是對增刪改查節點的解析:

 會獲取到當前mapper中所有的增刪改查節點,然后再遍歷,遍歷的時候,會獲取到每個節點的所有參數信息,由於這個解析具體參數的篇幅比較長,就不粘貼了,org.apache.ibatis.builder.xml.XMLStatementBuilder#parseStatementNode 在這個方法中;

 其中需要提的是,以select為例,解析到select節點的所有參數和參數值之后,會把所以參數build成一個mappedStatement對象,然后存放到mappedStatements這個map中,key值就是namespace+id(這里的ID就是select/update...配置的id屬性,一般配置成方法名);

 

把所有的增刪改查存到mappedStatements之后,會進行另外一個操作,就是根據namespace,通過反射,創建一個Class,對象,然后把class對象存到另外一個map中,knownMappers

 1 private void bindMapperForNamespace() {
 2     String namespace = builderAssistant.getCurrentNamespace();
 3     if (namespace != null) {
 4       Class<?> boundType = null;
 5       try {
 6         boundType = Resources.classForName(namespace);
 7       } catch (ClassNotFoundException e) {
 8         //ignore, bound type is not required
 9       }
10       if (boundType != null) {
11         if (!configuration.hasMapper(boundType)) {
12           // Spring may not know the real resource name so we set a flag
13           // to prevent loading again this resource from the mapper interface
14           // look at MapperAnnotationBuilder#loadXmlResource
15           configuration.addLoadedResource("namespace:" + namespace);
16           configuration.addMapper(boundType);
17         }
18       }
19     }
20   }

put方法就在最后一行代碼,configuration.addMapper(boundType);

這里也比較簡單,就是new MapperProxyFactory()作為value,class作為key,然后存到map中,后面會用到


 

截止到這里,對配置文件,SQL的解析就完成了,下面我們來說如何執行SQL

  1.List<OperChannel> list = sqlSession.selectList("com.springsource.study.mybatis.OperChannelMapper.selectAll");

  我們先說這種方法來請求SQL,這里就是調用DefaultSqlSession 的selectList()方法,

   首先會根據如此那終端額全類名+方法名,從mappedStatements這個map中獲取到mappedStatement對象,這也就是為什么namespace一定要和接口的包名+類名一直的原因

   獲取到mappedStatement對象之后,就是先查詢緩存,緩存中沒有就從數據庫查詢,數據庫查到之后,存到一級緩存中,這里的大致的原理是這樣的,后面,會單獨寫一遍筆記。來記錄,如何具體執行SQL的

 

 2.OperChannelMapper operChannelMapper = sqlSession.getMapper(OperChannelMapper.class);

    System.out.println(operChannelMapper.selectAll(1));

    這是第二種方式,和第一種方式稍微有些不同

    這里的入參是mapperInterface.class,會根據入參,從上面提到的knownMappers中獲取到mapperProxyFactory對象,然后再調用mapperProxyFactory.newInstance()方法來生成代理對象

1 protected T newInstance(MapperProxy<T> mapperProxy) {
2         return Proxy.newProxyInstance(this.mapperInterface.getClassLoader(), new Class[]{this.mapperInterface}, mapperProxy);
3     }
4 
5     public T newInstance(SqlSession sqlSession) {
6         MapperProxy<T> mapperProxy = new MapperProxy(sqlSession, this.mapperInterface, this.methodCache);
7         return this.newInstance(mapperProxy);
8     }

在使用jdk代理生成代理對象時,需要傳三個入參,這里重要是mapperproxy,就是實現了invocationHandler接口的實現類,生成了接口的代理對象之后,

在執行selectAll()的時候,由於這時候,是代理對象,所以會執行mapperproxy的invoke方法,

 

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, args);
}

if (this.isDefaultMethod(method)) {
return this.invokeDefaultMethod(proxy, method, args);
}
} catch (Throwable var5) {
throw ExceptionUtil.unwrapThrowable(var5);
}

MapperMethod mapperMethod = this.cachedMapperMethod(method);
return mapperMethod.execute(this.sqlSession, args);
}

在這里,mapperMethod.execute()方法中,會判斷當前SQL是select?update?delete?等,然后調用DefaultSqlSession對應的方法;
也就是說,selSession.getMapper()這種方式其實是通過代理對象來執行SQL的;

截止到這里,基本上就是原生mybatis解析、執行的邏輯,后面有新的認識,會繼續更新


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM