Met a problem today is the interface and the corresponding mybatis mapper file location is different, caused by the operation will be different, to find on the Internet for a long time, finally found the method here is simple analytic: we know that in a typical maven project directory structure are: SRC /main/ Java and SRC /main/resources, the former is used to store Java source code, and the latter is used to store resource files such as configuration files. Interfaces and mapper files in Mybatis need not be placed in the same package. If they are put together for automatic scanning of Mybatis, the name of the Java interface must be the same as the name of the mapper file, otherwise an exception will be reported. Since Mybatis will automatically resolve the corresponding interface and the corresponding configuration file, there is no need to configure the location of mapper file. 1: Interfaces and files are packaged in the same package as follows: By default, maven packages only the source code for the SRC /main/ Java directory and no other files. SRC /main/ Java/mapper will not be packaged into the final JAR folder, nor will it be exported to the target folder, because the unit test is executed in/ target /test-classes. So it won’t work when you test it. In order to implement the maven default environment packaging, Mybatis interface and mapper file in the same package, you can put the interface file in a SRC /main/ Java package, and create the same package in SRC /main/resources directory, this is a convention over configuration way. This will merge files from SRC /main/ Java and SRC /main/ Resources into the same package when Maven is packaged. In the default Maven packaging environment, do not put the interface file and mapper file in SRC /main/ Java, so that the mapper file will not be packaged
SRC /main/ Java and SRC /main/resources have the same package name and the same file name.
I found that the package was already in the same bag. SRC /main/ Java: SRC /main/ Java:
The packed files are as follows:
Mapper file is not found after compilation and packaging, so at this time both testing and formal execution will fail!! If you do not want to place the interface and mapper files in SRC /main/ Java and SRC /main/resources respectively, you need to specify that the Maven package includes XML files at build time. The configuration is as follows: [Java] view plain copy SRC /main/ Java **/*. 2: The interface and file are not in the same package. If the interface and mapper file are not in the same package, automatic scanning and parsing cannot be performed. You need to configure the interface and file respectively. 2.1 XML Configuration Mode 2.1.1 Not Using Spring The configuration file of Mybatis is as follows: [Java] View plain Copy
// …