org.apache.ibatis.binding.bindingexception: invalid bound statement (not found)
问题: springboot 集成 mybatis-plus 3.4.2 报错 org.apache.ibatis.binding.bindingexception: invalid bound statement (not found)
场景:
多模块的 spring boot 项目:
blog-admin 后台管理模块
blog-common 公共方法工具模块
blog-dao 实体类 和 dao层
blog-service service模块
application.yml
mybatis-plus:
mapper-locations: classpath*:com/wenhe/blogdao/mapper/mapping/**.xml
type-aliases-package: com.wenhe.blogdao.model
global-config:
db-config:
id-type: AUTO #0:数据库ID自增 1:用户输入id 2:全局唯一id(IdWorker) 3:全局唯一ID(uuid)
logic-delete-value: 0
logic-not-delete-value: 1
configuration:
map-underscore-to-camel-case: false
cache-enabled: true #配置的缓存的全局开关
lazyLoadingEnabled: true #延时加载的开关
multipleResultSetsEnabled: true #开启的话,延时加载一个属性时会加载该对象全部属性,否则按需加载属性
blog-dao 实体类 和 dao层 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.wenhe</groupId>
<artifactId>blog</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>com.wenhe</groupId>
<artifactId>blog-dao</artifactId>
<version>${blog.version}</version>
<name>blog-dao</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-generator -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.5</version>
</plugin>
</plugins>
</build>
</project>
mybatis plus 的 *.xml 的路径 在 src/main/java/com/wenhe/blogdao/mapper/mapping 下, 不在 resources 目录 下
然后写了个
ArticleMapper.java
package com.wenhe.blogdao.mapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wenhe.blogdao.model.Article;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.data.repository.query.Param;
import java.util.List;
import java.util.Map;
/**
* <p>
* 文章表 Mapper 接口
* </p>
*
* @author Erwin Feng
* @since 2021-06-16
*/
public interface ArticleMapper extends BaseMapper<Article> {
List<Map<String,Object>> selectListPage(@Param("page") Page<Article> page, @Param("article")Article article, @Param("keyword")String keyword);
}
ArticleMapper.xml
<?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="com.wenhe.blogdao.mapper.ArticleMapper">
<resultMap id="BaseResultMap" type="com.wenhe.blogdao.model.Article">
<id column="article_id" jdbcType="INTEGER" property="articleId" />
<result column="ac_id" jdbcType="INTEGER" property="acId" />
<result column="article_url" jdbcType="VARCHAR" property="articleUrl" />
<result column="article_show" jdbcType="BIT" property="articleShow" />
<result column="article_abstract" jdbcType="VARCHAR" property="articleAbstract" />
<result column="article_sort" jdbcType="TINYINT" property="articleSort" />
<result column="article_title" jdbcType="VARCHAR" property="articleTitle" />
<result column="article_time" jdbcType="INTEGER" property="articleTime" />
<result column="article_state" jdbcType="TINYINT" property="articleState" />
<result column="delete_time" jdbcType="TIMESTAMP" property="deleteTime" />
</resultMap>
<sql id="Base_Column_List">
article_id, ac_id, article_url, article_show, article_abstract, article_sort, article_title,
article_time, article_state, article_author, article_image, article_keyword, article_click,
article_type, article_modify_time, article_tag, article_comment_count, allow_comment,
article_origin, is_hot, is_best, is_new, type_id, delete_time
</sql>
<select id="selectListPage" resultType="map" parameterType="com.wenhe.blogdao.model.Article">
select * from bk_article a
<trim prefix="WHERE" prefixOverrides="AND">
<if test="article.articletitle != null and article.articletitle != ''">
AND <![CDATA[a.article_title like CONCAT('%',#{article.articletitle},'%') ]]>
</if>
<if test="article.isDelete != null">
AND (isNull(a.delete_time ) = false or a.delete_time > '0000-00-00 00:00:00')
</if>
</trim>
ORDER BY a.article_id desc
</select>
</mapper>
然后运行测试方法 后 报错 org.apache.ibatis.binding.bindingexception: invalid bound statement (not found)
解决方案
原因: ArticleMapper.xml 不在 resources 目录下 ( 在 src/main/java/com/wenhe/blogdao/mapper 下 mybatis-plus 里配置的是 mapper-locations: classpath*:com/wenhe/blogdao/mapper/mapping/**.xml ) 而 classpath 的默认路径是 resources 目录, 所以 找不到 ArticleMapper.xml 报错
解决:
<!--解决IDEA构建项目时,target/classes目录下不存在mapper.xml 映射文件-->
<!-- 第一种-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
<!-- 第二种-->
<build>
<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
网上搜的 解决方法 1
我的问题 不是一下情况造成的
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题,即在mybatis中dao接口与mapper配置文件在做映射绑定的时候出现问题,简单说,就是接口与xml要么是找不到,要么是找到了却匹配不到。
截图为网络中搜索到的常见原因:
照着修改之后,问题依旧存在。最终花费了好大的力气才找到自己代码问题的根源。dao接口与xml的文件名不一致。
接口名与接口文件名都是DepartmentDao, 而配置文件名为DeparmentDao.xml,费了很大的劲才看到两者名字查一个t字母。修改后就一切正常了。
这是一个很容易忽视的点,记住:接口名与Mybatis的映射文件名一定要一模一样。
(。・v・。)