spring-mybatis——MapperFactoryBean 之映射接口

为了代替手工使用 SqlSessionDaoSupport 或 SqlSessionTemplate 编写数据访问对象(DAO)的代码,MyBatis-Spring 提供了一个动态代理的实现——MapperFactoryBean。这个类可以让你直接注入数据映射器接口到你的 service 层bean 中。当使用映射器时,你仅仅如调用你的 DAO 一样调用它们就可以了,但是你不需要编写任何 DAO 实现的代码,因为 MyBatis-Spring将会为你创建代理。同样,MapperFactoryBean创建的代理控制开放和关闭 session

在使用MapperFactoryBean时,可以通过映射接口方式提供sql语句,也可以通过映射文件提供sql语句。


1、spring.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" 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.0.xsd   
            http://www.springframework.org/schema/mvc   
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   
            http://www.springframework.org/schema/context   
            http://www.springframework.org/schema/context/spring-context-3.0.xsd   
            http://www.springframework.org/schema/aop   
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
            http://www.springframework.org/schema/tx   
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">  
   <context:annotation-config />
  
    <!-- 扫描service、dao组件 -->  
    <context:component-scan base-package="cn.edu.nuc" />  
    <!-- 分解配置 jdbc.properites -->  
    <context:property-placeholder location="classpath:jdbc.properties" />  
      
    <!-- 数据源c3p0 -->  
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
        <property name="driverClass" value="${jdbc.driverClassName}" />  
        <property name="jdbcUrl" value="${jdbc.url}" />  
        <property name="user" value="${jdbc.username}" />  
        <property name="password" value="${jdbc.password}" />  
        <property name="maxPoolSize" value="${c3p0.pool.size.max}" />  
        <property name="minPoolSize" value="${c3p0.pool.size.min}" />  
        <property name="initialPoolSize" value="${c3p0.pool.size.ini}" />  
        <property name="acquireIncrement" value="${c3p0.pool.size.increment}" />  
    </bean>
    
    <!--MapperFactoryBean、使用注解sql方式:创建数据映射器,数据映射器必须为接口 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
    </bean>
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">   
     	<property name="mapperInterface" value="cn.edu.nuc.dao.UserMapper" />   
     	<property name="sqlSessionFactory" ref="sqlSessionFactory" />  
    </bean>  
    <!-- 使用 MapperScannerConfigurer,不用每次都指定MapperFactoryBean
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	  <property name="basePackage" value="cn.edu.nuc.dao" />
	</bean>-->
</beans> 

2、数据映射接口:

package cn.edu.nuc.dao;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import cn.edu.nuc.bean.User;

