binding模块要解决的问题:
public interface UserInfoMapper {
UserInfo selectById(int id);//对应一个SQL
}
//如果MyBatis初始化时该没有SQL与该方法匹配,会报错。这样就帮助我们尽早找到问题
复制代码
上图描述了核心组件的关系。
//MapperRegistry
这个类通过名字就可以看出 是用来注册Mapper接口与获取生成代理类实例的工具类
Configuration是MyBatis全局性的配置对象,在MyBatis初始化的过程中,所有配置信息都会解析成相对应的对象记录到Configuration中。Configuration.mapperRegistry记录当前mapperRegistry对象。
public class MapperRegistry {
private final Configuration config;//全局配置文件对象
//Mapper接口与MapperProxyFactory关系 ,这个MapperProxyFactory是创建Mapper代理对象的工厂
private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<>();
复制代码
初始化时读取配置文件以及Mapper接口注解信息,并调用MapperRegistry.addMapper填充knownMappers集合。MapperProxyFactory为Mapper接口创建代理对象。
//注册Mapper接口
public <T> void addMapper(Class<T> type) {
if (type.isInterface()) {//是否为接口
if (hasMapper(type)) {//是否已经加载过该接口
throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
}
boolean loadCompleted = false;
try {//添加进集合
knownMappers.put(type, new MapperProxyFactory<T>(type));
MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
parser.parse();
loadCompleted = true;
} finally {
if (!loadCompleted) {
knownMappers.remove(type);
}
}
}
}
复制代码
在需要执行某SQL语句时,会先调用MapperRegistry.getMapper获取实现Mapper接口的代理对象。UserInfoMapper userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);这里获取的实际上是JDK代理为UserInfoMapper生成的代理对象。
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
//通过Mapper的接口类型 去Map当中查找 如果为空就抛异常
final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
if (mapperProxyFactory == null) {
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
}
try {
//否则创建一个当前接口的代理对象 并且传入sqlSession
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception e) { }
}
//这个类负责创建具体Mapper接口代理对象的工厂类
public class MapperProxyFactory<T> {
//具体Mapper接口的Class对象
private final Class<T> mapperInterface;
//该接口下面方法的缓存 key是方法对象 value是对接口中方法对象的封装
private final Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<>();
public MapperProxyFactory(Class<T> mapperInterface) {
this.mapperInterface = mapperInterface;
}
@SuppressWarnings("unchecked")
protected T newInstance(MapperProxy<T> mapperProxy) {
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}
//在这里传入sqlSession 创建一个Mapper接口的代理类
public T newInstance(SqlSession sqlSession) {
//在这里创建了MapperProxy对象 这个类实现了JDK的动态代理接口 InvocationHandler
final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
//调用上面的方法 返回一个接口的代理类
return newInstance(mapperProxy);
}
}
复制代码
MapperProxy
它实现InvocationHandler接口。
public class MapperProxy<T> implements InvocationHandler, Serializable {
private static final long serialVersionUID = -6424540398559729838L;
private final SqlSession sqlSession;//记录关联的sqlsession对象
//接口的类型对象
private final Class<T> mapperInterface;
//接口中方法的缓存 有MapperProxyFactory传递过来的。MapperMethod完成参数转换及SQL语句执行
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 {
try { //如果目标方法继承Object,直接调目标方法
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, args);
} else if (isDefaultMethod(method)) {
return invokeDefaultMethod(proxy, method, args);//对动态语言支持
}
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
//这里进行缓存
final MapperMethod mapperMethod = cachedMapperMethod(method);
//核心的地方就在这个方法里,这个方法对才是真正对SqlSession进行的包装调用
//执行SQL
return mapperMethod.execute(sqlSession, args);
}
private MapperMethod cachedMapperMethod(Method method) {
MapperMethod mapperMethod=methodCache.get(method)
if(mapperMethod==null){//创建对象,并缓存
mapperMethod=new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
methodCache.put(method,mapperMethod);//MapperMethod完成参数转换及SQL语句执行
return mapperMethod;
}
// return methodCache.computeIfAbsent(method, k -> new MapperMethod(mapperInterface, method,
// sqlSession.getConfiguration()));
}
}
复制代码
MapperMethod
MapperMethod中封装Mapper接口中对应方法的信息,以及对应SQL语句信息。
可认为MapperMethod是Mapper接口和配置文件中定义的SQL的桥梁。
private final SqlCommand command;//记录SQL的名称和类型
private final MethodSignature method;//Mapper接口中方法的信息
//根据SQL语句类型调用SQLsession对应方法完成数据库操作
public Object execute(SqlSession sqlSession, Object[] args) {
//定义返回结果
Object result;
switch (command.getType()) {//根据Sql类型调用sqlsession对应的方法
case INSERT: {//如果是INSERT操作
//使用paramNameResolver处理args(用户传入的实参),将实参与指定参数关联
Object param = method.convertArgsToSqlCommandParam(args);
//调用sqlSession的insert方法
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));
break;
}
case SELECT://处理返回值为void且ResultSet通过ResultHandler处理的方法
if (method.returnsVoid() && method.hasResultHandler()) {
executeWithResultHandler(sqlSession, args);
result = null;
} else if (method.returnsMany()) {//返回值为集合或数组的方法
result = executeForMany(sqlSession, args);
} else if (method.returnsMap()) {//返回值为map
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 = OptionalUtil.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;
}
//当执行insert、update、delete类型的SQL,结果都要经过MapperMethod.rowCountResult方法。
//会将int 转换成Mapper接口对应方法的返回值
private Object rowCountResult(int rowCount) {
final Object result;
if (method.returnsVoid()) {
result = null;//Mapper接口中相应方法的返回值为void
} else if (Integer.class.equals(method.getReturnType()) || Integer.TYPE.equals(method.getReturnType())) {
result = rowCount;//返回int 或Integer
} else if (Long.class.equals(method.getReturnType()) || Long.TYPE.equals(method.getReturnType())) {
result = (long)rowCount;
} else if (Boolean.class.equals(method.getReturnType()) || Boolean.TYPE.equals(method.getReturnType())) {
result = rowCount > 0;//返回值为boolean或Boolean
} else {
throw new BindingException("Mapper method '" + command.getName() + "' has an unsupported return type: " + method.getReturnType());
}
return result;
}
复制代码
SqlCommand
sqlCommand是内部类,使用name记录SQL的名称,type记录SQL类型。SqlCommand的构造方法会初始化name和type
public static class SqlCommand {
private final String name;//表示sql语句的名称 //xml标签的id
private final SqlCommandType type;//表示sql语句的类型
public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
//拿到全名 比如 org.mybatis.example.BlogMapper.selectBlog
//id形成
String statementId = mapperInterface.getName() + "." + methodName;
//获取MappedStatement对象 这个对象封装了XML当中一个标签的所有信息 比如下面
//<select id="selectBlog" resultType="Blog">
//select * from Blog where id = #{id}
//</select>
MappedStatement ms =null;
if (configuration.hasStatement(statementId)) {
return configuration.getMappedStatement(statementId);//MappedStatement封装SQL信息,检测是否有该名称的SQL语句
} else if (mapperInterface.equals(declaringClass)) {
return null;
}
for (Class<?> superInterface : mapperInterface.getInterfaces()) {
if (declaringClass.isAssignableFrom(superInterface)) {
MappedStatement ms = resolveMappedStatement(superInterface, methodName,
declaringClass, configuration);
if (ms != null) {
return ms;
}
}
}
if (ms == null) {
if(method.getAnnotation(Flush.class) != null){//处理@Flush注解
name = null;
type = SqlCommandType.FLUSH;
} else {
throw new BindingException("Invalid bound statement (not found): "
+ mapperInterface.getName() + "." + methodName);
}
} else {
name = ms.getId();//初始化name,type
type = ms.getSqlCommandType();
if (type == SqlCommandType.UNKNOWN) {
throw new BindingException("Unknown execution method for: " + name);
}
}
}
复制代码
//ParamNameResolver
在MethodSignature中,会使用ParamNameResolver处理Mapper接口中定义的的方法的参数。
# ParamNameResolver
private final SortedMap<Integer, String> names;//记录参数在参数列表中位置和参数名称的关系
private boolean hasParamAnnotation;//标示方法参数是否有@param注解
//构造函数通过反射的方式读取Mapper接口中对应方法的信息,并初始化上述两个字段。
public ParamNameResolver(Configuration config, Method method) {
//获取参数列表中每个参数的类型
final Class<?>[] paramTypes = method.getParameterTypes();
//获取参数列表上的注解
final Annotation[][] paramAnnotations = method.getParameterAnnotations();
//记录参数索引和参数名称的关系
final SortedMap<Integer, String> map = new TreeMap<>();
int paramCount = paramAnnotations.length;
//
for (int paramIndex = 0; paramIndex < paramCount; paramIndex++) {
if (isSpecialParameter(paramTypes[paramIndex])) {//遍历方法所有参数
// 如果参数是RowBounds或ResultHandler类型,则跳过
continue;
}
String name = null;
//遍历该参数对应的注解集合
for (Annotation annotation : paramAnnotations[paramIndex]) {
if (annotation instanceof Param) {
//@Param注解出现过一次,将hasParamAnnotation = true
hasParamAnnotation = true;
name = ((Param) annotation).value();//获取注解指定的参数名称
break;
}
}
if (name == null) {
// 该参数没@Param ,根据配置决定是否使用参数实际名称作为其名称
if (config.isUseActualParamName()) {
name = getActualParamName(method, paramIndex);
}
if (name == null) {//使用参数索引作为其名称
name = String.valueOf(map.size());
}
}
map.put(paramIndex, name);//记录到map中
}
names = Collections.unmodifiableSortedMap(map);//初始化names集合
}
//过滤RowBounds和ResultHandler
private static boolean isSpecialParameter(Class<?> clazz) {
return RowBounds.class.isAssignableFrom(clazz) || ResultHandler.class.isAssignableFrom(clazz);
}
//names集合主要在该方法里用,args表示用户传入的实参列表,并将实参与其对应名称关联
public Object getNamedParams(Object[] args) {
final int paramCount = names.size();
if (args == null || paramCount == 0) {
return null;//无参数,返回null
} else if (!hasParamAnnotation && paramCount == 1) {
return args[names.firstKey()];//未使用@param且只有一个参数
} else {//处理使用@param注解指定了的参数或多个参数的情况
final Map<String, Object> param = new ParamMap<>();
int i = 0;
for (Map.Entry<Integer, String> entry : names.entrySet()) {
//param记录参数名称与实参间对应关系。
param.put(entry.getValue(), args[entry.getKey()]);
//创建param1,param2 这种形式的参数名称
final String genericParamName = GENERIC_NAME_PREFIX + String.valueOf(i + 1);
if (!names.containsValue(genericParamName)) {
param.put(genericParamName, args[entry.getKey()]);
}
i++;
}
return param;
}
}
#MethodSignature内部类,封装Mapper接口中定义的方法信息
public static class MethodSignature {
//是否返回多调结果
private final boolean returnsMany;//返回值类型是否为Collection或数组类型
//返回值是否是MAP
private final boolean returnsMap;
//返回值是否是VOID
private final boolean returnsVoid;
private final boolean returnsCursor;//返回值类型是否为Cursor
private final boolean returnsOptional;
//返回值类型
private final Class<?> returnType;
private final String mapKey;//若返回Map,该字段记录作为key的列名
//resultHandler类型参数的位置
private final Integer resultHandlerIndex;
//rowBound类型参数的位置
private final Integer rowBoundsIndex;
//该方法对应的paramNameResolver对象
private final ParamNameResolver paramNameResolver;
//在这里对上面的属性进行初始化
public MethodSignature(Configuration configuration, Class<?> mapperInterface, Method method) {
//解析方法返回值类型
Type resolvedReturnType = TypeParameterResolver.resolveReturnType(method, mapperInterface);
if (resolvedReturnType instanceof Class<?>) {
this.returnType = (Class<?>) resolvedReturnType;
} else if (resolvedReturnType instanceof ParameterizedType) {
this.returnType = (Class<?>) ((ParameterizedType) resolvedReturnType).getRawType();
} else {
this.returnType = method.getReturnType();
}
this.returnsVoid = void.class.equals(this.returnType);
this.returnsMany = configuration.getObjectFactory().isCollection(this.returnType) || this.returnType.isArray();
this.returnsCursor = Cursor.class.equals(this.returnType);
this.returnsOptional = Jdk.optionalExists && Optional.class.equals(this.returnType);
this.mapKey = getMapKey(method);
this.returnsMap = this.mapKey != null;
this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class);
this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class);
//创建ParamNameResolver对象
this.paramNameResolver = new ParamNameResolver(configuration, method);
}
// 查找指定类型参数在参数列表的位置
private Integer getUniqueParamIndex(Method method, Class<?> paramType) {
Integer index = null;
final Class<?>[] argTypes = method.getParameterTypes();
for (int i = 0; i < argTypes.length; i++) {//遍历
if (paramType.isAssignableFrom(argTypes[i])) {//是否是这个类型
if (index == null) {
index = i;//记录索引
} else {
throw new BindingException(method.getName() + " cannot have multiple " + paramType.getSimpleName() + " parameters");
}
}
}
return index;
}
//负责将args[]数组(用户传入的实参列表)转换成SQL语句对应的参数列表
public Object convertArgsToSqlCommandParam(Object[] args) {
return paramNameResolver.getNamedParams(args);
}
//如果Mapper接口中定义的方法准备使用ResultHandler处理查询结果集,通过下面方法处理
private void executeWithResultHandler(SqlSession sqlSession, Object[] args) {
//获取SQL对应的MappedStatement对象,MappedStatement中记录SQL语句相关信息
MappedStatement ms = sqlSession.getConfiguration().getMappedStatement(command.getName());
//必须指定ResultMap或ResultType
if ( void.class.equals(ms.getResultMaps().get(0).getType())) {
throw new BindingException("method " + command.getName() );
}
Object param = method.convertArgsToSqlCommandParam(args);//转换实参列表
if (method.hasRowBounds()) {//检测参数列表中是否有RowBounds类型参数
RowBounds rowBounds = method.extractRowBounds(args);
sqlSession.select(command.getName(), param, rowBounds, method.extractResultHandler(args));
} else {
sqlSession.select(command.getName(), param, method.extractResultHandler(args));
}
}
复制代码