Here we can refer to the MyBatis official website (mybatis.org/mybatis-3/) query XML header preparation specification, directly copy and paste for reference.
The specific steps are as follows:
- According to the resources/application directory. The properties of mybatis. Mapper – locations = classpath: / mapper/.xml configuration path to create the file.
- Create an XML file in the mapper. Brand directory:
- Query XML mapping header file format via MyBatis official (mybatis.org/mybatis-3/) :
Continue to find the mapping file format:
Make sure the required mapping header specification is copied directly to our own XML file for use:
4. Configure the map:
- Require that the namespace be consistent with the full class name of the DAO
- ResultType is required to be consistent with the full class name in the POJO:
- The primary ID is required to be the same as the method name in the DAO package:
Summary: Simple SQL statements can be directly used to describe the use of annotations, complex SQL is recommended to write in the XML mapping file, the structure is more clear, live to learn and use!
Note: 1. Check the resources/application. The properties of mybatis configuration path is consistent with the create XML mapping file path; Check if the mapping file namespace is the same as the full class name of the DAO interface (note the space). Check if the name of the method accessed has the corresponding element ID in the mapping file (note spacing, case, etc.) 4. Check whether a ResultType or ResultMap attribute is defined in the mapping element