A-A+

Spring整合MyBatis配置中的问题

2016年06月21日 Java 暂无评论 阅读 2,273 views 次

最近在学习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的地方有错。

  1. <!-- 3:配置SqlSessionFactory对象 -->
  2.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  3.         <!-- 注入数据库连接池 -->
  4.         <property name="dataSource" ref="dataSource" />
  5.         <!-- 配置mybatis全局配置文件:mybatis-config.xml-->
  6.         <property name="configLocation" ref="classpath:mybatis-config.xml" />
  7.         <!-- 扫描entity包 使用别名 org.seckill.entity.Seckill=Seckill  -->
  8.         <property name="typeAliasesPackage" value="org.seckill.entity" />
  9.         <!-- 扫描sql配置文件:mapper需要的xml配置文件 -->
  10.         <property name="mapperLocations" value="classpath:mapper/*.xml" />
  11.     </bean>

检查了一下代码,发现属性ref和value没有正确使用,正确的代码如下

  1. <!-- 3:配置SqlSessionFactory对象 -->
  2.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  3.         <!-- 注入数据库连接池 -->
  4.         <property name="dataSource" ref="dataSource" />
  5.         <!-- 配置mybatis全局配置文件:mybatis-config.xml-->
  6.         <property name="configLocation" value="classpath:mybatis-config.xml" />
  7.         <!-- 扫描entity包 使用别名 org.seckill.entity.Seckill=Seckill  -->
  8.         <property name="typeAliasesPackage" value="org.seckill.entity" />
  9.         <!-- 扫描sql配置文件:mapper需要的xml配置文件 -->
  10.         <property name="mapperLocations" value="classpath:mapper/*.xml" />
  11.     </bean>

学习新知识的过程中问题一定会出现,所以一定要学会解决问题的能力。

给我留言

*

Copyright © If Coding 保留所有权利.   Theme  Ality   

用户登录