版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40990836/article/details/83501371
Mybatis中 Mapper方法出现 invalid bound statement(not found)
在网上百度了很久大部分都是说是
- 检查xml的namespace是否和dao层对应
- 检查定义的sql语句id是否和 dao层方法名是否一直
- 是否有配置正确的返回参数,比如说UserDao的返回参数为List,而select 元素没有正确配置ResultMap或者只是配置了ResultType
- 以上没问题就修改一下文件然后保存什么的。
- 查看mybatisConfig的配置路径是否正确
反正按照上面方法找了半天没什么乱用
后来看到target文件中并没有把xml文件加载到对应的文件夹中。
后来查到因为使用的聚合工程,需要在pom文件中配置build什么的。直接贴上代码
<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<!--<include>**/*.xml</include>-->
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
你以为事情这么简单? 加上了之后,虽然这个xml加载了。打开浏览器测试!
HTTP Status 500 – Internal Server Error
Type Exception Report
Message Request processing failed; nested exception is org.springframework.dao.TransientDataAccessResourceException:
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
后来仔细检查了application-spring.xml文件,发现是因为我配置sessionFactory的原因没有扫描mapper.xml文件的包。贴上代码。找了一天的bug 原来是因为自己太蠢了!
<!-- 配置sqlsessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
<property name="dataSource" ref="dataSource"></property>
<!-- 自动扫描mapper。xml文件,-->
<property name="mapperLocations" value="classpath:com/gss/dao/mappers/*.xml" />
</bean>
看来学习的路途不能只顾着复制粘贴咯