关于Invalid bound statement和Error creating bean with name 'xxx'错误问题全收录

本文主要是介绍关于Invalid bound statement和Error creating bean with name 'xxx'错误问题全收录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

关于Invalid bound statement和Error creating bean with name 'xxx’错误问题

在我使用Spring+Mybatis整合的时候出现的问题。
首先Invalid bound statement报错是因为mapper.xml文件没找到的原因,creating bean with name 'xxx'报错是因为注解包未扫描到,首先要去检查mapper.xml配置下的basePackagemapperLocations下的路径以及namespace

以下是我的代码:

  • 项目结构:
    项目结构

  • DepartmentDao接口
    在这里插入图片描述

  • mapper.xml配置

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.oa.dao.dao.DepartmentDao"><resultMap id="department" type="Department"><id property="sn" column="sn" javaType="String" /><result property="name" column="name" javaType="String"/><result property="address" column="address" javaType="String" /></resultMap><insert id="insert" parameterType="Department">insert into department values(#{sn},#{name},#{adress},)  <!--是数据库字段名,不是entity属性名--></insert><update id="update" parameterType="Department">update department set name=#{name},address=#{address} where sn=#{sn}</update><delete id="delete" parameterType="String">delete from department where sn=#{sn}</delete><select id="select" parameterType="String" resultMap="department">select * from department where  sn=#{sn}</select><!--resultMap涉及到接口字段和数据库的字段,所以需要另写一个<result>将接口字段与数据库字段匹配--><select id="selectAll" resultMap="department">select * from department</select></mapper>
  • Spring.xml配置文件

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.imooc.oa.dao"></context:component-scan><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/oa?useUnicode=true&amp;characterEncoding=utf-8"></property><property name="username" value="root"></property><property name="password" value="root"></property></bean><bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="typeAliasesPackage" value="com.imooc.oa.dao.entity"></property><property name="mapperLocations" value="classpath:mapper/*.xml"></property></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sessionFactory" ></property><property name="basePackage" value="com.imooc.oa.dao.dao" ></property></bean></beans>

注意:

  • MapperScannerConfigurer下的basePackage属性使用路径是接口路径也就是我上面DepartmentDao接口的路径。
  • typeAliasesPackage是用于entity下给实体类取别名的,包名为实体类包名。
  • mapperLocations 是扫描mapper.xml的配置,扫描的也就是我上面resources
    mapper文件夹的配置
ps:我出错的地方在于没有写mapperLocations导致找不到mapper.xml的包,还有不清楚basePackagemapperLocations的具体作用。

以下是搜集问题中遇到的解决方案

作者链接🔗: https://www.jianshu.com/p/a9516bcd3cb0

在实际项目,搭建mybatis会爆出 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): 这个错误非常的头疼,如图1,不知道为什么mybatis就是找不到对应的xml文件。博主整理了三种可能的情况,三种情况下肯定有一种能帮助到你。

  • 情况一:mapper.xml没有按照传统的maven架构进行放置
    传统的maven架构目录可以参考博主的另外一个博文:https://www.jianshu.com/p/477ad2e14150
    如果我们的mapper.xml文件没有放置到src-main-resources下面,是不会被maven build plugin给默认扫描到的。此时需要修改启动的模块的pom文件,在build标签里面加入:
<build><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include><include>**/*.properties</include></includes><filtering>true</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/*.xml</include><include>**/*.properties</include></includes><filtering>true</filtering></resource></resources>
</build>

并推荐在Project Structure -> Modules中将你存放mapper的包给标注成resources

  • 情况二:mybatis的配置信息出错
    博主给出所维护的项目的配置信息供读者参考:
       mybatis-plus.mapper-locations=classpath*:/mapper/**Mapper.xmlmybatis-plus.typeAliasesPackage=com.yuxun.**.entitymybatis-plus.defaultStatementTimeout=120mybatis-plus.config-locations=classpath:config/mybatis-config.xml

其中mybatis-plus.mapper-locations是写mapper编译过后的位置和名称,可以参见图2。

  • 情况三:idea的编译问题
    该种情况非常的莫名其妙,可能是内存爆满的原因导致的,idea有的时候没有编译生成相应的xml。在idea的target->classes下面没有找到相应的存在xml的文件夹,该文件夹里面有没有对应的mapper(实体名).xml文件,如图2。此时就算你采用重启idea,Invalidate caches等方式都不管用。最正确的做法是重新编译工程,点击导航栏build => rebuild project

图2,
在这里插入图片描述

编译后找不到对应的UserMapper.xml

ps:第三种情况常常见于,本来项目好好的可以扫描到mybatis的配置的,但是加了一个方法或者某一次run起来就突然报这个错误的情况。

这篇关于关于Invalid bound statement和Error creating bean with name 'xxx'错误问题全收录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/544724

相关文章

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

Debian 13升级后网络转发等功能异常怎么办? 并非错误而是管理机制变更

《Debian13升级后网络转发等功能异常怎么办?并非错误而是管理机制变更》很多朋友反馈,更新到Debian13后网络转发等功能异常,这并非BUG而是Debian13Trixie调整... 日前 Debian 13 Trixie 发布后已经有众多网友升级到新版本,只不过升级后发现某些功能存在异常,例如网络转

GSON框架下将百度天气JSON数据转JavaBean

《GSON框架下将百度天气JSON数据转JavaBean》这篇文章主要为大家详细介绍了如何在GSON框架下实现将百度天气JSON数据转JavaBean,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言一、百度天气jsON1、请求参数2、返回参数3、属性映射二、GSON属性映射实战1、类对象映

Web服务器-Nginx-高并发问题

《Web服务器-Nginx-高并发问题》Nginx通过事件驱动、I/O多路复用和异步非阻塞技术高效处理高并发,结合动静分离和限流策略,提升性能与稳定性... 目录前言一、架构1. 原生多进程架构2. 事件驱动模型3. IO多路复用4. 异步非阻塞 I/O5. Nginx高并发配置实战二、动静分离1. 职责2

解决升级JDK报错:module java.base does not“opens java.lang.reflect“to unnamed module问题

《解决升级JDK报错:modulejava.basedoesnot“opensjava.lang.reflect“tounnamedmodule问题》SpringBoot启动错误源于Jav... 目录问题描述原因分析解决方案总结问题描述启动sprintboot时报以下错误原因分析编程异js常是由Ja

MySQL 表空却 ibd 文件过大的问题及解决方法

《MySQL表空却ibd文件过大的问题及解决方法》本文给大家介绍MySQL表空却ibd文件过大的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录一、问题背景:表空却 “吃满” 磁盘的怪事二、问题复现:一步步编程还原异常场景1. 准备测试源表与数据

Spring创建Bean的八种主要方式详解

《Spring创建Bean的八种主要方式详解》Spring(尤其是SpringBoot)提供了多种方式来让容器创建和管理Bean,@Component、@Configuration+@Bean、@En... 目录引言一、Spring 创建 Bean 的 8 种主要方式1. @Component 及其衍生注解

解决Nginx启动报错Job for nginx.service failed because the control process exited with error code问题

《解决Nginx启动报错Jobfornginx.servicefailedbecausethecontrolprocessexitedwitherrorcode问题》Nginx启... 目录一、报错如下二、解决原因三、解决方式总结一、报错如下Job for nginx.service failed bec