关于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

相关文章

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red

nginx 负载均衡配置及如何解决重复登录问题

《nginx负载均衡配置及如何解决重复登录问题》文章详解Nginx源码安装与Docker部署,介绍四层/七层代理区别及负载均衡策略,通过ip_hash解决重复登录问题,对nginx负载均衡配置及如何... 目录一:源码安装:1.配置编译参数2.编译3.编译安装 二,四层代理和七层代理区别1.二者混合使用举例

浅析Spring如何控制Bean的加载顺序

《浅析Spring如何控制Bean的加载顺序》在大多数情况下,我们不需要手动控制Bean的加载顺序,因为Spring的IoC容器足够智能,但在某些特殊场景下,这种隐式的依赖关系可能不存在,下面我们就来... 目录核心原则:依赖驱动加载手动控制 Bean 加载顺序的方法方法 1:使用@DependsOn(最直

linux解压缩 xxx.jar文件进行内部操作过程

《linux解压缩xxx.jar文件进行内部操作过程》:本文主要介绍linux解压缩xxx.jar文件进行内部操作,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、解压文件二、压缩文件总结一、解压文件1、把 xxx.jar 文件放在服务器上,并进入当前目录#

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操

Redis出现中文乱码的问题及解决

《Redis出现中文乱码的问题及解决》:本文主要介绍Redis出现中文乱码的问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 问题的产生2China编程. 问题的解决redihttp://www.chinasem.cns数据进制问题的解决中文乱码问题解决总结

全面解析MySQL索引长度限制问题与解决方案

《全面解析MySQL索引长度限制问题与解决方案》MySQL对索引长度设限是为了保持高效的数据检索性能,这个限制不是MySQL的缺陷,而是数据库设计中的权衡结果,下面我们就来看看如何解决这一问题吧... 目录引言:为什么会有索引键长度问题?一、问题根源深度解析mysql索引长度限制原理实际场景示例二、五大解决

Springboot如何正确使用AOP问题

《Springboot如何正确使用AOP问题》:本文主要介绍Springboot如何正确使用AOP问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录​一、AOP概念二、切点表达式​execution表达式案例三、AOP通知四、springboot中使用AOP导出