目前Spring官方还没有出整合Mybatis的特性,但是mybitis比较给力,开发了一个mybatis-spring插件,达到与Spring的完美整合目的。
         在与Spring集成前,一方面我们需要下载Spring的所需的jar文件,我是刚才Spring官方网站上下载的最新的spring-framework-3.2.2.RELEASE-dist.zip压缩文件,里面包含了Spring所有jar文件以及文档;另一方面我们需要从mybitis的官方网站上下载mybatis-spring插件,我是下载最新的mybatis-spring-1.2.0-bundle.zip压缩文件,里面包含了需要的jar文件。由于本文使用的dbcp作为数据库连接池,所以还要准备dbcp jar文件。
          关于Spring的事务配置,本博文不会赘述。下面就详细介绍下集成
           以下代码按照下图结构组织:
          
 
一、准备t_mobile表
        
二、编写Mobile实体
- package com.jefry;  
-   
- public class Mobile {  
-     private int id;  
-     private String telnumber;  
-   
-     public int getId() {  
-         return id;  
-     }  
-   
-     public void setId(int id) {  
-         this.id = id;  
-     }  
-   
-     public String getTelnumber() {  
-         return telnumber;  
-     }  
-   
-     public void setTelnumber(String telnumber) {  
-         this.telnumber = telnumber;  
-     }  
-   
- }  
 
三、编写Mobile数据库接口
- package com.jefry;  
-   
- public interface MobileMapper {  
-     public Mobile getMoble(int id);   
- }  
 
四、编写Mobile业务Bean,里面注入Mobile数据库接口对象
- package com.jefry;  
-   
- public class MobileService {  
-     private MobileMapper mobileMapper;  
-     public void setMobileMapper(MobileMapper mobileMapper) {  
-         this.mobileMapper = mobileMapper;  
-     }  
-       
-     public Mobile getMoble(int id){  
-         return mobileMapper.getMoble(id);  
-     }  
- }  
 
五、准备ibitis配置文件
- <?xml version="1.0" encoding="UTF-8" ?>  
- <!DOCTYPE configuration  
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
- "http://mybatis.org/dtd/mybatis-3-config.dtd">  
- <configuration>  
-     <typeAliases>  
-         <typeAlias alias="Mobile" type="com.jefry.Mobile"/>  
-     </typeAliases>  
-     <mappers>  
-         <mapper resource="com/jefry/MobileMapper.xml"/>  
-     </mappers>  
- </configuration>  
 
六、编写Mobile数据库映射文件
- <?xml version="1.0" encoding="UTF-8" ?>  
- <!DOCTYPE mapper  
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
- <mapper namespace="com.jefry.MobileMapper">  
-      <resultMap id="mobileResultMap" type="Mobile">   
-             <id property="id" column="id" javaType="int" jdbcType="INTEGER"/>    
-             <result property="telnumber" column="telnumber" javaType="string" jdbcType="VARCHAR"/>    
-      </resultMap>  
-       
-     <select id="getMoble" parameterType="int"  resultMap="mobileResultMap" >  
-          select * from t_mobile where id = #{id}   
-     </select>  
- </mapper>  
 
七、由Spring管理业务bean对象(bean.xml)
- <?xml version="1.0" encoding="UTF-8"?>  
- <beans xmlns="http://www.springframework.org/schema/beans"  
-     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
-     xsi:schemaLocation="http://www.springframework.org/schema/beans  
-     http://www.springframework.org/schema/beans/spring-beans.xsd">  
-   
-     
-     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">   
-         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
-         <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>  
-         <property name="username" value="root"/>   
-         <property name="password" value="root"/>  
-     </bean>  
-     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
-         
-         <property name="dataSource" ref="dataSource"/>  
-         
-         <property name="configLocation" value="mybatis-config.xml"/>   
-     </bean>   
-     <bean id="mobileMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">   
-         
-         <property name="sqlSessionFactory" ref="sqlSessionFactory" />   
-         
-         <property name="mapperInterface" value="com.jefry.MobileMapper" />    
-     </bean>   
-       
-     <bean id="mobileService" class="com.jefry.MobileService">  
-          <property name="mobileMapper" ref="mobileMapper"/>   
-     </bean>  
- </beans>  
 
八、编写测试代码
- package com.jefry;  
-   
- import java.io.IOException;  
-   
- import org.springframework.context.ApplicationContext;  
- import org.springframework.context.support.ClassPathXmlApplicationContext;  
-   
- public class Test {  
-     static String resource = "mybatis-config.xml";  
-   
-     public static void main(String[] args) throws IOException {  
-           
-         ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"bean.xml"});  
-         MobileService mobileService = context.getBean("mobileService", MobileService.class);  
-         System.out.println("编号1的电话号码:" + mobileService.getMoble(1).getTelnumber());  
-     }  
- }