Mybatis3.3.x技術內幕(二):動態代理之投鞭斷流(自動映射器Mapper的底層實現原理)

一日小區漫步,我問朋友:Mybatis中聲明一個interface接口,沒有編寫任何實現類,Mybatis就能返回接口實例,並調用接口方法返回數據庫數據,你知道爲何不?朋友非常詫異:是啊,我也很納悶,咱們領導告訴咱們按照這個模式編寫就行了,我同事也感受很奇怪,雖然我不知道具體是怎麼實現的,但我以爲確定是……(此處略去若干的漫天猜測),可是也不對啊,難道是……(再次略去若干似懂非懂)。java

這激發了我寫本篇文章的衝動。sql


動態代理的功能:經過攔截器方法回調,對目標target方法進行加強。數據庫

言外之意就是爲了加強目標target方法。上面這句話沒錯,但也不要認爲它就是真理,卻不知,動態代理還有投鞭斷流的霸權,連目標target都不要的科幻模式。apache

注:本文默認認爲,讀者對動態代理的原理是理解的,若是不明白target的含義,難以看懂本篇文章,建議先理解動態代理。
網絡

1. 自定義JDK動態代理之投鞭斷流實現自動映射器Mapper

首先定義一個pojo。app

public class User {
	private Integer id;
	private String name;
	private int age;

	public User(Integer id, String name, int age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}
	// getter setter
}

再定義一個接口UserMapper.java。ide

public interface UserMapper {
	public User getUserById(Integer id);	
}

接下來咱們看看如何使用動態代理之投鞭斷流,實現實例化接口並調用接口方法返回數據的。
源碼分析

自定義一個InvocationHandler。學習

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MapperProxy implements InvocationHandler {

	@SuppressWarnings("unchecked")
	public <T> T newInstance(Class<T> clz) {
		return (T) Proxy.newProxyInstance(clz.getClassLoader(), new Class[] { clz }, this);
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		if (Object.class.equals(method.getDeclaringClass())) {
			try {
				// 諸如hashCode()、toString()、equals()等方法,將target指向當前對象this
				return method.invoke(this, args);
			} catch (Throwable t) {
			}
		}
		// 投鞭斷流
		return new User((Integer) args[0], "zhangsan", 18);
	}
}

上面代碼中的target,在執行Object.java內的方法時,target被指向了this,target已經變成了傀儡、象徵、佔位符。在投鞭斷流式的攔截時,已經沒有了target。
測試

寫一個測試代碼:

public static void main(String[] args) {
	MapperProxy proxy = new MapperProxy();

	UserMapper mapper = proxy.newInstance(UserMapper.class);
	User user = mapper.getUserById(1001);

	System.out.println("ID:" + user.getId());
	System.out.println("Name:" + user.getName());
	System.out.println("Age:" + user.getAge());

	System.out.println(mapper.toString());
}

output:

ID:1001
Name:zhangsan
Age:18
x.y.MapperProxy@6bc7c054

這即是Mybatis自動映射器Mapper的底層實現原理。

可能有讀者不由要問:你怎麼把代碼寫的像初學者寫的同樣?沒有結構,且缺少美感。

必須聲明,做爲一名經驗老道的高手,能把程序寫的像初學者寫的同樣,那一定是高手中的高手。這樣可讓初學者感受到親切,舒服,符合本身的Style,讓他們或她們,感受到大牛寫的代碼也不過如此,本身甚至寫的比這些大牛寫的還要好,今後自信滿滿,熱情高漲,認爲與大牛之間的差距,僅剩下三分鐘。

‍‍2. Mybatis自動映射器Mapper的源碼分析

首先編寫一個測試類:

    public static void main(String[] args) {
		SqlSession sqlSession = MybatisSqlSessionFactory.openSession();
		try {
			StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
			List<Student> students = studentMapper.findAllStudents();
			for (Student student : students) {
				System.out.println(student);
			}
		} finally {
			sqlSession.close();
		}
	}

Mapper長這個樣子:

public interface StudentMapper {
	List<Student> findAllStudents();
	Student findStudentById(Integer id);
	void insertStudent(Student student);
}

org.apache.ibatis.binding.MapperProxy.java部分源碼。

public class MapperProxy<T> implements InvocationHandler, Serializable {

  private static final long serialVersionUID = -6424540398559729838L;
  private final SqlSession sqlSession;
  private final Class<T> mapperInterface;
  private final Map<Method, MapperMethod> methodCache;

  public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
    this.sqlSession = sqlSession;
    this.mapperInterface = mapperInterface;
    this.methodCache = methodCache;
  }

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (Object.class.equals(method.getDeclaringClass())) {
      try {
        return method.invoke(this, args);
      } catch (Throwable t) {
        throw ExceptionUtil.unwrapThrowable(t);
      }
    }
    // 投鞭斷流
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    return mapperMethod.execute(sqlSession, args);
  }
  // ...

org.apache.ibatis.binding.MapperProxyFactory.java部分源碼。

public class MapperProxyFactory<T> {

  private final Class<T> mapperInterface;

  @SuppressWarnings("unchecked")
  protected T newInstance(MapperProxy<T> mapperProxy) {
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
  }

這即是Mybatis使用動態代理之投鞭斷流

3. 接口Mapper內的方法能重載(overLoad)嗎?(重要)

相似下面:

public User getUserById(Integer id);
public User getUserById(Integer id, String name);

Answer:不能。

緣由:在投鞭斷流時,Mybatis使用package+Mapper+method全限名做爲key,去xml內尋找惟一sql來執行的。相似:key=x.y.UserMapper.getUserById,那麼,重載方法時將致使矛盾。對於Mapper接口,Mybatis禁止方法重載(overLoad)。


注:學習時,是先研究的源碼,看懂了原理。寫博文時,則先闡釋原理,再閱讀的源碼。順序恰好相反,但願讀者不要所以疑惑,覺得我強大到未卜先知。


版權提示:文章出自開源中國社區,若對文章感興趣,可關注個人開源中國社區博客(http://my.oschina.net/zudajun)。(通過網絡爬蟲或轉載的文章,常常丟失流程圖、時序圖,格式錯亂等,仍是看原版的比較好)

相關文章
相關標籤/搜索