Error creating bean with name 与 invalid bound statement (not found) 错误解决方法

5 篇文章 0 订阅
3 篇文章 0 订阅

今天使用idea建立maven项目遇到一个很奇怪的bug

在使用MapperScannerConfigurer和SqlSessionFactoryBean对mapper进行配置时出现了bug

配置如下:

 <!-- 配置SqlSessionFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
<!--        <property name="configLocation" value="classpath:conf.xml" />-->
        <property name="mapperLocations" value="classpath:com/my/mapper/studentInfoMapper.xml"></property>
</bean>

<!-- 配置MapperScannerConfigurer,DAO接口所在包名,Spring会自动查找其下的类(相当于配了bean) -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <property name="basePackage" value="com.my.mapper" />
    </bean>

 bug如下:

org.springframework.context.support.AbstractApplicationContext.refresh Exception
 encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentInfoService': Unsatisfied dependency expressed through field 
'studentInfoMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
'studentInfoMapper' defined in file 
[D:\IDEA\MavenProject\mavenTest03\target\mavenTest03\WEB-
INF\classes\com\my\mapper\StudentInfoMapper.class]: Cannot resolve reference to bean 
'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
'sqlSessionFactory' defined in URL 
[file:/D:/IDEA/MavenProject/mavenTest03/target/mavenTest03/WEB-
INF/classes/applicationContext.xml]: Invocation of init method failed; nested exception is
 org.springframework.core.NestedIOException: Failed to parse mapping resource: 'class path 
resource [com/my/mapper/studentInfoMapper.xml]'; nested exception is 
java.io.FileNotFoundException: class path resource [com/my/mapper/studentInfoMapper.xml]
 cannot be opened because it does not exist

在接口名和mapper文件名相同的情况下,出现这样的错误很奇怪,经过排查,发现在idea中配置maven项目时,resources路径并没有自动装配,所以会导致mapper.xml文件失效。

解决方法如下:

在pom.xml中进行如下配置:

<build>
 <!-- 如果不进行此配置,mybatis的mapper.xml文件都会被漏掉。 -->
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>

再者就是配置文件的准确性(配置属性“mapperLocations”时路径前面必须加上classpath或者classpath*):

 <!-- 配置SqlSessionFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
<!--        <property name="configLocation" value="classpath:conf.xml" />-->
        <property name="mapperLocations" value="classpath:com/my/mapper/studentInfoMapper.xml"></property>
</bean>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当出现"Invalid bound statement (not found)"错误时,可能由以下几种原因导致: 1. mapper方法名和mapper.xml中的id不匹配。请确保mapper接口中的方法名与mapper.xml文件中的id名保持一致。检查方法名的拼写和大小写是否正确。 2. 在搭建项目时使用了MyBatis Plus,但是没有正确配置mapper接口和mapper.xml的对应关系。在使用MyBatis Plus时,需要在mapper接口上添加`@Mapper`注解,并且在application.yml或application.properties配置文件中正确指定mapper.xml的位置。如果没有正确配置对应关系,会导致"Invalid bound statement (not found)"错误。请检查相关配置是否正确。 3. 可能是IDE或编辑器的插件问题。有些IDE或编辑器提供了MyBatis相关的插件,这些插件可以帮助我们更方便地编写mapper接口和mapper.xml。但是如果插件配置不正确或版本不兼容,可能会导致"Invalid bound statement (not found)"错误。请检查插件的配置和版本是否正确,并尝试重新安装或更新插件。 综上所述,要解决"Invalid bound statement (not found)"错误,需要检查mapper方法名和mapper.xml中的id是否匹配,确保MyBatis Plus的配置正确,以及检查IDE或编辑器的插件配置和版本。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Invalid bound statement (not found)的原因以及解决方法](https://blog.csdn.net/axbhealj/article/details/130331164)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [mybatisplus报Invalid bound statement (not found)错误解决方法](https://download.csdn.net/download/weixin_38625442/12821082)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值