之前我一直有一個問題不懂.而且以爲很神奇.就是Mybatis咱們開發的時候只須要定義接口,並無寫實現類,爲何咱們運行的時候就能夠直接使用? 如今我想分享下這部分大體是怎麼實現的.java
根據以前的分享,在初始化階段Build SqlSessionFactory的時候須要用到XMLConfigBuilder去parse XML文件生成Configuration對象,在 parse的步驟中其中有一步就是parse mappers節點sql
在parse mapper過程當中會用到XMLMapperBuilder去parse.一步一步進入斷點.express
會發現後面會調用configuration的addMapper方法.它會調用MapperRegistry的addMapper方法apache
MapperRegistry至關用knownMappers這個hashmap於爲每一個Mapper註冊一次,其中key是你自定義的Mapper接口的class,Value是MapperProxyFactory類的對象.session
Factory一個就是一個工廠類,它確定須要生產對應的對象,從名字上也能發現它生產的就是MapperProxyapp
1 /** 2 * Copyright 2009-2015 the original author or authors. 3 * <p> 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * <p> 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * <p> 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.apache.ibatis.binding; 17 18 import org.apache.ibatis.session.SqlSession; 19 20 import java.lang.reflect.Method; 21 import java.lang.reflect.Proxy; 22 import java.util.Map; 23 import java.util.concurrent.ConcurrentHashMap; 24 25 /** 26 * @author Lasse Voss 27 */ 28 public class MapperProxyFactory<T> { 29 30 private final Class<T> mapperInterface; 31 private final Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<Method, MapperMethod>(); 32 33 public MapperProxyFactory(Class<T> mapperInterface) { 34 this.mapperInterface = mapperInterface; 35 } 36 37 public Class<T> getMapperInterface() { 38 return mapperInterface; 39 } 40 41 public Map<Method, MapperMethod> getMethodCache() { 42 return methodCache; 43 } 44 45 @SuppressWarnings("unchecked") 46 protected T newInstance(MapperProxy<T> mapperProxy) { 47 return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[]{mapperInterface}, mapperProxy); 48 } 49 50 public T newInstance(SqlSession sqlSession) { 51 final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache); 52 return newInstance(mapperProxy); 53 } 54 55 }
1 /** 2 * Copyright 2009-2015 the original author or authors. 3 * <p> 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * <p> 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * <p> 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.apache.ibatis.binding; 17 18 import org.apache.ibatis.reflection.ExceptionUtil; 19 import org.apache.ibatis.session.SqlSession; 20 21 import java.io.Serializable; 22 import java.lang.reflect.InvocationHandler; 23 import java.lang.reflect.Method; 24 import java.util.Map; 25 26 /** 27 * @author Clinton Begin 28 * @author Eduardo Macarron 29 */ 30 public class MapperProxy<T> implements InvocationHandler, Serializable { 31 32 private static final long serialVersionUID = -6424540398559729838L; 33 private final SqlSession sqlSession; 34 private final Class<T> mapperInterface; 35 private final Map<Method, MapperMethod> methodCache; 36 37 public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) { 38 this.sqlSession = sqlSession; 39 this.mapperInterface = mapperInterface; 40 this.methodCache = methodCache; 41 } 42 43 @Override 44 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 45 if (Object.class.equals(method.getDeclaringClass())) { 46 try { 47 return method.invoke(this, args); 48 } catch (Throwable t) { 49 throw ExceptionUtil.unwrapThrowable(t); 50 } 51 } 52 final MapperMethod mapperMethod = cachedMapperMethod(method); 53 return mapperMethod.execute(sqlSession, args); 54 } 55 56 private MapperMethod cachedMapperMethod(Method method) { 57 MapperMethod mapperMethod = methodCache.get(method); 58 if (mapperMethod == null) { 59 mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()); 60 methodCache.put(method, mapperMethod); 61 } 62 return mapperMethod; 63 } 64 65 }
從代碼中咱們能夠得知,這裏是用了JDK動態代理,MapperProxy類implements了InvocationHandler,less
若是是調用自定義Mapper的Object類中的方法,好比toString,那就直接調用,不然的話調用mapperMethod.execute去執行對應的方法(好比selectById).那麼mapperMethod是什麼呢?ide
這個類的對象其實就是對應你寫的Mapper裏的方法,你的每一個方法對應1個MapperMethod,至關因而Java的Method的包裝.ui
另外還包含了你在XML裏定義的SQL字符串, 是select仍是insert仍是update,delete操做等信息.至關於融合了你定義的Mapper裏的Method和你爲每一個Method在XML裏的寫的信息.this
這樣你調用mapperMethod.execute的時候就能找到對應的SQL去執行了.
初始化完成後就如同以前的介紹,會在confuguration的mapperRegistry裏註冊好了各類MapperFactory.那麼經過SqlSession去獲取Mapper的時候也是相似的.會調用configuration去獲取Mapper,內部會調用mapperRegistry去獲取Mapper
而後經過MapperProxyFactory去建立一個MapperProxy並返還
1.說白了,其實就是利用JDK動態代理,返回給你1個實現了你寫的Mapper接口的對象,而其中的Invocation接口的實現類就是MapperProxy.
2.在初始化階段會爲你寫的每一個Mapper在Configuration的MapperRegistry裏註冊一個MapperFactory,當你要獲取Mapper實例的時候就經過這個Factory來new.
3.當你調用Mapper.XXX方法的時候,好比select,就會調用MapperProxy的invoke方法,獲取你定義在Mapper裏的xxx方法對應的MapperMethod對象,這個對象就是Method的封裝,同時在XML裏找到對應的select語句再執行.
4.你寫的每一個Mapper類的對象對應1個MapperProxyFactory生成1個MapperProxy,你在Mapper中定義的每一個方法對應1個MapperMethod,它是Java的Method的封裝