mybatis报错:Could not find resource mapper/UserMapper.xml

Error 1: Could not find resource mapper/UserMapper.xml

Means: Cannot find UserMapper.xml.

The reason is: did not put the xml under the resource directory. Idea's maven project will not load the xml files under non-resource directories.

There are two solutions:
1. Putting xml in the resource directory is one way.
2. Add resource filtering in the pom.xml file and put it <dependencies></dependencies>outside.

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

Error 2: Byte 1 of a 1-byte UTF-8 sequence is invalid.

The prompt error may appear in UserMapper.xml.
Insert picture description here
This is the error message prompted.
Insert picture description here

Reason: Chinese comments are added in UserMapper.xml.

There are also two solutions:
1. Just remove the Chinese comment and run it.
2. If you don't want to remove the Chinese comment, change the first line encoding="UTF-8"to encoding="UTF8". It is to -go.

Note: There are also some places that are easier to forget and cause errors.

1. Forgot to add UserMapper.xml mapping in the mybatis-config.xml core configuration file, every .xml must be added.
Insert picture description here
2. The type is not complete after resulType, and it can be omitted only after naming the alias.
Insert picture description here

3. Explanation of words in mapper.xml

idIs the name of the method in the interface class.
namespaceNext is the interface class to be bound.
parameterTypeIs the parameter type.

Guess you like

Origin blog.csdn.net/qq_42524288/article/details/108560674