Author: HuiFer
Description: 该文介绍 mybatis sqlCommand 类的源码
源码阅读工程: SourceHot-Mybatis
org.apache.ibatis.binding.MapperMethod.SqlCommand
/** * 核心内容: sql id , Sql 类型 */publicstaticclassSqlCommand { /** * sql id */privatefinalStringname; /** * sql 类型select|update|delete|insert|... */privatefinalSqlCommandTypetype; /** * 根据传递的参数 设置sql的一些属性 , sql id , type . * * @param configuration * @param mapperInterface * @param method */publicSqlCommand(Configurationconfiguration, Class<?> mapperInterface, Methodmethod) { // 方法名finalStringmethodName = method.getName(); finalClass<?> declaringClass = method.getDeclaringClass(); // Statement 实质是sqlMappedStatementms = resolveMappedStatement(mapperInterface, methodName, declaringClass, configuration); if (ms == null) { if (method.getAnnotation(Flush.class) != null) { name = null; type = SqlCommandType.FLUSH; } else { thrownewBindingException("Invalid bound statement (not found): " + mapperInterface.getName() + "." + methodName); } } else { name = ms.getId(); type = ms.getSqlCommandType(); if (type == SqlCommandType.UNKNOWN) { thrownewBindingException("Unknown execution method for: " + name); } } } publicStringgetName() { returnname; } publicSqlCommandTypegetType() { returntype; } /** * @param mapperInterface mapper.class * @param methodName 方法名 * @param declaringClass 可能是 mapper.class * @param configuration * @return */privateMappedStatementresolveMappedStatement(Class<?> mapperInterface, StringmethodName, Class<?> declaringClass, Configurationconfiguration) { // 接口名称+方法名StringstatementId = mapperInterface.getName() + "." + methodName; if (configuration.hasStatement(statementId)) { // 从 configuration 获取returnconfiguration.getMappedStatement(statementId); } elseif (mapperInterface.equals(declaringClass)) { returnnull; } // new 一个新的实例for (Class<?> superInterface : mapperInterface.getInterfaces()) { if (declaringClass.isAssignableFrom(superInterface)) { MappedStatementms = resolveMappedStatement(superInterface, methodName, declaringClass, configuration); if (ms != null) { returnms; } } } returnnull; } }