天天看點

spring源碼之模拟mybatis擷取Mapper

上一章說了,既然mybatis擷取Mapper的時候使用的是代理,那麼我們是否可以模拟實作一下。

話不多說上代碼;

這樣就更加清楚mapper的代理實作過程了吧。

public class MySqlSession {

	public static Object getMapper(Class clazz){
		ClassLoader classLoader = MySqlSession.class.getClassLoader();
		Class[] classes = {clazz};

		Object proxy = Proxy.newProxyInstance(classLoader, classes, new MyTestInvocationHandler());
		return proxy;
	}

	static class MyTestInvocationHandler implements InvocationHandler{

		@Override
		public Object invoke(Object o, Method method, Object[] args) throws Throwable {
			//這個是判斷要執行的方法是在Object聲明的方法,
			// 比如toString這種,就不需要我們通過代理實作
			if(method.getDeclaringClass().equals(Object.class)){
				method.invoke(this, args);
			}

			Select select = method.getAnnotation(Select.class);
			//取出@select注解中的sql語句。數組是因為注解中可以寫多條sql語句
			String sql = select.value()[0];
			System.out.println("假裝連接配接資料庫");
			System.out.println("假裝執行了查詢語句"+sql);
			System.out.println("假裝傳回了JSON串");
			return null;
		}
	}

}


	@Test
	public void customBatis(){
		TMapper tMapper = (TMapper)MySqlSession.getMapper(TMapper.class);
		tMapper.queryMap("1");
	}

//列印結果
假裝連接配接資料庫
假裝執行了查詢語句select * from t where id = ${id}
假裝傳回了JSON串
           

有些同學會發散思維了,說既然這個我們可以實作,那是不是我們可以自己開發一個mybatis架構來讓市面上的人使用呢。

可以,但是要解決一些問題,比如這個:

我們都知道在service中調用mapper的時候,是通過Autowired來進行注入的,但是你這個mapper是在使用的時候才通過代理對象生成,那麼spring在初始化的時候找不到這個mapper,注入不了必定是要報錯的。

問題就是:如何将一個第三方的對象注入到Spring呢?