Invalid bound statement (not found)

看了好多博客(不过很多都是复制克隆),最后才解决了问题。如果对你有帮助的话,给我一个推荐和点赞哦。

问题描述

使用springboot框架和mybatis-plus的代码自动生成器,在进行多表连接查询的时候,需要用到xml,但是出现了如下报错

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.dj.dormitory.mapper.MenuMapper.getMenuByType

原因分析

读取不到xml文件

解决问题

看了很多的博客,最后终于解决了问题。按了下面的步骤一步步来。

@MapperScan

在配置类或者主启动类加上@MapperScan("com.dj.dormitory.mapper"),注意:路径需要写到mapper层

@MapperScan是扫描mapper,如果你没有加这个注解就无法自动扫描到mapper,从而导致找不到。

如果是spring项目的话,就需要在配置文件中配置

	<!-- 配置sqlsessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
        <property name="dataSource" ref="dataSource"></property>
        <!--  自动扫描mapper。xml文件,-->
        <property name="mapperLocations" value="classpath:com/gss/dao/mappers/*.xml" />
    </bean>

资源过滤问题

当你的项目运行之后,会生成一个target文件,你需要去查看一下是或否存在xml文件

image-20210131093038108

如果不存在的话,那就是存在着资源过滤的问题。在pom.xml中添加。再次运行成功之后检查生成

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

检查mapper文件

这一点是参考了网上的一篇博客,不过我一步步测试,对我还是没有帮助

<?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.dj.dormitory.mapper.MenuMapper">
    <select id="getMenuByType" resultType="com.dj.dormitory.entity.Menu" parameterType="int">
        select tb_menu.id,title,icon,href,target,parent_id from tb_menu
        join tb_role_menu on tb_menu.id=tb_role_menu.menu_id
        where role_id = #{type}
    </select>
    
</mapper>
public interface MenuMapper extends BaseMapper<Menu> {

    List<Menu> getMenuByType(Integer type);

}
  • 检查xml中的namespace是否正确
  • 检查自己编写的代码中是否编写了xml文件,即xml是否存在
  • 在xml中配置好正确的idresultType(或resultMap),parameterType
  • 玄学做法:在xml删除一个空行,再运行项目

配置mapper.xml的引用路径

  • 如果引用mybatis-plus-boot-starter 依赖,需要配置 mybatis-plus.mapper-locations
  • 如果引用mybatis-plus 依赖,需要配置 mybatis.mapper-locations

根据自己的项目情况,选其中一种情况,进行配置。

注意:路径换成自己的项目路径

引入mybatis-plus-boot-starter

<dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus-boot-starter</artifactId>
     <version>3.1.0</version>
 </dependency>
mybatis-plus:
  mapper-locations: classpath:com/dj/dormitory/mapper/xml/*.xml

引用mybatis-plus包

<dependency>
   <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>3.1.0</version>
</dependency>
mybatis:
	mapper-locations: classpath:com/dj/dormitory/mapper/xml/*.xml

参考链接:

https://www.cnblogs.com/tv151579/p/11565509.html

https://blog.csdn.net/u013234928/article/details/94060733

posted @ 2021-01-31 09:53  DJ同学  阅读(325)  评论(0编辑  收藏  举报