關於在整合mybatis、spring、springmvc時出現的Invalid bound statement (not found): 錯誤
今天在使用ssm框架進行ajax使用測試的時候,copy了教學視訊的程式碼,程式結構如下:
sping的關於mybatis的配置語句如下:
<!-- mapper的加強————————1.配置SqlSessionFactory --> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 設定資料來源 --> <property name="dataSource" ref="dataSource" /> <!-- 設定MyBatis核心配置檔案 --> <property name="configLocation" value="classpath:SqlMapConfig.xml" /> </bean> <!-- ———————————————————2.配置Mapper掃描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 設定Mapper掃描包 --> <property name="basePackage" value="it.laobing.mapper" /> </bean>
這時執行專案,出現了Invalid bound statement (not found): 錯誤,具體錯誤資訊如下:
嚴重: Servlet.service() for servlet [SpringMvcController] in context with path [] threw exception [Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): it.laobing.mapper.CityMapper.selectCityName] with root cause
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): it.laobing.mapper.CityMapper.selectCityName
解決方式一:
通過觀察,很明顯的發現,Invalid bound statement (not found)沒有找到的原因是mapple.xml配置檔案被從mapper包中移出來了,然而MapperScannerConfigurer類掃描的包仍舊是原來的位置,這樣,CityMappers.xml就無法掃描到了,程式就會報錯,解決辦法是,要麼將CityMappers.xml放回到定義好的掃描包下,要麼在SqlMapConfig.xml中,調價mappers語句,即
<mappers>
<mapper resource="CityMappers.xml"/>
</mappers>
如此,問題就解決了
解決方式二:
在pom中新增如下語句:
-
<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>