Springboot integrated mybatisplus problem handling

foreword

Friends who are new to mybatisPlus may encounter various problems, especially the injection of xml files and classes of mybatis. Let's take a look at the common problems below.

Multiple implementations of an interface

The error message is as follows:

Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

This is the specific problem that tells you, maybe your injection interface is in conflict.

The annotated interface class, when the IOC container is automatically assembled when the project starts, its specific implementation class cannot be found, or the problem caused by multiple implementation classes of the interface is found.

solution

First, check whether your injected class is normal, and add one of the two annotations such as @Component or @Repository.
Second, check to see if your class names are consistent, and if so, change a class name.

Invalid bound statement (not found)

This problem is actually caused by our mapper file and xml not completing the correct mapping. Next, let's take a look at the solution

solution

1. The namespace in mapper.xml is inconsistent with the mapper file we actually created

This problem is actually very easy to solve. We only need to carefully compare whether the configuration in the mapper file and the namespace in xml are consistent.

2. The method name called in the mapper interface is inconsistent with the id label in mapper.xml

This is also very simple, just rely on seriousness and carefulness, and compare to see if they are consistent. If they are not consistent, just change to a consistent name.

3. This is disgusting. There is no xml in our target folder, only class files
insert image description here

This is also relatively easy to solve, just add the following content under the build tag in our pom file

 <resources>
            <!-- 通用资源文件 -->
            <resource><directory>src/main/resources</directory><includes><include>**/*.*</include></includes></resource>
            <resource>
                <directory>src/main/java</directory>
                <includes><include>**/*.xml</include></includes><!-- maven可以将mapper.xml进行打包处理,否则仅对java文件处理 -->
            </resource>
            <!-- 放置通用配置yml文件, 开发时仅配置一套参数即可。   实际生产环境下应在每个项目下 与jar同级目录下新建application.yml覆写对应参数。  -->
            <resource>
                <directory>../conf/devCommons</directory>
                <includes><include>**/*.yml</include></includes>
            </resource>
        </resources>

Above, we can first see the <includes>< include>**/ .xml</include></includes> this code, which is the mark that lets us package the .xml, so that it will appear under the target The xml file we need is gone.

For other resources, we can clearly understand his role through the above comments

Place the general configuration yml file, and only configure a set of parameters during development. In the actual production environment, a new application.yml should be created in each project under the same directory as the jar to overwrite the corresponding parameters. This is also our common function configuration.

Summarize

In fact, as long as we take a closer look at the above problems, some mistakes will be avoided, and the problem of no xml in the final compiled code can be easily solved. There are many similar solutions on the Internet. It can be solved by comparison!

Today’s analysis of the pitfalls encountered in the use of mybatis-Plus is here, thank you for reading! ! !

Welcome everyone to click on the card below to pay attention to "coder trainees"

Guess you like

Origin blog.csdn.net/ybb_ymm/article/details/131479434