MyBatis 源码分析java
这张图总结的很到位::spring
knownMappers.put(type, new MapperProxyFactory<T>(type));
openSessionFromDataSource
ClassPathMapperScanner doScan方法的真正调用地方
definition.setBeanClass(this.mapperFactoryBean.getClass()); 注册 beanDefinition class 为mapperFactoryBean
xmlConfigBuilder
configuration = xmlConfigBuilder.getConfiguration();
XMLStatementBuilder
builderAssistant.addMappedStatement...
MappedStatement.Builder statementBuilder = new MappedStatement.Builder(...
XMLMapperBuilder 解析xml 配置文件,包括 mapper.xml 的配置
xmlMapperBuilder.parse();
bindMapperForNamespace 绑定 mapper
configuration.addMapper(boundType);
mapperRegistry.addMapper(type);
knownMappers.put(type, new MapperProxyFactory<T>(type));
return this.sqlSessionFactoryBuilder.build(configuration);
return new DefaultSqlSessionFactory(config);
MapperFactoryBean<T> extends SqlSessionDaoSupport implements FactoryBean<T> {
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { if (!this.externalSqlSession) { this.sqlSession = new SqlSessionTemplate(sqlSessionFactory); } }
SqlSessionTemplate 本质是 SqlSession 的装饰器
this.sqlSessionProxy = (SqlSession) newProxyInstance( // sqlSessionProxy 本质是 SqlSession的动态代理 new Class[] { SqlSession.class }, new SqlSessionInterceptor());
if (!isEmpty(this.plugins)) { for (Interceptor plugin : this.plugins) { configuration.addInterceptor(plugin); // 初始化插件 if (LOGGER.isDebugEnabled()) { LOGGER.debug("Registered plugin: '" + plugin + "'"); } } }
interceptorChain.addInterceptor(interceptor);
protected final InterceptorChain interceptorChain = new InterceptorChain(); // InterceptorChain 是configuration 的成员变量
好比:doUpdate 时候,执行 newParameterHandler
StatementHandler handler = configuration.newStatementHandler(
public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) { ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql); parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler); return parameterHandler; }
findCandidateComponents
if (isCandidateComponent(sbd)) {
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) { return beanDefinition.getMetadata().isInterface() && beanDefinition.getMetadata().isIndependent(); }
public static void main(String[] args) { //定义 SqlSessionFactory SqlSessionFactory sqlSessionFactory = null; try { //使用配置文件建立 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build( Resources.getResourceAsReader("mybatis-config.xml")); } catch (IOException ex) { //打印异常. Logger.getLogger(MainCh1.class.getName()).fatal("建立 SqlSessionFactory失败", ex); return; } //定义 sqlSession SqlSession sqlSession = null; try { //用sqlSessionFactory建立sqlSession sqlSession = sqlSessionFactory.openSession(); //获取Mapper UserMapper userMapper = sqlSession.getMapper(UserMapper.class); //执行Mapper接口方法. UserPO user = userMapper.findUser(1); //打印信息 System.err.println(user.getUsername()); } finally { //使用完后要记得关闭sqlSession资源 if (sqlSession != null) { sqlSession.close(); } } }
<import resource="spring-mybatis.xml"/>
@Intercepts( { @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}), @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}), } )
public class PageInterceptor implements Interceptor {