关于Mybatis中mapper方法重载问题

Mybatis面试注意点

接口Mapper内的方法能重载吗?
比如:

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

回答:不能,Mybatis使用package+Mapper+method全限定名作为key,去**.xml内寻找唯一sql执行,类似key=com.hmr…mapper.UserMapper.getUserById,因此,在Mapper内禁止方法重载.*

具体的可以去查看Mybatis自动映射器Mapper的源码分析,以下附上相关代码

public class MapperProxy<T> implements InvocationHandler, Serializable {

	private static final long serialVersionUID = -6424540398559729838L;
	private final SqlSession sqlSession;
	private final Class<T> mapperInterface;
	private final Map<Method, MapperMethod> methodCache;

	public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> 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);
  }



public class MapperProxyFactory<T> {
	
	  private final Class<T> mapperInterface;
	
	  @SuppressWarnings("unchecked")
	  protected T newInstance(MapperProxy<T> mapperProxy) {
	    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值