Spring整合MyBatis错误:Mapped Statements collection does not contain value for

 
MyBatis Mapper使用Spring注入:
applicationContext.xml
Xml代码 复制代码   收藏代码
  1. <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
  2.     <property name="mapperInterface" value="com.itsyc.fmpp.mappers.StudentMapper" />  
  3.     <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
  4. </bean>  

使用上面方式注入时需要注意下面问题:
- interface = com.itsyc.fmpp.mappers.StudentMapper.class
- mapper file = /com/itsyc/fmpp/mappers/StudentMapper.xml
- mapper namespace = com.itsyc.fmpp.mappers.StudentMapper
这三者必须相同否则会出现下面错误:
Stacktraces
java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.itsyc.fmpp.mappers.StudentMapper.getById

2011-04-22更新:
软件版本:

mybatis: 3.0.4
spring: 3.0.5
mybatis-spring: 1.0.0

更正:mapper file是可以和interface、namespace不同的(注意:interface和namespace必须相同),但这时需要进行配置。
方式一:
1.在MyBatis配置文件中添加mappers:
mybatis-config.xml
Xml代码 复制代码   收藏代码
  1. <mappers>  
  2.     <mapper resource="com/itsyc/fmpp/mappers/StudentMapper.xml" /> <!-- Mapper文件可以和interface不在相同路径下! -->  
  3. </mappers>  

2.使用configLocation,在Spring配置文件中添加MyBatis配置文件的引用:
applicationContext.xml
Xml代码 复制代码   收藏代码
  1. <!-- MyBatis配置 -->  
  2. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  3.     <property name="dataSource" ref="dataSource" />  
  4.     <property name="configLocation" value="classpath:mybatis-config.xml" />  
  5. </bean>  

方式二:
使用mapperLocations,MyBatis-Spring会自动地扫描该路径下的所有Mapper文件:
applicationContext.xml
Xml代码 复制代码   收藏代码
  1. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  2.     <property name="dataSource" ref="dataSource" />  
  3.     <property name="mapperLocations" value="classpath*:com/itsyc/fmpp/mappers/*Mapper.xml" />  
  4. </bean>  


具体请查看mybatis-spring的参考手册:
  • 11
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值