mapper方法在Mybatis中为什么不能重载
创始人
2024-12-27 08:41:45
0次

动态代理的功能:通过拦截器方法回调,对目标target方法进行增强。

言外之意就是为了增强目标target方法。上面这句话没错,但也不要认为它就是真理,殊不知,动态代理还有投鞭断流的霸权,连目标target都不要的科幻模式。

1. 自定义JDK动态代理之投鞭断流实现自动映射器Mapper

首先定义一个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
}

再定义一个接口UserMapper.java。

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

接下来我们看看如何使用动态代理之投鞭断流,实现实例化接口并调用接口方法返回数据的。

自定义一个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 newInstance(Class 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 {
 // 诸如hashCode()、toString()、equals()等方法,将target指向当前对象this
 return method.invoke(this, args);
 } catch (Throwable t) {
 }
 }
 // 投鞭断流
 return new User((Integer) args[0], "zhangsan", 18);
 }
}

上面代码中的target,在执行Object.java内的方法时,target被指向了this,target已经变成了傀儡、象征、占位符。在投鞭断流式的拦截时,已经没有了target。

写一个测试代码:

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

output:

ID:1001
Name:zhangsan
Age:18 page
x.y.MapperProxy@6bc7c054

这便是Mybatis自动映射器Mapper的底层实现原理。

可能有读者不禁要问:你怎么把代码写的像初学者写的一样?没有结构,且缺乏美感。

必须声明,作为一名经验老道的高手,能把程序写的像初学者写的一样,那必定是高手中的高手。这样可以让初学者感觉到亲切,舒服,符合自己的Style,让他们或她们,感觉到大牛写的代码也不过如此,自己甚至写的比这些大牛写的还要好,从此自信满满,热情高涨,认为与大牛之间的差距,仅剩下三分钟。

2. Mybatis自动映射器Mapper的源码分析

首先编写一个测试类:

public static void main(String[] args) {
 SqlSession sqlSession = MybatisSqlSessionFactory.openSession();
 try {
 StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
 List students = studentMapper.findAllStudents();
 for (Student student : students) {
 System.out.println(student);
 }
 } finally {
 sqlSession.close();
 }
 }

Mapper长这个样子:

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

org.apache.ibatis.binding.MapperProxy.java部分源码。

public class MapperProxy implements InvocationHandler, Serializable {

 private static final long serialVersionUID = -6424540398559729838L;
 private final SqlSession sqlSession;
 private final Class mapperInterface;
 private final Map methodCache;

 public MapperProxy(SqlSession sqlSession, Class mapperInterface, Map methodCache) {
 this.sqlSession = sqlSession;
 this.mapperInterface = mapperInterface;
 this.methodCache = methodCache;
 }

 @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) {
 throw ExceptionUtil.unwrapThrowable(t);
 }
 }
 // 投鞭断流
 final MapperMethod mapperMethod = cachedMapperMethod(method);
 return mapperMethod.execute(sqlSession, args);
 }
 // ...

org.apache.ibatis.binding.MapperProxyFactory.java部分源码。

public class MapperProxyFactory {

 private final Class mapperInterface;

 @SuppressWarnings("unchecked")
 protected T newInstance(MapperProxy mapperProxy) {
 return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
 }

这便是Mybatis使用动态代理之投鞭断流。

3. 接口Mapper内的方法能重载(overLoad)吗?(重要)

类似下面:

public User getUserById(Integer id);
public User getUserById(Integer id, String name);

Answer:不能。

原因:在投鞭断流时,Mybatis使用package+Mapper+method全限名作为key,去xml内寻找唯一sql来执行的。类似:key=x.y.UserMapper.getUserById,那么,重载方法时将导致矛盾。对于Mapper接口,Mybatis禁止方法重载(overLoad)。

关于mapper方法在Mybatis中为什么不能重载问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...