Spring source code analysis (7) Spring integration mybatis source code analysis

​​​​​​​​​​​​​​​​​​​​​https://blog.csdn.net/caicongyang/category_2139981.html?spm=1001.2014.3001.5482 

My other Spring source code analysis is as above

1. Introduce MapperScannerRegistrar into the @MapperScan annotation

2.MapperScannerRegistrar inherits ImportBeanDefinitionRegistrar. During the Spring process, the registerBeanDefinitions method will be called to obtain a MapperScannerConfigurer object.

3. At the same time, MapperScannerConfigurer is a BeanDefinitionRegistryPostProcessor object and will call the postProcessBeanDefinitionRegistry method during Spring startup;

4. In the implementation of the postProcessBeanDefinitionRegistry method, mybatis customizes ClassPathMapperScanner and implements Spring's ClassPathBeanDefinitionScanner to scan the mapper interface of mybatis into a Spring bean.

5. Modify the scanned BeanDefinition, change beanClass to MapperFactoryBean, and change AutowireMode to byType

6. In the getObject method in MapperFactoryBean, getSqlSession() is called to get a sqlSession object, and then a Mapper interface proxy object is generated based on the corresponding Mapper interface. This proxy object becomes a Bean in the Spring container.

7. The sqlSession object is in Mybatis. A sqlSession object requires SqlSessionFactory to generate it.

8. The AutowireMode of MapperFactoryBean is byType, so Spring will automatically call the set method. There are two set methods, one setSqlSessionFactory and one setSqlSessionTemplate. The prerequisite for the execution of these two methods is that the corresponding bean can be found according to the method parameter type, so the Spring container There must be a bean of SqlSessionFactory type or a bean of SqlSessionTemplate type.

9. If you define a bean of type SqlSessionFactory, it will eventually be packaged as a SqlSessionTemplate object and assigned to the sqlSession property.

10. There is a getMapper method in the SqlSessionTemplate class, which generates a Mapper interface proxy object.

11. The subsequent process will enter the underlying execution process of the Mybatis framework.

おすすめ

転載: blog.csdn.net/caicongyang/article/details/122900247