MyBatis Simple implementation principle of interface

Used MyBatis3 Why do you think MyBatis Of Mapper Interface has no implementation class , But it can be used directly ?

That's because MyBatis use Java Interface implemented by dynamic agent .

Here is just a simple example to illustrate the principle , Not exactly MyBatis Of , This kind of thinking can also be applied in other places .

Define an interface
public interface MethodInterface { String helloWorld(); }
Implement dynamic agent interface
public class MethodProxy<T> implements InvocationHandler { private Class<T>
methodInterface;public MethodProxy(Class<T> methodInterface) { this
.methodInterface = methodInterface; }@Override public Object invoke(Object
proxy, Method method, Object[] args)throws Throwable { System.out.println(
"========================="); System.out.println(" Method name :" + method.getName());
// Different operations for different methods return null; } }
This is for invoke The method is simple MyBatis Implementation principle of , In this method , We passed Method Ability to get interface and method names , The full name of the interface is equivalent to MyBatis XML In
namespace, The method name is equivalent to the id. That is to say, after dynamic proxy , Through SqlSession To pass
Method to call the corresponding method . Easier to use interface , But it's an indirect way .

Dynamic agent factory class
public class MethodProxyFactory { public static <T> T newInstance(Class<T>
methodInterface) {final MethodProxy<T> methodProxy = new
MethodProxy<T>(methodInterface);return (T) Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(),new Class[]{methodInterface},
methodProxy); } }
Through this factory class, you can generate a dynamic proxy class of any interface .

MethodInterface method =
MethodProxyFactory.newInstance(MethodInterface.class); method.helloWorld();
You can see MethodInterface Can execute without implementation class .


When it comes to dynamic agents, we usually deal with transactions , Application of log or record method in execution efficiency . It is a special treatment of the pre or post implementation of class methods .

Through this article , In fact, we can see another application direction , That is, there is no need to implement classes , Implement interface methods directly through dynamic agents ,MyBatis In this way, it is convenient for us to call methods , With this idea, we may be able to make some better designs in other aspects .

©2019-2020 Toolsou All rights reserved,
Bitcoin in ten years ,VDS Opportunity or fraud Don't annoy the panda with any cat !「 Kung Fu Panda 」20 It's the year of man 4 blood sort ( one ) bubble sort Random forest R Language implementation java Realize the function of grabbing red packets Python Basic knowledge and notes python To solve the problem of dictionary writing list in CSS architecture design Vue Common features ( one )2021 year 2 Monthly programmer salary statistics , average 15144 element