1 与spring整合
实现mybatis与spring进行整合,通过spring管理SqlSessionFactory、mapper接口。
1.1 mybatis与spring整合jar
mybatis官方提供与mybatis与spring整合jar包:
还包括其它jar:
spring3.2.0
mybatis3.2.7
dbcp连接池
数据库驱动
参考:
1.2 Mybatis配置文件
在classpath下创建mybatis/SqlMapConfig.xml
<?xml version="1.0"encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTDConfig 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!—使用自动扫描器时,mapper.xml文件如果和mapper.java接口在一个目录则此处不用定义mappers -->
<mappers>
<package name="cn.itcast.mybatis.mapper"/>
</mappers>
</configuration>
1.3 Spring配置文件:
在classpath下创建applicationContext.xml,定义数据库链接池、SqlSessionFactory。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 数据库连接池 -->
<bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
<property name="driverClassName"value="${jdbc.driver}"/>
<property name="url"value="${jdbc.url}"/>
<property name="username"value="${jdbc.username}"/>
<property name="password"value="${jdbc.password}"/>
<property name="maxActive"value="10"/>
<property name="maxIdle"value="5"/>
</bean>
<!-- mapper配置 -->
<!-- 让spring管理sqlsessionfactory使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource"ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation"value="classpath:mybatis/SqlMapConfig.xml" />
</bean>
</beans>
注意:在定义sqlSessionFactory时指定数据源dataSource和mybatis的配置文件。
1.4 Mapper编写的三种方法
1.4.1 Dao接口实现类继承SqlSessionDaoSupport
使用此种方法即原始dao开发方法,需要编写dao接口,dao接口实现类、映射文件。
1、 在sqlMapConfig.xml中配置映射文件的位置
<mappers>
<mapper resource="mapper.xml文件的地址" />
<mapper resource="mapper.xml文件的地址" />
</mappers>
2、 定义dao接口
3、 dao接口实现类集成SqlSessionDaoSupport
dao接口实现类方法中可以this.getSqlSession()进行数据增删改查。
4、 spring 配置
<beanid=" "class="mapper接口的实现">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
1.4.2 使用org.mybatis.spring.mapper.MapperFactoryBean
此方法即mapper接口开发方法,只需定义mapper接口,不用编写mapper接口实现类。每个mapper接口都需要在spring配置文件中定义。
1、 在sqlMapConfig.xml中配置mapper.xml的位置
如果mapper.xml和mappre接口的名称相同且在同一个目录,这里可以不用配置
<mappers>
<mapper resource="mapper.xml文件的地址" />
<mapper resource="mapper.xml文件的地址" />
</mappers>
2、 定义mapper接口
3、 Spring中定义
<beanid="" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="mapper接口地址"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
1.4.3 使用mapper扫描器
此方法即mapper接口开发方法,只需定义mapper接口,不用编写mapper接口实现类。只需要在spring配置文件中定义一个mapper扫描器,自动扫描包中的mapper接口生成代代理对象。
1、 mapper.xml文件编写,
2、 定义mapper接口
注意mapper.xml的文件名和mapper的接口名称保持一致,且放在同一个目录
3、 配置mapper扫描器
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper接口包地址"></property>
<property name="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>
</bean>
basePackage:扫描包路径,中间可以用逗号或分号分隔定义多个包
4、 使用扫描器后从spring容器中获取mapper的实现对象
如果将mapper.xml和mapper接口的名称保持一致且放在一个目录则不用在sqlMapConfig.xml中进行配置