本文主要是介绍关于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
配置下的basePackage
和mapperLocations
下的路径以及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&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
的包,还有不清楚basePackage
和mapperLocations
的具体作用。
以下是搜集问题中遇到的解决方案
作者链接🔗: 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'错误问题全收录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!