mybatis提供了一种非常简便的方式去访问数据库,定义接口和sql之后,就能自动帮你完成jdbc操作。这得益于它的mapper机制,本篇文章在于分析mapper的流程。
DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
从mybatis的官方示例代码可以看到,mapper接口的加载是通过Configuration类的addMapper方法去实现的。
public <T> void addMapper(Class<T> type) {
this.knownMappers.put(type, new MapperProxyFactory(type));
MapperAnnotationBuilder parser = new MapperAnnotationBuilder(this.config, type);
parser.parse();
}
可以看到,这里创建了一个MapperProxyFactory对象,当用户调用getMapper方法时,则会调用该factory生成一个基于jdk的代理
public T newInstance(SqlSession sqlSession) {
final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
return newInstance(mapperProxy);
}
protected T newInstance(MapperProxy<T> mapperProxy) {
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}
最终调用方法时,则是通过MapperProxy的invoke方法完成逻辑调用。
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
final MapperMethod mapperMethod = cachedMapperMethod(method);
return mapperMethod.execute(sqlSession, args);
}
MapperProxy内部由MapperMethod的execute完成方法调用,该方法调用sqlSession完成操作,即完成了mapper的封装。
if (SqlCommandType.INSERT == command.getType()) {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.insert(command.getName(), param));
...
网友评论