將MyBatis與Spring進行整合,主要解決的問題就是將SqlSessionFactory對象交由Spring來管理。。所以該整合,只需將SQLSessionFactory的對象生成器SQLSessionFactoryBean注冊到Spring容器中,再將其注入給Dao的實現類即可完成整合。
可以通過2種方式來實現Spring與MyBatis的整合:
- Mapper動態代理
- 支持掃描的Mapper動態代理
一、環境搭建
二、定義映射文件

1 import java.util.List; 2 3 import com.jmu.beans.Student; 4 5 //Dao增刪改查 6 public interface IStudentDao { 7 void insertStudent(Student student); 8 void deleteById(int id); 9 void updateStudent(Student student); 10 11 List<String> selectAllStudentsNames(); 12 String selectStudentNameById(int id); 13 14 List<Student> selectAllStudents(); 15 Student selectStudentById(int id); 16 }

1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.jmu.dao.IStudentDao"> 6 <insert id="insertStudent"> 7 insert into student(name,age) values(#{name},#{age}) 8 </insert> 9 10 <delete id="deleteById"> 11 delete from student where id=#{XXX} 12 </delete> 13 14 <update id="updateStudent"> 15 update student set name=#{name},age=#{age} where id=#{id} 16 </update> 17 18 <select id="selectAllStudents" resultType="student"> 19 select id,name,age from student 20 </select> 21 22 <select id="selectStudentById" resultType="student"> 23 select id,name,age from student where id=#{XXX} 24 </select> 25 </mapper>
三、定義主配置文件

1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 7 <!--配置別名 --> 8 <typeAliases> 9 <package name="com.jmu.beans" /> 10 </typeAliases> 11 12 <mappers> 13 <!-- <mapper resource="com/jmu/dao/mapper.xml"/> --> 14 <!--xml文件和dao同名 --> 15 <package name="com.jmu.dao" /> 16 17 </mappers> 18 19 </configuration>
四、Mapper動態代理方式生成Dao代理對象

1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xsi:schemaLocation=" 5 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 7 8 9 <!--注冊數據源:C3P0 --> 10 <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 11 <property name="driverClass" value="${jdbc.driver}" /> 12 <property name="jdbcUrl" value="${jdbc.url}" /> 13 <property name="user" value="${jdbc.user}" /> 14 <property name="password" value="${jdbc.password}" /> 15 </bean> 16 17 <!-- 注冊屬性文件 --> 18 <context:property-placeholder location="classpath:jdbc.properties" /> 19 20 <bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 21 <property name="configLocation" value="classpath:mybatis.xml"></property> 22 <property name="dataSource" ref="myDataSource"></property> 23 </bean> 24 25 26 <!--生成Dao的代理對象 --> 27 <bean id="studentDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> 28 <property name="sqlSessionFactory" ref="mySqlSessionFactory" /> 29 <property name="mapperInterface" value="com.jmu.dao.IStudentDao" /> 30 </bean> 31 32 <!-- 注冊Service --> 33 <bean id="studentService" class="com.jmu.service.StudentServiceImpl"> 34 <property name="dao" ref="studentDao" /> 35 </bean> 36 </beans>
五、測試

1 import java.util.List; 2 3 import org.junit.Before; 4 import org.junit.Test; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 import com.jmu.beans.Student; 9 import com.jmu.service.IStudentService; 10 11 public class MyTest { 12 13 private IStudentService service; 14 15 @Before 16 public void before() { 17 //創建容器對象 18 String resource = "applicationContext.xml"; 19 ApplicationContext ac=new ClassPathXmlApplicationContext(resource); 20 service = (IStudentService) ac.getBean("studentService"); 21 } 22 23 @Test 24 public void test01() { 25 Student student=new Student("張三", 23); 26 service.addStudent(student); 27 } 28 29 @Test 30 public void test02() { 31 service.removeById(2); 32 } 33 34 @Test 35 public void test03() { 36 Student student=new Student("王意義", 23); 37 student.setId(3); 38 service.modifyStudent(student); 39 } 40 @Test 41 public void test04() { 42 List<String> names = service.findAllStudentsNames(); 43 System.out.println(names); 44 } 45 @Test 46 public void test05() { 47 String names = service.findStudentNameById(3); 48 System.out.println(names); 49 } 50 51 @Test 52 public void test06() { 53 List<Student> students=service.findAllStudents(); 54 for (Student student : students) { 55 System.out.println(student); 56 } 57 } 58 59 @Test 60 public void test07() { 61 Student student=service.findStudentById(3); 62 System.out.println(student); 63 } 64 65 66 }
對於IstudentDao.xml中沒有寫到的List<String> selectAllStudentsNames()和String selectStudentNameById(int id)進行修改
六、支持掃描的動態代理
對於上面的情況,如果有多個Dao接口,就要寫多次的以下字段
<!--生成Dao的代理對象 -->
<bean id="studentDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="mySqlSessionFactory" />
<property name="mapperInterface" value="com.jmu.dao.IStudentDao" />
</bean>

1 <!--生成Dao的代理對象 2 當前配置會被本包中所有的接口生成代理對象 3 --> 4 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 5 <property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory" /> 6 <property name="basePackage" value="com.jmu.dao" /> 7 </bean> 8 9 <!-- 注冊Service --> 10 <bean id="studentService" class="com.jmu.service.StudentServiceImpl"> 11 <!-- 這里的Dao的注入需要使用ref屬性,且其作為接口的簡單類名 --> 12 <property name="dao" ref="studentDao" /> 13 </bean>