MyBatis Mapper 动态代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class MapperRegistry {
private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<>();

public <T> void addMapper(Class<T> type) {
knownMappers.put(type, new MapperProxyFactory<>(type));
new MapperAnnotationBuilder(config, type).parse(); // 解析 @Select 等注解
}

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
return knownMappers.get(type).newInstance(sqlSession);
}
}

public class MapperProxyFactory<T> {
private final Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<>();

public T newInstance(SqlSession sqlSession) {
return (T) Proxy.newProxyInstance(
mapperInterface.getClassLoader(),
new Class<?>[]{mapperInterface},
new MapperProxy<>(sqlSession, mapperInterface, methodCache));
}
}