在使用Mybatis开发中,或者和Spring整合中,在Dao层中的Mapper接口对xml中的sql对应着,在service中直接调用Dao中的方法达到访问sql,如下:
这是一个Mapper的接口
public interface ActivityCzMapper {
ActivityCz selectByPrimaryKey(Integer id);
}
xml中对应的sql
...
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from activity_cz
where id = #{id,jdbcType=INTEGER}
</select>
...
ActivityCzMapper 接口并没有实现类,那是如何调用的sql呢?可以在源码中学习
在MyBatis中调用一个sql时候,sqlSession.selectList(""); 在这之前通过SqlSessionFactoryBuilder的build("MYbatis配置文件")出的sqlsession。
Mybatis调用接口中方法时如下
ActivityCzMapper am = sqlSession.getMapper(ActivityCzMapper.class);
ActivityCz activityCz = am.selectByPrimaryKey(1);
我们查看org.apache.ibatis下的session的DefaultSqlSession代码的getMapper方法
DefaultSqlSession.class文件
...
public <T> T getMapper(Class<T> type) {
return this.configuration.getMapper(type, this);
}
...
继续找getMapper方法,在Configuration.class中
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
return this.mapperRegistry.getMapper(type, sqlSession);
}
注意这里getMapper(type, sqlSession)方法里的type是一直传递的Mapper接口类,sqlSession是上面的DefaultSqlSession实例。继续跳到MapperRegistry.class中的getMapper(type,sqlSession)方法中
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory)this.knownMappers.get(type);
if(mapperProxyFactory == null) {
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
} else {
try {
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception var5) {
throw new BindingException("Error getting mapper instance. Cause: " + var5, var5);
}
}
}
这里首先看return的东西,是一个mapperProxyFactory的 newInstance实例,那么可以猜想是利用动态代理模式,继续看mapperProxyFactory是从哪里来的,可以看到这行代码: MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory)this.knownMappers.get(type); 通过knownMappers一个map 得到的--因为在类MapperRegistry中定义了private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap()这个map,
knownMappers中get的key是我们传来的Mapper接口(最上面示例的ActivityCzMapper ),那说明这个map在我们getMapper方法之前应该add过,在类MapperRegistry下面可以看到代码:
public <T> void addMapper(Class<T> type) {
if(type.isInterface()) {
if(this.hasMapper(type)) {
throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
}
boolean loadCompleted = false;
try {
this.knownMappers.put(type, new MapperProxyFactory(type));
MapperAnnotationBuilder parser = new MapperAnnotationBuilder(this.config, type);
parser.parse();
loadCompleted = true;
} finally {
if(!loadCompleted) {
this.knownMappers.remove(type);
}
}
}
}
发现了add里面具体的就不看,重点有一句代码this.knownMappers.put(type, new MapperProxyFactory(type));
可以理解这里的type就是我们的Mapper接口,把他当成key,put到map中。
这里我们再回到MapperRegistry.class中的getMapper(type,sqlSession)方法中,找到return mapperProxyFactory.newInstance(sqlSession)的东西
跳到MapperProxyFactory.class中,可以看到代码
public T newInstance(SqlSession sqlSession) {
MapperProxy<T> mapperProxy = new MapperProxy(sqlSession, this.mapperInterface, this.methodCache);
return this.newInstance(mapperProxy);
}
看到这个方法里,new了一个MapperProxy的构造方法,里面有三个参数,第一个sqlSession是我前面提起的DefaultSqlSession,第二个mapperInterface是MapperProxyFactory.class中的定义的成员变量,就是我们传的Mapper接口,在我们前面找到的addMapper(Class<T> type) 方法中那个knownMappers的map的value值。第三个参数是一个初始化的ConcurrentHashMap在类中这样定义
private Map<Method, MapperMethod> methodCache = new ConcurrentHashMap();
是空的。
注意下这个方法中new的MapperProxy类,这个类
public class MapperProxy<T> implements InvocationHandler, Serializable {
...
}
他实现了InvocationHandler,说明利用了JDK的动态代理,把三个参数初始化的MapperProxy代理类传到下一个重载方法。
继续找到当前类的newInstance的重载方法
protected T newInstance(MapperProxy<T> mapperProxy) {
return Proxy.newProxyInstance(this.mapperInterface.getClassLoader(), new Class[]{this.mapperInterface}, mapperProxy);
}
到这里就可以知道了利用jdk的动态代理模式Proxy.newProxyInstance方法,里面的三个参数显而易见,对于第三个参数mapperProxy就是 实现了InvocationHandler接口的MapperProxy类。