MyBatis-XML mapping file

XML mapping file

specification

  • The name of the XML mapping file is consistent with the name of the Mapper interface (EmpMapper corresponds to EmpMpper.xml), and the XML mapping file and the Mapper interface are placed under the same package (the same package with the same name)

    ​​​

    • In the maven project structure, all configuration files are under the resources directory, so the same file directory as the Mapper interface should be created in this directory. Note that / is used to classify the directory when creating the directory 

  • The namespace attribute of the XML mapping file is consistent with the fully qualified name of the Mapper interface
  • The id of the SQL statement in the XML mapping file is consistent with the method name in the Mapper interface, and the return type is consistent.
    • Method name in Mapper interface
    •  id in the XML mapping file

    • Set the return type resultType of the returned query result

      • The resultType setting is the type encapsulated by a single record of the query result

principle 

When calling a method in a Mapper interface, the corresponding SQL statement in the XML mapping file corresponding to the Mapper interface will be executed

 

As shown in Figure 3 above: when List() in the EmpMapper interface is called, the SQL statement with id=List in the corresponding EmpMapper.xml mapping file will be executed 

IDEA plugin

  • MybatisX is a plug-in for rapid development of Mybatis based on IDEA, born for efficiency.
  • Just search in the IDEA plugin
  • After the installation is successful, it will be shown in the figure below before the Mapper interface file

summary

Using annotations to map simple statements will make the code more concise, but for slightly more complex statements, Java annotations are not only powerless, but also make your already complex SQL statements even more confusing. Therefore, if you need to do some very complex operations, it is best to use XML to map statements.

How you choose to configure your mappings, and whether you think you should unify the mapping statement definitions, is entirely up to you and your team. In other words, never stick to one method, you can easily port and switch freely between annotation-based and XML-based statement mapping methods.

  1. XML Mapping File Definition Specification
    1. The name of the XML file should be consistent with the name of the Mapper interface file, and placed under the same package (same name as the package)
    2. The namespace attribute of the XML file is consistent with the fully qualified name of the Mapper interface (referring to the complete naming of a class or interface in Java, including the package name and class)
    3. The id of the SQL statement in the XML file is consistent with the method name in the Mapper interface

Guess you like

Origin blog.csdn.net/weixin_64939936/article/details/132110721