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 {