The foreword 0.

MybatisPlus Mapper layer interface implementation

1.MybatisPlusAutoConfiguration

In order to further understand the MP MybatisConfiguration relevant properties and create SqlSessionTemplate, since MybatisPlusAutoConfiguration. In the auto-configuration class, there is the method sqlSessionFactory, which returns the sqlSessionFactory (factory bean of factory bean, Used to analyse the source “production” factory) is the method with new MybatisSqlSessionFactoryBean factories to build factories, and through the getObject () method returns the factory (type of DefaultSqlSessionFactory). In addition, the method will also perform MyBatis components, MyBatis-plus primary key generator, SQL injector and other components injection operations, see the source code.

public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean(); . return factory.getObject(); }Copy the code

2.MybatisSqlSessionFactoryBean

Through the getObject () method into MybatisSqlSessionFactoryBean, discovery method buildSqlSessionFactory () returns to the realization of ultimate DefaultSqlSessionFactory. In this method, there is an important object, targetConfiguration(type: MybatisConfiguration). This object is used to store the parsing information of the MapperXML file and to generate the SqlSessionFactory.

protected SqlSessionFactory buildSqlSessionFactory() throws Exception { final MybatisConfiguration targetConfiguration; // TODO uses MybatisXmlConfigBuilder instead of XMLConfigBuilder MybatisXmlConfigBuilder = null; . if (this.mapperLocations ! = null) { if (this.mapperLocations.length == 0) { LOGGER.warn(() -> "Property 'mapperLocations' was specified but matching resources are not found."); } else { for (Resource mapperLocation : this.mapperLocations) { if (mapperLocation == null) { continue; } try { XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(), targetConfiguration, mapperLocation.toString(), targetConfiguration.getSqlFragments()); xmlMapperBuilder.parse(); } catch (Exception e) { throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e); } finally { ErrorContext.instance().reset(); } LOGGER.debug(() -> "Parsed mapper file: '" + mapperLocation + "'"); } } } else { LOGGER.debug(() -> "Property 'mapperLocations' was not specified."); } final SqlSessionFactory sqlSessionFactory = new MybatisSqlSessionFactoryBuilder().build(targetConfiguration); . return sqlSessionFactory;Copy the code

The parsing process of XML files is quite tedious, which is introduced in Chapter 3. This chapter only compares the targetConfiguration attribute before and after XML parsing, and it can be found that the XML information of mapper interface after parsing is stored in the mybatisMpperRegistry attribute.

In addition, from mybatisMpperRegistry, config->mappedStatements->key and value key stores the ID of namespace+ SQL statement in XML. Value stores the MappedStatement object, where the SqlSource attribute is the SQL statement under the corresponding ID.

To sum up, in MybatisPlusAutoConfiguration bean plant production, has been will be parsed and stored in the mybatisMapperRegistry mapperxml file, It is passed to the factory bean via targetConfiguration, and after mapper injection and interface method invocation, the factory bean produces the SqlSessionTemplate object for database queries.

3. XML file parsing process of Mapper interface

To be continued…