学习本节内容请先看"MyBatis的基本应用"。地址:http://lydia-fly.iteye.com/admin/blogs/2152948
Spring与MyBatis整合需要引入mybatis-spring.jar文件包。
其提供了与整合相关的API:
SqlSessionFactoryBean
--为整合应用提供SqlSession对象资源
MapperFactoryBean
--根据指定的Mapper接口生成Bean实例
MapperScannerConfigurer
--根据指定包批量扫描Mapper接口并生成实例
SqlSessionFactoryBean:
在单独使用MyBatis时,所有操作都是围绕SqlSession展开的,SqlSession是通过SqlSessionFactory获取的,SqlSessionFactory又是通过SqlSessionFactoryBuilder创建生成的。
在SpringMvc+MyBatis整合时,同样需要SqlSession。SqlSessionFactoryBean这个组件通过原来的SqlSessionFactoryBuilder生成SqlSessionFactory对象,为整合应用提供SqlSession对象。
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" /> <property name="username" value="jsd1403" /> <property name="password" value="root" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="myDataSource" /> <property name="mapperLocations" value="classpath:com/lydia/entity/*.xml" /> </bean>
MapperFactoryBean:
其作用是根据Mapper接口获取我们想要的Mapper对象,它封装了原有的session.getMapper()功能的实现。
在定义MapperFactoryBean时,需要注入一下两个属性:
--SqlSessionFactoryBean对象,用于提供SqlSession
--要返回Mapper对象的Mapper接口
MapperFactoryBean配置如下:
<!-- 方法一:定义mapper --> <bean id="deptMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.lydia.entity.DeptMapper"></property> <!-- 指定SqlSessionFactoryBean对象 --> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean>
MapperScannerConfigurer配置使用:
注意:使用MapperFactoryBean时,当有一个Mapper(可以理解为表对应的映射文件)就MapperFactoryBean,当mapper少数可以通过applicationContext配置文件,通过id获取。
如果大量的mapper,需要使用mybatis-spring.jar通过的MapperScannerConfigurer组件,通过这个组件可以自动扫描指定包下的各个Mapper接口,并注册对应的MapperFactoryBean对象。
把之前的MapperFactoryBean的配置注释掉,换成如下配置依然执行通过:
<!--方法2: 可以把扫描到的Mapper接口变成Mapper对象--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--指定要扫描包: 多个包用逗号隔开 --> <property name="basePackage" value="com.lydia,com.tarena" /> <!--指定sqlSessionFactory --> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean>
注意:上面sqlSessionFactory属性也可以不用指定,默认会以Autowired方式注入。
如果指定的某个包下并不完全是我们定义的Mapper接口,我们也可以通过自定义注解的方式指定生成MapperFactoryBean对象。
配置如下:
<!--方法3: 只要Mapper类前面加上@MyBatisRepository 这个自己指定的注解就OK--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.lydia" /> <property name="annotationClass" value="com.lydia.annotation.MyBatisRepository" /> </bean>
自定义注解:MyBatisRepository.java
public @interface MyBatisRepository { }
在DeptMapper接口中使用:
//@Repository("deptMapper") @MyBatisRepository public interface DeptMapper { void addDept(Dept dept); void deleteDept(Dept dept); void updateDept(Dept dept); ...... }
测试:
public class TestCase { @Test public void testFindAll() throws Exception { String conf = "applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(conf); //获取对应的mapper对象,并调用mapper接口中对应的方法 DeptMapper mapper = ac.getBean("deptMapper", DeptMapper.class); List<Dept> lists = mapper.findAllDept(); for (Dept dept : lists) { System.out.println(dept); } } }
相关推荐
Spring+SpringMVC+Mybatis框架整合例子——亲测可用.zip
Spring MVC+Mybatis整合详解——资源自Linux公社
基于maven+springmvc+redis+mybatis整合案例框架,主要实现redis的读取案例
Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+...
springboot+mybatis整合,利用配置文件生成实体类以及操作数据库的方法,实现后台接收验证码,注册登录功能
基于SpringMVC+Spring+MyBatis+Maven项目案例 基于SpringMVC+Spring+MyBatis+Maven项目案例 基于SpringMVC+Spring+MyBatis+Maven项目案例 基于SpringMVC+Spring+MyBatis+Maven项目案例 基于SpringMVC+Spring+MyBatis...
这是一个基于springboot+vue+mybatis的学生成绩管理系统。java开发基于springboot的管理系统源码。这是一个基于springboot+vue+mybatis的学生成绩管理系统。java开发基于springboot的管理系统源码。这是一个基于...
SpringBoot+mybatis+Oracle整合代码,测试通过,可以学习下载。
基于springboot+mybatis的选课管理系统基于springboot+mybatis的选课管理系统 基于springboot+mybatis的选课管理系统基于springboot+mybatis的选课管理系统 基于springboot+mybatis的选课管理系统基于springboot+...
基于SpringBoot+Mybatis的演出订票系统基于SpringBoot+Mybatis的演出订票系统 基于SpringBoot+Mybatis的演出订票系统基于SpringBoot+Mybatis的演出订票系统 基于SpringBoot+Mybatis的演出订票系统基于SpringBoot+...
基于spring+spring mvc+mybatis框架整合实现超市货物管理系统(实现登入功能、mysql数据库的增删改查及分页显示)
springMVC+cxf+mybatis整合项目
Spring+SpringMVC+MyBatis 整合 DEMO
springboot+mybatis 整合Demo下载,直接下载导入就可以使用。例子已经写的非常完善。
Struts2+maven+spring+mybatis整合,并实现注册功能实例。
项目描述 spring boot2开发的...spring boot2+mybatis+thymeleaf+layui http://localhost:8080/ 管理后台登录地址 http://localhost:8080/login admin 123456 springboot开发的系统,直接运行YixinliApplication即可
springmvc+myBatis整合,项目比较纯净,简单实现一个登录功能
Spring + Spring MVC + MyBatis整合所需的jar包下载。
spring + springmvc + mybatis 整合 demo 及 mybatis-pagehelper分页 demo
基于SpringBoot + MyBatis + Layui的后台权限管理系统。代码简洁易懂、界面美观大方,内部封装了权限管理系统常用的全部功能,可直接作为快速开发JavaWeb项目的脚手架使用。 基于SpringBoot + MyBatis + Layui的...