今天在使用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>