Solve Invalid bound statement (not found) (Mybatis of Mapper binding problem)

I. Description of the problem

Use mybatis items can be run locally normal, but when using maven or packaged Jenkins appeared binding errors when deploying to a remote server, exception information is:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.yo.news.user.mapper.UserMapper.getUserByTelPwd

Second, problem analysis and solutions

First, given the abnormal message is not accurate, there are multiple reasons for the error will be thrown. mybatis this problem, usually corresponds to the definition of the xml file Mapper interface and not on a corresponding cause, then you need to check carefully compare package name, the xml namespace, interface method names correspond. I say this because the wrong name or wrong forget to add a method method name in the id attribute xml tag occurs before.

When this error occurs, follow the steps below to check the general will to solve the problem:
1: Check whether the package xml file where the name and the package name Mapper interface where one correspondence;
2: Check whether the xml namespace name and package xml files one by one corresponds;
3: check whether the method name corresponds;
4: removing xml file in Chinese comments;
5: free a space in the xml file and save or empty lines.

 

--------------------------------

but! ! ! ! ! ! And finally found the problem: maven project agreement must put the resources in the configuration file, xml file in the src directory is not the default compiler to target. Because I mapper.xml in the src directory, was a result of the occurrence of errors, the essence of the problem is, idea provisions of the classpath. In the eclipse, the resource file in the src folder, it can be found; but in the idea of ​​directly to the resource files in the src folder, if not set, can not be found.

The original Maven provides us with a consistent program directory configuration (source folder, resource folder, etc.), while automatically build the project, Maven will perform the operation (to compile the source file, copy the resource file) According to this configuration, Maven default source folders and folders resource configuration code is as follows:
<build>  
   <sourceDirectory>src/main/java</sourceDirectory >  
   <testSourceDirectory>src/test/java</testSourceDirectory >  
   <resources>  
       <resource>  
          <directory>src/main/resources</directory>  
       </resource>  
   </resources>  
   <testResources>  
       <testResource>  
          <directory>src/test/resources</directory>  
       </testResource>  
   </testResources>  
</build>

There are two solutions:

1, you can put xml file into the resource directory, when this project will be constructed loaded into the target.

2, in the pom.xml build file to add resource list of resources.

<! - This element describes the path a list of all project-related resources, such as files and projects related properties, these resources are included in the final package file. -> 
< Resources > 
    < Resource > 
        <-!    Describe a directory storage resources, the relative path Path POM -> 
        < Directory > the src / main / Java </ Directory > 
        < Includes > 
            < the include > ** / *. XML </ the include > 
        </ Includes > 
    </ Resource > 
</ Resources >

to sum up

If org.apache.ibatis.binding.BindingException appears: Invalid bound statement (not found) error, because the general definition of the corresponding Mapper interface and the xml file does not need to check the package name, namespace, the function names corresponding to whether the need more detailed contrast, one by one to perform the following steps:

1, package name check xml file is located and whether the interface corresponding package name one by one

2, namespace xml file to check whether the name and package xml file correspondence

3, corresponding to the function checks whether the name

4, remove the xml file in the Chinese comments

5, free to a space in the xml file and then save or blank lines

In addition, I met some special cases, I wasted a lot of time, the Internet has also led bound for other reasons can not be found:

1, Intellij Idea package name and directory name generating mechanism, a new packet ABCD, the directory structure is not a-> b-> c-> d, but generated "abcd" directory, leading to mybatis mapping errors, which is difficult to investigation.

2, xml file is defined as follows:

<select id="countMembers" parameterMap="java.util.Map" resultType="java.lang.Integer">

parameterMap wrong, should parameterType, this error will result mybatis all mapper binding errors are reported, very pit.

 

 

 

Guess you like

Origin www.cnblogs.com/itzyz/p/10954427.html