mybatis报错:Invalid bound statement (not found)

mybatis报错:Invalid bound statement (not found)
Insert picture description here

Grammatical errors

Java DAO layer interface

public void delete(@Param("id")String id);

Mapper.xml file corresponding to Java

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xxx.xxx.xxx.xxDao">
    <!-- 删除数据 -->
    <delete id="delete" parameterType="java.lang.String">
        DELETE FROM xxx WHERE id=#{id}
    </delete>
</mapper>

an examination:

  1. Whether the method name (delete) in the interface is consistent with the id="delete" in the xml file
  2. Whether the path in namespace="xxx.xxx.xxx.xxDao" in the xml file is consistent with the interface file path
  3. Whether parameterType and resultType are accurate; resultMap and resultType are not the same.

Compile Error

Locate the project path: under the error path in target\classes\, and look for the corresponding xml file.

  1. If there is no corresponding xml file, you need to add the following code in pom.xml:
<build>
    <resources>
         <resource>
             <directory>src/main/java</directory>
             <excludes>
                 <exclude>**/*.java</exclude>
             </excludes>
         </resource>
         <resource>
             <directory>src/main/resources</directory>
             <includes>
                 <include>**/*.*</include>
             </includes>
        </resource>
    </resources>
</build>

Delete the files in the classes folder, recompile, and the corresponding xml file will appear.

(2) If there is an xml file, open the xml file and check whether the error report part is consistent with the source file. If it is inconsistent, first clear the files in the classes folder and execute the command: mvn clean to clean up the content and recompile.

Configuration error

  1. The mybatis.mapperLocations in the configuration file should be configured correctly, as follows

mybatis.mapperLocations=classpath:mapper/xxx/*.xml

	@Primary
    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
    
    
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/xxx/*.xml"));
        sqlSessionFactoryBean.setTypeAliasesPackage("com.xxx.dao");
        return sqlSessionFactoryBean.getObject();
    }
  1. The scan package path must be configured correctly

@MapperScan(“com.xxx.dao”)

Guess you like

Origin blog.csdn.net/Dkangel/article/details/111153656