实现原理及规范

Mapper接口动态代理的方式需要手动编写Mapper接口,Mybatis框架将根据接口定义创建接口的动态代理对象,代理对象的方法体实现Mapper接口中定义的方法。

使用Mapper接口需要遵守以下规范:

1.  Mapper.xml文件中的namespace与mapper接口的类路径相同

2.  Mapper接口方法名和Mapper.xml中定义的每个statement的id相同

3.  Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql 的parameterType的类型相同

4.  Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同

编写Mapper.xml映射文件

定义mapper映射文件ProductMapper.xml,需要修改namespace的值为 ProductMapper接口路径

<?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">
<!-- namespace:此处使用包名+文件名 的形式 -->
<mapper namespace="com.sl.mapper.ProductMapper">
<!-- 返回自定义类型 注意 selectAllProduct返回的是集合,这种情况下resultType是集合包含的类型,而不能是集合本身 -->
<select id="selectAllProduct" resultType="com.sl.po.Product">
select * from products
</select>
<select id="selectProductsByVo" resultType="com.sl.po.Product">
select * from products
<where>
<if test="product.cityCode!=null">
and citycode = #{product.cityCode}
<!-- citycode = #{cityCode} -->
</if>
<if test="product.Name!=null">
and name like #{product.Name}
</if>
<if test="product.Description!=null">
and description like #{product.Description}
</if>
</where>
</select>
</mapper>

编写Mapper.java接口

接口定义注意点:

1.  Mapper接口方法名和Mapper.xml中定义的statement的id相同

2.  Mapper接口方法的输入参数类型和mapper.xml中定义的statement的parameterType的类型相同

3.  Mapper接口方法的输出参数类型和mapper.xml中定义的statement的resultType的类型相同

package com.sl.mapper;
import java.util.List;
import com.sl.po.Product;
import com.sl.po.ProductVo; public interface ProductMapper { List<Product> selectAllProduct(); List<Product> selectProductsByVo(ProductVo vo); }
package com.sl.po;

public class ProductVo {

    private int category;

    private Product product;

    public int getCategory() {
return category;
} public void setCategory(int category) {
this.category = category;
} public Product getProduct() {
return product;
} public void setProduct(Product product) {
this.product = product;
} }

注册Mapper.xml配置文件(或者Mapper.java接口)

修改SqlMapConfig.xml文件:

<mappers>
<!-- 注册productMapper.xml文件 -->
<mapper resource="mapper/productMapper.xml"></mapper> <!-- mapper.xml文件和mapper接口可以不在一个包下 --> <!-- 注册mapper接口 -->
<!-- <mapper class="com.sl.mapper.ProductMapper"></mapper> --> <!--通过注册mapper接口方式: Mapper接口和mapper.xml必须在同一个包下 --> </mappers>

测试方法

//Mapper接口动态代理方式
public class TestProductMapperClient { // 定义会话SqlSession
SqlSession session = null; @Before
public void init() throws IOException {
// 定义mabatis全局配置文件
String resource = "SqlMapConfig.xml"; // 加载mybatis全局配置文件
// InputStream inputStream =
// TestClient.class.getClassLoader().getResourceAsStream(resource); InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(inputStream);
// 根据sqlSessionFactory产生会话sqlsession
session = factory.openSession();
} // select by id
//@Test
public void testSelectProduct() { // 获取mapper接口的代理对象
ProductMapper productMapper = session.getMapper(ProductMapper.class); List<Product> listProduct = productMapper.selectAllProduct(); for (Product product : listProduct) {
System.out.println(product);
}
// 关闭会话
session.close();
} @Test
public void testwhereTest() {
Product product = new Product();
product.setCityCode("A01");
product.setName("%国际%");
//product.setDescription("%xx%");
//product.setUnitPrice(new BigDecimal(100));
ProductVo vo = new ProductVo();
vo.setProduct(product);
ProductMapper productMapper = session.getMapper(ProductMapper.class);
List<Product> listProduct = productMapper.selectProductsByVo(vo);
for (Product pro : listProduct) {
System.out.println(pro);
} // 关闭会话
session.close();
} }

动态代理对象内部调用sqlSession.selectOne()和sqlSession.selectList()实现数据库操作,具体调用哪一个是根据mapper接口方法的返回值决定,如果返回list则调用selectList方法,如果返回单个对象则调用selectOne方法。

