//Configuration里面 MapperRegistry mapperRegistry = new MapperRegistry(this);//mapperRegistry里面有Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<Class<?>, MapperProxyFactory<?>>(); key是类名,value是个反射的代理工厂privatevoidbindMapperForNamespace(){//当前的namespaceString namespace = builderAssistant.getCurrentNamespace();if(namespace !=null){Class<?> boundType =null;try{//反射获得里面的mapper的类
boundType =Resources.classForName(namespace);//反射获得class 类 class com,wjk,}catch(ClassNotFoundException e){//ignore, bound type is not required}if(boundType !=null){//判断是否已经注入过了,如果没有注入 mapperRegistry是否有boundTypeif(!configuration.hasMapper(boundType)){
configuration.addLoadedResource("namespace:"+ namespace);
configuration.addMapper(boundType);}}}}
configuration.addMapper(boundType)
public<T>voidaddMapper(Class<T> type){if(type.isInterface()){//是否已经绑定过Mapperif(hasMapper(type)){thrownewBindingException("Type "+ type +" is already known to the MapperRegistry.");}boolean loadCompleted =false;try{//MapperProxyFactory给我们的mapper生成代理类使用,type就是我们的mapper
knownMappers.put(type,newMapperProxyFactory<T>(type));// It's important that the type is added before the parser is run// otherwise the binding may automatically be attempted by the// mapper parser. If the type is already known, it won't try.//下面是注解的类型,我们这边先不用MapperAnnotationBuilder parser =newMapperAnnotationBuilder(config, type);
parser.parse();
loadCompleted =true;}finally{if(!loadCompleted){
knownMappers.remove(type);}}}}//判断是否已经新增过了//Map<Class<?>, MapperProxyFactory<?>> knownMapperspublic<T>booleanhasMapper(Class<T> type){return knownMappers.containsKey(type);}
总结:
我们已经把MappedStatement放入configuration里面了,然后再映射当前mapper的class,放入到configuration里面的MapperRegistry里面,具体是放到mapperRegistry的knownMappers里面(Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<Class<?>, MapperProxyFactory<?>>()) MapperProxyFactory到时候给mapper类生成代理类使用