Home > Java > javaTutorial > [Transfer] Simple implementation principle of MyBatis interface

[Transfer] Simple implementation principle of MyBatis interface

PHP中文网
Release: 2017-07-09 18:12:39
Original
1353 people have browsed it

Simple implementation principle of MyBatis interface

People who have used MyBatis3 may wonder why the Mapper interface of MyBatis does not have an implementation class, but it can be used directly?

That's because MyBatis uses the interface implemented by Java dynamic proxy.

Here is just a simple example to illustrate the principle. It is not completely specific to MyBatis. We can also apply this idea to other places.

Define an interface

<span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> MethodInterface { String helloWorld(); }
Copy after login

Implement dynamic proxy interface

<span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> MethodProxy<T> <span style="color: #0000ff">implements</span><span style="color: #000000"> InvocationHandler {
    </span><span style="color: #0000ff">private</span> Class<T><span style="color: #000000"> methodInterface;

    </span><span style="color: #0000ff">public</span> MethodProxy(Class<T><span style="color: #000000"> methodInterface) {
        </span><span style="color: #0000ff">this</span>.methodInterface =<span style="color: #000000"> methodInterface;
    }

    @Override
    </span><span style="color: #0000ff">public</span> Object invoke(Object proxy, Method method, Object[] args) <span style="color: #0000ff">throws</span><span style="color: #000000"> Throwable {
        System.out.println(</span>"========================="<span style="color: #000000">);
        System.out.println(</span>"方法名:" +<span style="color: #000000"> method.getName());
        </span><span style="color: #008000">//</span><span style="color: #008000">针对不同的方法进行不同的操作</span>
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span><span style="color: #000000">;
    }
}</span>
Copy after login

Here we briefly talk about the implementation principle of MyBatis for the invoke method. In this method, we can obtain the interface and method name through Method. The full name of the interface is equivalent to the namespace in MyBatis XML. , the method name is equivalent to the id in a specific method. That is to say, after passing the dynamic proxy, you can call the corresponding method through SqlSession through namespace.id. Using interfaces is more convenient, but in an indirect way.

Dynamic proxy factory class

<span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> MethodProxyFactory {
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <T> T newInstance(Class<T><span style="color: #000000"> methodInterface) {
        </span><span style="color: #0000ff">final</span> MethodProxy<T> methodProxy = <span style="color: #0000ff">new</span> MethodProxy<T><span style="color: #000000">(methodInterface);
        </span><span style="color: #0000ff">return</span><span style="color: #000000"> (T) Proxy.newProxyInstance(
                Thread.currentThread().getContextClassLoader(), 
                </span><span style="color: #0000ff">new</span><span style="color: #000000"> Class[]{methodInterface}, 
                methodProxy);
    }
}</span>
Copy after login

This factory class can generate a dynamic proxy class for any interface.

test

MethodInterface method = MethodProxyFactory.newInstance(MethodInterface.<span style="color: #0000ff">class</span><span style="color: #000000">);
method.helloWorld();</span>
Copy after login

Summarize

Generally speaking, when it comes to dynamic agents, our usual usage is to deal with transactions, logs, or recording method execution efficiency. They are all special processing of pre- or post-implementation class methods.

Through this article, we can actually see another application direction, that is, there is no need to implement the class, and the interface method is executed directly through the dynamic proxy. MyBatis uses this method to facilitate us to call the method. Using this idea, we Maybe some better designs can be made in other aspects.

original:

The above is the detailed content of [Transfer] Simple implementation principle of MyBatis interface. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Mybatis mapping problem
From 1970-01-01 08:00:00
0
0
0
java - Mybatis related query
From 1970-01-01 08:00:00
0
0
0
REGEXP in mybatis
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template