关于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 NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

解决IDEA报错:编码GBK的不可映射字符问题

《解决IDEA报错:编码GBK的不可映射字符问题》:本文主要介绍解决IDEA报错:编码GBK的不可映射字符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录IDEA报错:编码GBK的不可映射字符终端软件问题描述原因分析解决方案方法1:将命令改为方法2:右下jav

MyBatis模糊查询报错:ParserException: not supported.pos 问题解决

《MyBatis模糊查询报错:ParserException:notsupported.pos问题解决》本文主要介绍了MyBatis模糊查询报错:ParserException:notsuppo... 目录问题描述问题根源错误SQL解析逻辑深层原因分析三种解决方案方案一:使用CONCAT函数(推荐)方案二:

Redis 热 key 和大 key 问题小结

《Redis热key和大key问题小结》:本文主要介绍Redis热key和大key问题小结,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、什么是 Redis 热 key?热 key(Hot Key)定义: 热 key 常见表现:热 key 的风险:二、

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖

Spring 中的循环引用问题解决方法

《Spring中的循环引用问题解决方法》:本文主要介绍Spring中的循环引用问题解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录什么是循环引用?循环依赖三级缓存解决循环依赖二级缓存三级缓存本章来聊聊Spring 中的循环引用问题该如何解决。这里聊

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基