sm整合

来源:互联网 发布:喵哥捏脸数据 编辑:程序博客网 时间:2024/04/26 01:17

目前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实体

[java] view plain copy
  1. package com.jefry;  
  2.   
  3. public class Mobile {  
  4.     private int id;  
  5.     private String telnumber;  
  6.   
  7.     public int getId() {  
  8.         return id;  
  9.     }  
  10.   
  11.     public void setId(int id) {  
  12.         this.id = id;  
  13.     }  
  14.   
  15.     public String getTelnumber() {  
  16.         return telnumber;  
  17.     }  
  18.   
  19.     public void setTelnumber(String telnumber) {  
  20.         this.telnumber = telnumber;  
  21.     }  
  22.   
  23. }  


三、编写Mobile数据库接口

[java] view plain copy
  1. package com.jefry;  
  2.   
  3. public interface MobileMapper {  
  4.     public Mobile getMoble(int id);   
  5. }  

四、编写Mobile业务Bean,里面注入Mobile数据库接口对象

[java] view plain copy
  1. package com.jefry;  
  2.   
  3. public class MobileService {  
  4.     private MobileMapper mobileMapper;  
  5.     public void setMobileMapper(MobileMapper mobileMapper) {  
  6.         this.mobileMapper = mobileMapper;  
  7.     }  
  8.       
  9.     public Mobile getMoble(int id){  
  10.         return mobileMapper.getMoble(id);  
  11.     }  
  12. }  


五、准备ibitis配置文件

[html] view plain copy
  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.     <typeAliases>  
  7.         <typeAlias alias="Mobile" type="com.jefry.Mobile"/>  
  8.     </typeAliases>  
  9.     <mappers>  
  10.         <mapper resource="com/jefry/MobileMapper.xml"/>  
  11.     </mappers>  
  12. </configuration>  

六、编写Mobile数据库映射文件

[html] view plain copy
  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.jefry.MobileMapper">  
  6.      <resultMap id="mobileResultMap" type="Mobile">   
  7.             <id property="id" column="id" javaType="int" jdbcType="INTEGER"/>    
  8.             <result property="telnumber" column="telnumber" javaType="string" jdbcType="VARCHAR"/>    
  9.      </resultMap>  
  10.       
  11.     <select id="getMoble" parameterType="int"  resultMap="mobileResultMap" >  
  12.          select * from t_mobile where id = #{id}   
  13.     </select>  
  14. </mapper>  

七、由Spring管理业务bean对象(bean.xml)

[html] view plain copy
  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"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.     http://www.springframework.org/schema/beans/spring-beans.xsd">  
  6.   
  7.     <!-- more bean definitions go here -->  
  8.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">   
  9.         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
  10.         <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>  
  11.         <property name="username" value="root"/>   
  12.         <property name="password" value="root"/>  
  13.     </bean>  
  14.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  15.         <!--dataSource属性指定要用到的连接池-->   
  16.         <property name="dataSource" ref="dataSource"/>  
  17.         <!--configLocation属性指定mybatis的核心配置文件-->   
  18.         <property name="configLocation" value="mybatis-config.xml"/>   
  19.     </bean>   
  20.     <bean id="mobileMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">   
  21.         <!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例-->    
  22.         <property name="sqlSessionFactory" ref="sqlSessionFactory" />   
  23.         <!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象-->    
  24.         <property name="mapperInterface" value="com.jefry.MobileMapper" />    
  25.     </bean>   
  26.       
  27.     <bean id="mobileService" class="com.jefry.MobileService">  
  28.          <property name="mobileMapper" ref="mobileMapper"/>   
  29.     </bean>  
  30. </beans>  

八、编写测试代码

[java] view plain copy
  1. package com.jefry;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.springframework.context.ApplicationContext;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7.   
  8. public class Test {  
  9.     static String resource = "mybatis-config.xml";  
  10.   
  11.     public static void main(String[] args) throws IOException {  
  12.           
  13.         ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"bean.xml"});  
  14.         MobileService mobileService = context.getBean("mobileService", MobileService.class);  
  15.         System.out.println("编号1的电话号码:" + mobileService.getMoble(1).getTelnumber());  
  16.     }  
  17. }  


九、测试结果 


0 0
原创粉丝点击