mybatis总结

  • mybatis-spring类结构

mybatis总结_第1张图片

scanner:

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	    <property name="basePackage" value="xx.dao;ss.dao" />
	</bean>
  1.  scanner.doScan()方法扫描包路径下所有类,设置beanDefinition的相关属性,设置sqlSessionTemplate,设置SqlSessionFactory;
  2. FactoryBean.checkDaoConfig()设置conriguration.addMapper();
  3. FactoryBean.getObject():SqlSessionTemplate override getMapper():Configuration.getMapper():使用mapperRegistry.getMapper();使用MapperProxy.newInstance(),生成MapperMethod。
  4. spring autowired的dao对象时,使用到代理:MapperProxy对象。
  5. 调用mapper.invoke()方法,生成MapperMethod对象,调用execute方法:
public Object execute(Object[] args) {
    Object result = null;
    if (SqlCommandType.INSERT == type) {
      Object param = getParam(args);
      result = sqlSession.insert(commandName, param);
    } else if (SqlCommandType.UPDATE == type) {
      Object param = getParam(args);
      result = sqlSession.update(commandName, param);
    } else if (SqlCommandType.DELETE == type) {
      Object param = getParam(args);
      result = sqlSession.delete(commandName, param);
    } else if (SqlCommandType.SELECT == type) {
      if (returnsVoid && resultHandlerIndex != null) {
        executeWithResultHandler(args);
      } else if (returnsList) {
        result = executeForList(args);
      } else if (returnsMap) {
        result = executeForMap(args);
      } else {
        Object param = getParam(args);
        result = sqlSession.selectOne(commandName, param);
      }
    } else {
      throw new BindingException("Unknown execution method for: " + commandName);
    }
    return result;
  }

 再回掉SqlSessionTemplate的具体方法进行执行sql和resultMap。

其中涉及到事务的部分,走配置和特殊拦截器,默认无事务。

 

 

http://www.axure.org/tools/

你可能感兴趣的:(mybatis)