记录一下这个让人很懵逼的问题
Invalid bound statement (not found) 也就是mybatis找不到你映射的mapper
我这个项目是spring 整合 Mybatis
先贴一下配置文件
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
">
<!-- 声明用了annotation注解bean
开启组件扫描:它会到基础包下扫描@Service @Repository @Controller @Component这四种注解声明的bean,
扫描后会将这些bean交由Spring容器管理
-->
<context:component-scan base-package="com.imooc.shop"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"/>
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource">
<!-- 配置类型别名:采用包扫描的方式到基础包下扫描所有的类,作为MyBatis2能够转换的类型,多个包之间用;分隔 -->
<property name="typeAliasesPackage">
<value>
com.imooc.shop.bean
</value>
</property>
<!--<property name="mapperLocations" value="classpath:com.imooc.com.imooc.shop.com/*.xml"/>-->
</bean>
<!-- 配置数据访问接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:sqlSessionFactoryBeanName="sqlSessionFactory">
<!-- 配置数据访问接口:采用包扫描的方式到基础包下扫描所有的类,作为MyBatis2的数据访问接口,
并创建这些类的代理对象,创建出来后会把这些代理对象交给Spring容器管理,bean的id名默认为接口的类名前面首字母小写
多个包之间用;分隔 -->
<property name="basePackage" value="com.imooc.shop.repository"/>
</bean>
<!-- 配置DataSourceTransactionManager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/>
<!-- 开启annotation注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
配置本身是没有错的,错误的原因在路径上,上图体会下
解释一下,第一个框,三层文件夹是逐一创建的,下面的是一次创建的(也就是直接写了com.xxx.xxx),
再看图
也就是说在java目录下进行com.xxxx.xxx的穿件会成为3个潜逃的文件夹,而在resources文件夹下如此操作会得到一个名为
“com.xxxx.xxxx”
的文件夹
这也太TM坑了吧
这就是为什么mybatis一直拿不到mapper的原因
解决方法
当然是将这个名为“com.xxxx.xxxx”的文件夹改为3个互相嵌套的文件夹。然后在spring的xml文件中配置,我的项目里面是开启了自动扫描,核心配置代码如下
<!-- 声明用了annotation注解bean
开启组件扫描:它会到基础包下扫描@Service @Repository @Controller @Component这四种注解声明的bean,
扫描后会将这些bean交由Spring容器管理
-->
<context:component-scan base-package="com.imooc.shop"/>
顺手上传一个spring整合mybatis的配置
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
">
<!-- 声明用了annotation注解bean
开启组件扫描:它会到基础包下扫描@Service @Repository @Controller @Component这四种注解声明的bean,
扫描后会将这些bean交由Spring容器管理
-->
<context:component-scan base-package="com.imooc.shop"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"/>
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource">
<!-- 配置类型别名:采用包扫描的方式到基础包下扫描所有的类,作为MyBatis2能够转换的类型,多个包之间用;分隔 -->
<property name="typeAliasesPackage">
<value>
com.imooc.shop.bean
</value>
</property>
</bean>
<!-- 配置数据访问接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:sqlSessionFactoryBeanName="sqlSessionFactory">
<!-- 配置数据访问接口:采用包扫描的方式到基础包下扫描所有的类,作为MyBatis2的数据访问接口,
并创建这些类的代理对象,创建出来后会把这些代理对象交给Spring容器管理,bean的id名默认为接口的类名前面首字母小写
多个包之间用;分隔 -->
<property name="basePackage" value="com.imooc.shop.repository"/>
</bean>
<!-- 配置DataSourceTransactionManager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/>
<!-- 开启annotation注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>