public interface UserMapper {
	@Select("SELECT * FROM user WHERE id = #{id}")   
    User getUser(@Param("id") long id);
}
3、service层:( 可以不用dao层,直接把数据映射器Mapper接口注入到service层

<p style="margin:0in;font-family:Calibri;font-size:10.5pt">packagecn.edu.nuc.service.impl;</p><p style="margin:0in;font-family:Calibri;font-size:10.5pt"> </p><p style="margin:0in;font-family:Calibri;font-size:10.5pt">importorg.springframework.beans.factory.annotation.Autowired;</p><p style="margin:0in;font-family:Calibri;font-size:10.5pt">importorg.springframework.stereotype.Service;</p><p style="margin:0in;font-family:Calibri;font-size:10.5pt">importcn.edu.nuc.bean.User;</p><p style="margin:0in;font-family:Calibri;font-size:10.5pt">importcn.edu.nuc.dao.UserMapper;</p><p style="margin:0in;font-family:Calibri;font-size:10.5pt">importcn.edu.nuc.service.UserService;</p><p style="margin:0in;font-family:Calibri;font-size:10.5pt"> </p><p style="margin:0in;font-family:Calibri;font-size:10.5pt">@Service</p><p style="margin:0in;font-family:Calibri;font-size:10.5pt">public classUserServiceImpl implements UserService {</p><p style="margin:0in;margin-left:.375in;font-family:Calibri;font-size:10.5pt">@Autowired</p><p style="margin:0in;margin-left:.375in;font-family:Calibri;font-size:10.5pt">privateUserMapper userMapper; </p><p style="margin:0in;margin-left:.375in;font-family:Calibri;font-size:10.5pt"> </p><p style="margin:0in;margin-left:.375in;font-family:Calibri;font-size:10.5pt">publicUser getUserById(Long id) {</p><p style="margin:0in;margin-left:.75in;font-family:Calibri;font-size:10.5pt">returnuserMapper.getUser(id);</p><p style="margin:0in;margin-left:.375in;font-family:Calibri;font-size:10.5pt">}</p><p style="margin:0in;font-family:Calibri;font-size:10.5pt">}</p>

MapperFactoryBean创建的代理类实现了 UserMapper 接口,并且注入到应用程序中。 因为代理创建在运行时环境中(Runtime,译者注),那么指定的映射器必须是一个接口,而 不是一个具体的实现类。

4、测试:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.edu.nuc.bean.User;
import cn.edu.nuc.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring.xml"})
public class UserServiceTest {
	@Autowired  
    <span style="white-space:pre">	</span>private UserService userService;  
	
	@Test
	public void getUserTest() {
		Long id = 1L;
		User user = userService.getUserById(id);
		
		System.out.println(user.getName());
	}
	
}

分析:

1MapperFactoryBean 创建的代理类实现了 UserMapper 接口,并且注入到应用程序中。 因为代理创建在运行时环境中(Runtime,译者注),那么指定的映射器必须是一个接口,而不是一个具体的实现类。

2)数据映射器接口UserMapper不需要显示写一个注解交给spring管理,然后在service层进行注入;因为UserMapper接口其实是供MapperFactoryBean 创建的代理类来实现的,然后自动放到springioc容器中,在Service实现中注入的是MapperFactoryBean 创建的代理类;


补充

没有必要在 Spring 的 XML配置文件中注册所有的映射器。相反,你可以使用一个MapperScannerConfigurer, 它 将 会 查 找 类 路 径 下 的 映 射 器 并 自 动 将 它 们 创 建 成 MapperFactoryBean。要创建MapperScannerConfigurer,可以在 Spring 的配置中添加如下代码:

<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">

  <property name="basePackage"value="org.mybatis.spring.sample.mapper" />

</bean>

basePackage属性是让你为映射器接口文件设置基本的包路径。 你可以使用分号或逗号 作为分隔符设置多于一个的包路径。每个映射器将会在指定的包路径中递归地被搜索到。

所以,上面spring配置文件可以改成如下:

...

         <!--创建数据映射器,数据映射器必须为接口-->

    <bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"> 

        <propertyname="dataSource" ref="dataSource" /> 

    </bean>

    <beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">

  <property name="basePackage"value="cn.edu.nuc.dao" />

         </bean>

注:

1)没 有 必 要 去 指 定 SqlSessionFactory 或SqlSessionTemplate , 因 为 MapperScannerConfigurer 将会创建 MapperFactoryBean,之后自动装配。但是,如果你使 用了一个 以上的 DataSource ,那 么自动 装配可 能会失效 。这种情况下 ,你可 以使用 sqlSessionFactoryBeanName 或 sqlSessionTemplateBeanName 属性来设置正确的bean 名 称来使用。这就是它如何来配置的,注意 bean 的名称是必须的,而不是 bean 的引用,因 此,value 属性在这里替代通常的 ref:

<propertyname="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

MapperScannerConfigurer支 持 过 滤 由 指 定 的 创 建 接 口 或 注 解 创 建 映 射 器 。 annotationClass 属性指定了要寻找的注解名称。markerInterface 属性指定了要寻找的父 接口。如果两者都被指定了,加入到接口中的映射器会匹配两种标准。默认情况下,这两个 属性都是null,所以在基包中给定的所有接口可以作为映射器加载。

2)MapperScannerConfigurer其实和MapperFactoryBean类似,都是创建代理然后注入到spring中;区别是前者是自动扫描数据映射器接口,生成代理类、并注入到Spring的容器中


参考:http://www.mybatis.org/spring/zh/mappers.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值