A-A+
Spring整合MyBatis配置中的问题
最近在学习Java高并发秒杀API,项目使用SpringMVC、Spring和MyBatis框架来实现,其中必然要学习Spring与MyBatis的整合。
在spring整合mybatis单元测试中碰到如下问题:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring/spring-dao.xml]:
Cannot resolve reference to bean 'classpath:mybatis-config.xml' while setting bean property 'configLocation';
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'classpath:mybatis-config.xml' is defined
大体理解了一下,好像是说在spring/spring-dao.xml中注册configLocation的地方有错。
- <!-- 3:配置SqlSessionFactory对象 -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <!-- 注入数据库连接池 -->
- <property name="dataSource" ref="dataSource" />
- <!-- 配置mybatis全局配置文件:mybatis-config.xml-->
- <property name="configLocation" ref="classpath:mybatis-config.xml" />
- <!-- 扫描entity包 使用别名 org.seckill.entity.Seckill=Seckill -->
- <property name="typeAliasesPackage" value="org.seckill.entity" />
- <!-- 扫描sql配置文件:mapper需要的xml配置文件 -->
- <property name="mapperLocations" value="classpath:mapper/*.xml" />
- </bean>
检查了一下代码,发现属性ref和value没有正确使用,正确的代码如下
- <!-- 3:配置SqlSessionFactory对象 -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <!-- 注入数据库连接池 -->
- <property name="dataSource" ref="dataSource" />
- <!-- 配置mybatis全局配置文件:mybatis-config.xml-->
- <property name="configLocation" value="classpath:mybatis-config.xml" />
- <!-- 扫描entity包 使用别名 org.seckill.entity.Seckill=Seckill -->
- <property name="typeAliasesPackage" value="org.seckill.entity" />
- <!-- 扫描sql配置文件:mapper需要的xml配置文件 -->
- <property name="mapperLocations" value="classpath:mapper/*.xml" />
- </bean>
学习新知识的过程中问题一定会出现,所以一定要学会解决问题的能力。