拼多多二面:Mybatis是如何执行一条SQL命令的?
阅读本文大概需要 3 分钟。
public enum SqlCommandType {UNKNOWN, INSERT, UPDATE, DELETE, SELECT, FLUSH;}
public interface StudentMapper {ListfindAllStudents(Map<String, Object> map, RowBounds rowBounds, ResultSetHandler rh); }

private final MapmethodCache; 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);}// 缓存MapperMethodprivate MapperMethod cachedMapperMethod(Method method) {MapperMethod mapperMethod = methodCache.get(method);if (mapperMethod == null) {mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());methodCache.put(method, mapperMethod);}return mapperMethod;}
public class MapperMethod {// 保存了Sql命令的类型和键idprivate final SqlCommand command;// 保存了Mapper接口方法的解析信息private final MethodSignature method;public MapperMethod(Class> mapperInterface, Method method, Configuration config) {this.command = new SqlCommand(config, mapperInterface, method);this.method = new MethodSignature(config, method);}// 根据解析结果,路由到恰当的SqlSession方法上public Object execute(SqlSession sqlSession, Object[] args) {Object result;if (SqlCommandType.INSERT == command.getType()) {Object param = method.convertArgsToSqlCommandParam(args);result = rowCountResult(sqlSession.insert(command.getName(), param));} else if (SqlCommandType.UPDATE == command.getType()) {Object param = method.convertArgsToSqlCommandParam(args);result = rowCountResult(sqlSession.update(command.getName(), param));} else if (SqlCommandType.DELETE == command.getType()) {Object param = method.convertArgsToSqlCommandParam(args);result = rowCountResult(sqlSession.delete(command.getName(), param));} else if (SqlCommandType.SELECT == command.getType()) {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 {Object param = method.convertArgsToSqlCommandParam(args);result = sqlSession.selectOne(command.getName(), param);}} else if (SqlCommandType.FLUSH == command.getType()) {result = sqlSession.flushStatements();} else {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;}// ...
public static class SqlCommand {// full id, 通过它可以找到MappedStatementprivate final String name;private final SqlCommandType type;// ...
public static class MethodSignature {private final boolean returnsMany;private final boolean returnsMap;private final boolean returnsVoid;private final Class> returnType;private final String mapKey;private final Integer resultHandlerIndex;private final Integer rowBoundsIndex;private final SortedMapparams; private final boolean hasNamedParameters;public MethodSignature(Configuration configuration, Method method) {this.returnType = method.getReturnType();this.returnsVoid = void.class.equals(this.returnType);this.returnsMany = (configuration.getObjectFactory().isCollection(this.returnType) || this.returnType.isArray());this.mapKey = getMapKey(method);this.returnsMap = (this.mapKey != null);this.hasNamedParameters = hasNamedParams(method);// 分页参数this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class);// 自定义ResultHandlerthis.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class);this.params = Collections.unmodifiableSortedMap(getParams(method, this.hasNamedParameters));}
推荐阅读:
00后大学生在家发射火箭,从发动机到软件全部自制,「初中时受马斯克鼓舞」
微信扫描二维码,关注我的公众号
朕已阅 
评论