由于参数类型在mapper.xml配置文件中ParameterType配置,所以Mapper.java中接口方法只有一个参数,且类型与mapper.xml中配置的相同(mapper.xml可省略参数类型配置)。

Mybatis学习系列(四)Mapper接口动态代理的更多相关文章

  1. Mybatis学习第二天——mapper的动态代理

    传统的Dao层开发通过接口与实现类的方式,Mybatis中通过mapper动态代理是需要定义接口. 1.传统Dao层封装 那么可以将公共资源提取出来,剩余的封装成方法来实现.下面是UserDaoImp ...

  2. Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring

    Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring 非原创[只为记录],原博文地址:https://www.cnblogs.com/ ...

  3. (转)Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring

    Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring Mybatis在与Spring集成的时候可以配置MapperFactoryBea ...

  4. Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring - 大新博客 - 推酷 - 360安全浏览器 7.1

    Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring - 大新博客 时间 2014-02-11 21:08:00  博客园-所有随笔区 ...

  5. Spring学习(四)—— java动态代理(JDK和cglib)

    JAVA的动态代理 代理模式 代理模式是常用的java设计模式,他 的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等.代理类与委托 ...

  6. Mybatis学习系列(三)动态SQL

    在mapper配置文件中,有时需要根据查询条件选择不同的SQL语句,或者将一些使用频率高的SQL语句单独配置,在需要使用的地方引用.Mybatis的一个特性:动态SQL,来解决这个问题. mybati ...

  7. mybatis学习系列四--mybatis generator逆向工程

    采用命令行方式执行逆向工程 1.配置文件generatorConfig.xml 保存在目录:D:\E\workspace\eclipse\mybatis_generator <?xmlversi ...

  8. 【MyBatis学习04】mapper代理方法开发dao

    上一篇博文总结了mybatis使用 原始dao的方法存在的一些弊端,我们肯定不会去用它,那么mybatis中该如何开发dao呢?如题所述,这篇博文主要来总结一下使用mapper代理的方法来开发dao的 ...

  9. Mybaits 源码解析 (十一)----- 设计模式精妙使用:静态代理和动态代理结合使用:@MapperScan将Mapper接口生成代理注入到Spring

    上一篇文章我们讲了SqlSessionFactoryBean,通过这个FactoryBean创建SqlSessionFactory并注册进Spring容器,这篇文章我们就讲剩下的部分,通过Mapper ...

随机推荐

  1. http返回值含义

    1xx:信息响应类,表示接收到请求并且继续处理2xx:处理成功响应类,表示动作被成功接收.理解和接受 3xx:重定向响应类,为了完成指定的动作,必须接受进一步处理4xx:客户端错误,客户请求包含语法错 ...

  2. ABAP术语-Logical Lock

    Logical Lock 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/03/1088323.html Program logic that ...

  3. 思维导图 XMind 8 Update 8 Pro for Mac 中文破解版

    破解包下载地址 在官网下载8版本就可以了

  4. oracle 12c rac vip和监听故障

    环境:aix 7.1 ,oracle 12.1.0.2 rac -3节点. 硬件故障后,硬件工程师更换了内联网卡,不知为何资源VIP也有问题,只好先添加了VIP srvctl add vip -nod ...

  5. canvas 制作表情包

    canvas 制作表情包 代码如下. <!DOCTYPE html> <html> <head> <title>表情制作</title> & ...

  6. 邮件发送失败问题:Sending the email to the following server failed : smtp.qiye.163.com:25

    [邮件发送错误] : Sending the email to the following server failed : smtp.qiye.163.com:25, {}org.apache.com ...

  7. Windows之cmd指令

    gpedit.msc-----本地计算机策略sndrec32-------录音机 Nslookup-------IP地址侦测器 explorer-------打开资源管理器 logoff------- ...

  8. 【ntp时间校准配置】

    Ntp(网络时间协议)是一种可以通过TCP/IP网络传播,其架构模式可分为C/S(客户端/服务器),PTP(对等),broatcast(广播), mutilbrocast(组播),无论在任何系统或设备 ...

  9. Leecode刷题之旅-C语言/python-13.罗马数字转整数

    /* * @lc app=leetcode.cn id=13 lang=c * * [13] 罗马数字转整数 * * https://leetcode-cn.com/problems/roman-to ...

  10. 打印N个真值的所有真值组合

    例:N=2 (true,true),(false,true),(true,false),(false,false) #include<stdio.h> int count=0; void ...