mybatis 源码mapper注入和sql执行流程图
2023-02-17 12:45:51 0 举报
这个是作者通过阅读源码画出来的mybatis流程图,仅供有需要的人
作者其他创作
大纲/内容
processBeanDefinitions方法private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) { Iterator var3 = beanDefinitions.iterator(); while(var3.hasNext()) { BeanDefinitionHolder holder = (BeanDefinitionHolder)var3.next(); GenericBeanDefinition definition = (GenericBeanDefinition)holder.getBeanDefinition(); if (this.logger.isDebugEnabled()) { this.logger.debug(\"Creating MapperFactoryBean with name '\" + holder.getBeanName() + \"' and '\" + definition.getBeanClassName() + \"' mapperInterface\"); } definition.getConstructorArgumentValues().addGenericArgumentValue(definition.getBeanClassName()); definition.setBeanClass(this.mapperFactoryBean.getClass());}
StatementHandler
setParameters()
Configuration
@Import({MapperScannerRegistrar.class})
SqlSessionFactory
public void parameterize(Statement statement) throws SQLException { this.parameterHandler.setParameters((PreparedStatement)statement); }
doQuery
handleResultSets()
getMapper()
invoke()
MapperRegistry
BaseExecutor
sqlSession
registerBeanDefinitionregisterBeanDefinitions()
Executor
预编译sql
ParameterHandler
newInstance()
为什么要将接口的Bean设置为MapperFactoryBean呢,就是因为当IOC容器中Bean实现了FactoryBean后,通过getBean获取到的Bean对象并不是FactoryBean的实现类对象,而是实现类getObject方法返回的对象。MapperFactoryBean类的getObject方法如下:
实现
execute()
public void setParameters(PreparedStatement ps) { ErrorContext.instance().activity(\"setting parameters\").object(this.mappedStatement.getParameterMap().getId()); List<ParameterMapping> parameterMappings = this.boundSql.getParameterMappings(); if (parameterMappings != null) { for(int i = 0; i < parameterMappings.size(); ++i) { ParameterMapping parameterMapping = (ParameterMapping)parameterMappings.get(i); if (parameterMapping.getMode() != ParameterMode.OUT) { String propertyName = parameterMapping.getProperty(); Object value; if (this.boundSql.hasAdditionalParameter(propertyName)) { value = this.boundSql.getAdditionalParameter(propertyName); } else if (this.parameterObject == null) { value = null; } else if (this.typeHandlerRegistry.hasTypeHandler(this.parameterObject.getClass())) { value = this.parameterObject; } else { MetaObject metaObject = this.configuration.newMetaObject(this.parameterObject); value = metaObject.getValue(propertyName); } font color=\"#ff3333\
getMapper()
public Statement font color=\"#ff3333\
public class MapperProxy<T> implements font color=\"#ff3333\
MapperProxy
封装参数
getObject()
public Objectfont color=\"#ff3333\
SimpleExecutor
public List<Object> handleResultSets(Statement stmt) throws SQLException { ErrorContext.instance().activity(\"handling results\
MapperScan
ResultHandler
query()
processBeanDefinitions()
DefaultResultSetHandler
DefaultParameterHandler
参数封装
doQuer()
MapperFactoryBean
public void parameterize(Statement statement) throws SQLException { this.parameterHandler.setParameters((PreparedStatement)statement); }
basePackage=com.dv.xx
SqlSessionFactoryBuilder
执行里面的doScan()方法
prepareStatement()
configuration.getMapper
public class MapperFactoryBean<T> extends SqlSessionDaoSupport implements FactoryBean<T> {public T getObject() throws Exception { return this.getSqlSession().getMapper(this.mapperInterface); }}
BaseStatementHandler
public Set<BeanDefinitionHolder> doScan(String... basePackages) { Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages); if (beanDefinitions.isEmpty()) { this.logger.warn(\"No MyBatis mapper was found in '\" + Arrays.toString(basePackages) + \"' package. Please check your configuration.\"); } else { this.processBeanDefinitions(beanDefinitions); } return beanDefinitions; }
doScan()
DefaultSqlSession
结果映射
接口:sqlsession
font color=\"#ff0000\
selectList()
ClassPathMapperScanner
MapperMethod
PreparedStatementHandler
MapperScannerRegistrar
ResultSetHandler
0 条评论
下一页