程序员社区

Mybatis框架-----Mapper 动态代理原理


Mybatis框架-----Mapper 动态代理原理

文章目录

  • 🔥一、获取代理对象 MapperProxy
  • 🔥二、通过 MapperMethod 对象执行对应的操作
  • 🔥三、每日一夸


🍎姓名:洋葱爱代码🍎


🍊喜欢:Java编程🍊


🍉重要的事情说三遍!!!🍉


🍓欢迎大家来访问哦,互相学习🍓


🍋欢迎大家来访问哦,互相学习🍋


🍑欢迎大家来访问哦,互相学习🍑

✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨

🍎合抱之木,生于毫末;百丈之台,起于垒土;千里之行,始于足下。------《老子》
🍊今日学习任务!!!!!
🍊1、了解代理对象 MapperProxy和MapperMethod 对象

🔥一、获取代理对象 MapperProxy

在这里插入图片描述
在这里插入图片描述

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
final MapperProxyFactory<T> mapperProxyFactory =
(MapperProxyFactory<T>) knownMappers.get(type);
if (mapperProxyFactory == null) {
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
}
try {
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception e) {
throw new BindingException("Error getting mapper instance. Cause:
" + e, e);
}
}
public T newInstance(SqlSession sqlSession) {
final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession,
mapperInterface, methodCache);
return newInstance(mapperProxy);
}

✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨

🔥二、通过 MapperMethod 对象执行对应的操作

public Object invoke(Object proxy, Method method, Object[] args,
SqlSession sqlSession) throws Throwable {
return mapperMethod.execute(sqlSession, args);
}
public Object execute(SqlSession sqlSession, Object[] args) {
Object result;
switch (command.getType()) {
case INSERT: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.insert(command.getName(),
param));
break;
}
case UPDATE: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.update(command.getName(),
param));
break;
}
case DELETE: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.delete(command.getName(),
param));
65
break;
}
case SELECT:
if (method.returnsVoid() && method.hasResultHandler()) {
executeWithResultHandler(sqlSession, args);
result = null;
} else if (method.returnsMany()) {
result = executeForMany(sqlSession, args);
} else if (method.returnsMap()) {
result = executeForMap(sqlSession, args);
} else if (method.returnsCursor()) {
result = executeForCursor(sqlSession, args);
} else {
Object param = method.convertArgsToSqlCommandParam(args);
result = sqlSession.selectOne(command.getName(), param);
if (method.returnsOptional()
&& (result == null
|| !method.getReturnType().equals(result.getClass()))) {
result = Optional.ofNullable(result);
}
}
break;
case FLUSH:
result = sqlSession.flushStatements();
break;
default:
throw new BindingException("Unknown execution method for: " +
command.getName());
}
if (result == null && method.getReturnType().isPrimitive()
&& !method.returnsVoid()) {
throw new BindingException("Mapper method '" + command.getName()
+ " attempted to return null from a method with a primitive return
type (" + method.getReturnType() + ").");
}
return result;
}

✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨

🔥三、每日一夸

🍑每日一夸!!!
🍑就如同薛之謙对毛不易夸奖的
🍑"你现在就是流星" , 但是我相信你 ,你坚持下去你就是巨星"

赞(0) 打赏
未经允许不得转载:IDEA激活码 » Mybatis框架-----Mapper 动态代理原理

一个分享Java & Python知识的社区