Unlocking MyBatis Mapper Magic: Custom JDK Dynamic Proxy Explained

This article explains how a custom JDK dynamic proxy can automatically map interface methods to concrete objects, demonstrates the implementation with a User POJO and UserMapper, shows test output, analyzes MyBatis internal proxy classes, and clarifies why mapper methods cannot be overloaded.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Unlocking MyBatis Mapper Magic: Custom JDK Dynamic Proxy Explained

Custom JDK Dynamic Proxy for Automatic Mapper

Dynamic proxies intercept method calls to enhance the target method, providing automatic mapping capabilities.

1. Define POJO

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
}

2. Define Mapper Interface

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

3. Implement 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 {
        return method.invoke(this, args);
      } catch (Throwable t) {}
    }
    // 投鞭断流
    return new User((Integer) args[0], "zhangsan", 18);
  }
}

Test code and output demonstrate the proxy in action.

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());
}
ID:1001
Name:zhangsan
Age:18
x.y.MapperProxy@6bc7c054

This demonstrates the underlying principle of MyBatis automatic mapper.

2. MyBatis Mapper Source Analysis

Example test class using SqlSession to obtain a StudentMapper and list students.

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();
  }
}

StudentMapper interface definition.

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

Key excerpts from org.apache.ibatis.binding.MapperProxy and MapperProxyFactory show how MyBatis employs dynamic proxies and the “投鞭断流” technique.

3. Can Mapper Methods Be Overloaded?

Answer: No. MyBatis uses the fully qualified method name as a key to locate a unique SQL statement; overloaded methods would cause key collisions, so overload is prohibited.

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.

JavaReflectionMyBatisDynamic 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.