Spring与Mybatis整合需要引入一个mybatis-spring.jar包,该整合包有Mybatis提供,可以从Mybatis官网下载。
该jar包提供了几个API:
1.SqlSessionFactoryBean-为整合应用提供SqlSession对象资源。
在单独使用MyBatis时,所有操作都是围绕SqlSession展开的,SqlSession是通过SqlSessionFactory获取的,SqlSessionFactory又是通过SqlSessionFactoryBuilder创建生成的。
在Spring和Mybatis整合应用时,同样需要SqlSession,而SqlSessionFactoryBean组件的作用就是通过原SqlSessionFactoryBuilder生成SqlSessionFactory对象,为整合应用提供SqlSession对象。
SqlSessionFactoryBean在applicationContext.xml中定义格式如下:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定连接资源 -->
<property name="dataSource" ref="myDataSource"/>
<!-- 指定映射文件 -->
<property name="mapperLocations" value="classpath:org/tarena/entity/*.xml"/>
</bean>
在定义SqlSessionFactoryBean时,可以使用以下常用属性:
--dataSource:用于指定连接数据库的数据源
--mapperLocations:用于指定Mapper文件存放的位置
--configLocation:用于指定Mybatis的配置文件位置,如果指定了该属性,那么会以该配置文件的内容作为配置信息构建对应的SqlSessionFactoryBuilder,但是后续属性 指定的内容会覆盖该配置文件里面指定的对应内容。
--typeAliasesPackage:它一般对应我们的实体类所在的包,这个时候会自动取对应包中不包括包名的简单类名作为包括包名的别名。多个package之间可以用逗号或者分号等 来进行分隔。
--typeAliases:数组类型,用来指定别名的,指定了这个属性后,Mybatis会把这个类型的短名称作为这个类型的别名。
2.MapperFactoryBean-根据指定Mapper接口生成Bean实例。
3.MapperScannerConfigurer-根据指定包批量扫描Mapper接口并生成实例。
在使用MapperFactoryBean时,有一个Mapper就需要定义一个对应的MapperFactoryBean。但当遇到大量Mapper时就需要使用使用MapperScannerConfigurer组件,通过这个组件会自动扫描各个Mapper接口,并注册对应的MapperFactoryBean对象。
在定义MapperScannerConfigurer时,只需要指定一个basePackage即可。basePackage用于指定Mapper接口所在的包,这个包及其所有子包下面的Mapper接口都将被搜索到,并把它们注册为一个个MapperFactoryBean对象。多个包之间可以使用逗号或者分号进行分隔。
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.tarena.mapper"/>
</bean>
注意:sqlSessionFactory属性可以不用指定,会以Autowired方式自动注入
MapperScannerConfigurer定义示例:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.tarena"/>
<property name="annotationClass" value="org.tarena.annotation.MyBatisRepository"/>
</bean>
上面配置定义:MapperScannerConfigurer自动扫描org.tarena包下所有接口,遇到带@MyBatisRepository标记的将对应MapperFactoryBean对象注册。
4.SqlSessionTemplate
上述整合完成后,程序可直接使用Spring容器中的Mapper接口实例进行编程。此外,mybatis-spring.jar还提供了一个SqlSessionTemplate组件,也可以将该组件注入给程序中的DAO,在DAO中利用SqlSessionTemplate编程。
基于SqlSessionTemplate的DAO示例代码如下
@Repository
public class MyBatisDeptDAO implements DeptDAO{
private SqlSession Template template;
@Autowired
public void setTemplate(SqlSession Template template){
this.template=template;
}
public List<Dept> findAll(){
List<Dept> list= template.selectList("findAll");
return list;
}
}
基于SqlSessionTemplate的DAO配置信息如下:
<!-- 定义SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!-- 扫描DAO并注入template -->
<context:component-scan base-package="org.tarena.dao"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
</bean>
整合步骤介绍
基于SpringMVC和MyBatis技术开发的主要步骤如下:
1.创建工程,搭建SpringMVC和MyBatis技术环境
1)创建一个Web工程
2)添加MyBatis相关技术环境
-引入数据库驱动包和MyBatis开发包
-引入dbcp连接池开发包
3)添加SpringMVC相关技术环境
-引入Spring IOC,JDBC,TX等支持的开发包
-引入Spring webmvc支持的开发包
-在src下添加applicationContext.xml配置文件
-在web.xml中配置DispatcherServlet主控制器
4)引入MyBatis和Spring整合开发包mybatis-spring.jar
2.基于MapperScannerConfigurer方式整合MyBatis的Mapper接口(推荐)
1)根据数据表编写实体类
2)编写Mapper映射文件,在XML中添加SQL操作的定义
3)编写Mapper接口,定义SQL操作方法
4)在Spring配置文件中定义以下Bean
-DataSource
-SqlSessionFactoryBean
-MapperScannerConfigurer
5)测试Spring容器的DAO组件
3.编写和配置SpringMVC的主要组件,例如:Controller,HandlerMapping,ViewResolver等
1)编写Controller和请求处理方法
2)配置<mvc:annotation-driven/>,支持@RequestMapping
3)配置Controller组件
-开启组件扫描,将Controller扫描到Spring容器
-需要DAO时采用注入方式使用
-在请求处理方法上使用@RequestMapping指定对应的请求
4)配置ViewResolver组件
4.编写JSP视图组件,利用标签和表达式显示模型数据
1)JSP可以使用JSTL标签库,需要引入开发包
2)JSP可以使用EL表达式
3)JSP可以使用SpringMVC的表单标签
5.测试程序