How MyBatis Instantiates Mapper Interfaces with JDK Dynamic Proxies

This article explains how MyBatis uses JDK dynamic proxies—specifically a custom “投鞭断流” approach—to automatically create mapper interface implementations, walks through code examples, analyzes core source files, and clarifies why method overloading is prohibited in mapper interfaces.

Java Backend Technology
Java Backend Technology
Java Backend Technology
How MyBatis Instantiates Mapper Interfaces with JDK Dynamic Proxies

During a casual walk, the author wondered why MyBatis can return an instance of a mapper interface without an explicit implementation class. The answer lies in JDK dynamic proxies and a custom “投鞭断流” (whip‑break) mechanism that intercepts method calls and maps them to SQL statements.

1. Custom JDK Dynamic Proxy for Automatic Mapper Creation

First, define a POJO (image omitted for brevity).

Then create the mapper interface UserMapper.java (image shown).

Implement a custom InvocationHandler (image shown).

In this handler, the target is set to this, turning the real object into a placeholder. When the proxy intercepts a method call, the original target is no longer needed.

A test program (image) demonstrates the output (image), revealing the underlying mechanism of MyBatis automatic mapper generation.

The author notes that the deliberately simple style is meant to make the code feel approachable to beginners.

2. Source Code Analysis of MyBatis Mapper Proxy

A test class (image) is used to illustrate the generated mapper.

The mapper interface looks like (image).

Key parts of MapperProxy.java and MapperProxyFactory.java are shown (images), demonstrating how MyBatis builds the proxy and binds method calls to SQL statements.

3. Can Mapper Interface Methods Be Overloaded?

Example of an overloaded method (image) is presented.

Answer: No. MyBatis uses the fully‑qualified package+Mapper+method name as a key to locate the corresponding SQL in XML. Overloaded methods would create ambiguous keys, so MyBatis forbids method overloading in mapper interfaces.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaMyBatisDynamic Proxymapper
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.