MyBatis Mapper使用Spring注入:
applicationContext.xml
- <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
-
<property name="mapperInterface" value="com.itsyc.fmpp.mappers.StudentMapper" />
-
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
-
</bean>
<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.itsyc.fmpp.mappers.StudentMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</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
软件版本:
mybatis: 3.1.1
spring: 3.0.5
mybatis-spring: 1.1.1
更正:mapper file是可以和interface、namespace不同的(注意:interface和namespace必须相同),但这时需要进行配置。
方式一:
1.在MyBatis配置文件中添加mappers:
mybatis-config.xml
- <mappers>
-
<mapper resource="com/itsyc/fmpp/mappers/StudentMapper.xml" />
-
</mappers>
<mappers>
<mapper resource="com/itsyc/fmpp/mappers/StudentMapper.xml" /> <!-- Mapper文件可以和interface不在相同路径下! -->
</mappers>
2.使用configLocation,在Spring配置文件中添加MyBatis配置文件的引用:
applicationContext.xml
-
-
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
-
<property name="dataSource" ref="dataSource" />
-
<property name="configLocation" value="classpath:mybatis-config.xml" />
-
</bean>
<!-- MyBatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
方式二:
使用mapperLocations,MyBatis-Spring会自动地扫描该路径下的所有Mapper文件:
applicationContext.xml
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
-
<property name="dataSource" ref="dataSource" />
-
<property name="mapperLocations" value="classpath*:com/itsyc/fmpp/mappers/*Mapper.xml" />
-
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:com/itsyc/fmpp/mappers/*Mapper.xml" />
</bean>
本文转载自:http://ifrenzyc.iteye.com/blog/1011593;再次对高人表示感谢。
分享到:
相关推荐
spring整合mybatis时需要用到的配置文件
IDEA: spring+mybatis+springMVC SSM框架(二) spring 整合 Mybatis 以及分页插件 PageHelper
spring整合Mybatis纯净项目,基础搭建,下载就用,包含整套源码,jar包
Spring和MyBatis的整合是Java开发中常见的架构模式,它提供了一种高效、灵活的方式来管理数据库操作和业务逻辑层。本文深入探讨了Spring和MyBatis整合的关键原理和实践要点,帮助架构师和开发者有效地利用这两个强大...
一个完整而简单SSM项目,包含SQL脚本、代码与学习笔记。内容Spring如何整合SpringMVC与Mybatis以及整合原理的源码分析。
Spring整合Mybatis源码解析
spring_mybatis 整合jar包,基本常用jar包的整合,。。。。。。。。。。。。。。。
说明: 本项目是为springMVC+spring+mybatis的集成项目,使用maven进行项目管理 版本说明:spring3.2 mybatis3.2 mysql5.6 相关接口: 用户接口+角色权限接口+博客接口。
spring整合mybatis3所需的各种jar文件
使用spring+mybatis自动生成实体类工具包,部分错误解决方案等
spring cloud整合mybatis,spring cloud整合mybatis,spring cloud整合mybatis
Spring-Mybatis整合包含基本的增删改查简单易懂!
Spring整合MyBatis完整实例 最新 +详细注释 标准的MVC模式 包含详细注释
spring整合mybatis
Spring 整合mybatis(注解&xml版声明式事务).pdf
该压缩包包含了mybatis与spring整合的全部jar包,为学习整合ssm框架提供一个环境
spring整合mybatis所需jar包
spring整合mybatis实例,可以通过test1类测试
Spring整合Mybatis项目-影视资源管理 代码方便初学者理解学习