MyBitis(iBitis)系列随笔之六:mybitis与spring集成

MyBitis(iBitis)系列随笔之一:MyBitis入门实例

MyBitis(iBitis)系列随笔之二:类型别名(typeAliases)与表-对象映射(ORM)

MyBitis(iBitis)系列随笔之三:简单实现CRUD

MyBitis(iBitis)系列随笔之四:多表(多对一查询操作)

MyBitis(iBitis)系列随笔之五:多表(一对多关联查询)

MyBitis(iBitis)系列随笔之六:mybitis与spring集成

         目前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">

	<!-- more bean definitions go here -->
	<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">
		<!--dataSource属性指定要用到的连接池--> 
		<property name="dataSource" ref="dataSource"/>
		<!--configLocation属性指定mybatis的核心配置文件--> 
		<property name="configLocation" value="mybatis-config.xml"/> 
	</bean> 
	<bean id="mobileMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
	    <!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例-->  
		<property name="sqlSessionFactory" ref="sqlSessionFactory" /> 
		<!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象-->  
		<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());
	}
}


九、测试结果 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值