原文地址:http://blog.csdn.net/u011731544/article/details/78986698
06-Jan-2018 09:43:27.634 SEVERE [http-nio-8080-exec-6] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [springMVC] in context with path [] threw exception [Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.ssm.blog.dao.BlogDAO.getBlogList] with root cause
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.ssm.blog.dao.BlogDAO.getBlogList
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:223)
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:48
Spring和mybatis整合的时候出现上面的错误。出现这种错误的原因可能有以下几点:
1.使用spring生成代理接口实现类需要扫描dao接口所在的包:
<!--扫描所有mybatis的dao接口,生成代理实现类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.blog.dao"/>
</bean>
并且mapper的namespace要是接口的全路径,并且mapper中的语句的id要和接口中的方法名一致,
接口:
public interface BlogDAO {
List<Blog> getBlogList(BlogCriteria criteria);
}
mapper:
<mapper namespace="com.ssm.blog.dao.BlogDAO">
<select id="getBlogList" parameterType="BlogCriteria" resultMap="blogMap">
select B.ID,B.TITLE,B.USER_ID,B.CREATED_DATE,B.SUMMARY,B.COVER,B.CATEGORY_ID,B.VIEW_COUNT,B.REPLY_COUNT,
A.NICKNAME,C.NAME CATEGORY_NAME
from BLOG B
inner join ADMIN_USER A ON B.USER_ID=A.ID
left join CATEGORY C ON B.CATEGORY_ID=C.ID
</select>
检查一下mapper的namespace和语句的id是不是与接口相对应,
2. maven工程中没有配置resource
maven工程中默认只读取resources文件夹下的xml和properties,如果我们把mybatis的mapper放到java文件夹下,将不会读取。需要配置pom.xml:
<build>
<finalName>ssm-blog-admin</finalName>
<!--加上这段,否则mapper找不到-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
3. 没有扫描mapper,spring管理sessionFactory的时候需要加载mapper,检查一下有没有配置mapperLocations,还有文件路径前加没加classpath:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--加载mapper,加classpath-->
<property name="mapperLocations" value="classpath:com/ssm/blog/dao/mapper/*.xml"/>
<property name="typeAliasesPackage" value="com.ssm.blog.pojo"/>
</bean>
4. 重新编译。如果以上原因都没有问题。请clean一下工程,如果用的是idea IntelliJ,把target文件夹下classes清空,再把target下与工程同名那个文件夹删掉,强制重新编译