Mybatis: Error building SqlSession. The error may exist in UserMapper.xml

created at 12-20-2021 views: 25

Project scene

Cannot find the xml configuration file when maven is running

reason

The XML file was created under the java package, but the configuration file under the `resource` file was found when maven was running, and the java package could not be imported.

solution

Understand the target directory

directory

In a maven project, a module mainly has two directories `src` and `target`, and a `pom.xml` configuration file

  • src: This directory mainly stores the source code files of the project
  • target: This directory is a directory generated after the project is compiled, and it mainly stores the compiled .class files

1. When building a Maven project, if there is no special configuration, Maven will find and process various types of files according to the standard directory structure

src/main/java和src/test/java 

All *.java files in these two directories will be compiled in the stages of comile and test-comiple respectively, and the compilation results are placed in the directories of target/classes and targe/test-classes respectively. But other files in these two directories will be ignored.

 src/main/resouces和src/test/resources

The files in these two directories will also be copied to the target/classes and target/test-classes directories.

target/classes

By default, the packaging plugin will put all the contents in this directory into the jar package or war package.

Under normal circumstances, the resource files we use (various xml, properites, xsd files, etc.) are placed under src/main/resources. When using maven to package, maven can store these resource files Packaged into the corresponding jar or war.

Sometimes, such as the mapper.xml file of mybatis, we are used to putting it together with `Mapper.java`, both under src/main/java, so when using maven to package, you need to modify pom.xml File, to pack the mapper.xml file together into the jar or war, otherwise, these files will not be packaged. (maven thinks that src/main/java is just the source code path of java).

A simple example

example

In this project, I put the `UserMapper.xml` file in the `java/` directory. Therefore, this configuration file, after compilation, will not be placed under the target directory, which is an error. The reason to the xml file.

Solution one

Put the UserMapper.xml file in the test-classes of the target/ directory

UserMapper.xml

Here, the path registered by the mapper should be changed to a directory at the same level as the mybatis-config.xml directory

resource

Solution two:

Put the UserMapper.xml file in the resource directory, after compiling, the UseMapper file will be automatically added under the target directory

Solution two

Solution three:

While keeping the original xml configuration file directory, add the UserMapper.xml configuration file to the target directory by changing the pom.xml configuration file

pom.xml

Configure the resource of pom.xml to package the xml into the mapper directory

<build>
       <!-- Resource Directory -->
        <resources>
            <resource>
                <!-- Set the main resource directory -->
                <directory>src/main/java</directory>

                <!-- maven default life cycle, the resources goal of the maven-resources-plugin plug-in is executed in the process-resources phase when processing resource files under the main resource category, only the resource types contained in the following configuration are processed -->
<includes>
<include>**/*.xml</include>
</includes>

                <!-- maven default life cycle, the resources goal of the maven-resources-plugin plug-in is executed in the process-resources phase. When processing the resource files under the main resource category, the resource types included in the following configuration are not processed (reject the resources included in the following configuration) Type) -->
<excludes>
<exclude>**/*.yaml</exclude>
</excludes>

                <!-- maven default life cycle, in the process-resources phase, execute the resources goal of the maven-resources-plugin plug-in when processing resource files under the main resource directory, specify the processed resource file output directory, the default is ${build.outputDirectory} specified Directory -->
                <!--<targetPath>${build.outputDirectory}</targetPath> -->

                <!-- maven default life cycle, process-resources phase executes the resources goal of the maven-resources-plugin plug-in when processing resource files under the main resource directory, whether to enable resource filtering on the main resource directory -->
                <filtering>true</filtering>
            </resource>
        </resources>
  </build>
created at:12-20-2021
edited at: 12-20-2021: