If method overloading is allowed, it will lead to the same method name but different parameters, so MyBati cannot accurately map the correct SQL statement according to the method name.
Assuming that method overloading is allowed, it is impossible to determine which SQL statement to execute when calling the getUser method, because the method name is the same and the parameters are different. To avoid this confusion, MyBatis does not support defining method overloading in the Mapper interface. One way to solve this problem is to use different method names to represent different queries.
It should be noted that although method overloading cannot be defined directly in the Mapper interface, multiple SQL statements with different parameters can be defined in the XML mapping file and distinguished by different IDs.
The basic working principle of MyBatis:
Method signature: The signature of a Java method consists of the method name and the type of the parameter list. For method overloading, although the method names are the same, the uniqueness of methods can be distinguished by different parameter types because of different parameter lists.
Java reflection: MyBatis calls methods in the Mapper interface through Java reflection mechanism at runtime. If overloading is allowed, MyBatis needs to determine which method should be called according to the method name and parameter type, but in reflection, the method name is unique, and it is impossible to determine which method to call by the method name and parameter type.
Dynamic proxy: MyBatis creates the proxy object of Mapper interface through dynamic proxy technology and forwards the method call to the real SQL execution. Dynamic proxy needs to match the proxy method with the actual SQL statement according to the method name and parameter type, but if there is overload, this matching will become complicated and fuzzy.