实现原理及规范

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. UDP端口启动后一段时间无法接收到数据

    接口需求:开发一个UDP协议的接口作为服务端接收来自客户端的认证数据,数据量每分钟7w+条; 数据格式:标准的redius协议,redius协议的相关知识在网上查资料,提供线索:http://blog ...

  2. 浅谈Quartz(SimpleTrigger&CronTrigger)

    private void quartzOrderReturn(List<String> returnIds) { try { Scheduler scheduler = StdSchedu ...

  3. C.Sum 2017 ACM-ICPC 亚洲区(西安赛区)网络赛

    题目来源:Sum 限制:1000ms 32768K Define the function S(x) for xx is a positive integer. S(x) equals to the ...

  4. ABAP术语-Accounting Document

    Accounting Document 原文:http://www.cnblogs.com/qiangsheng/archive/2007/12/12/991731.html Accounting d ...

  5. gcc 手动安装

    由于公司内部服务器没有联通外网,只能苦逼的手动安装gcc(自带的版本太老) rpm -ivh ppl-0.10.2-11.el6.x86_64.rpm rpm -ivh cloog-ppl-0.15. ...

  6. linux命令之压缩与归档

    1.   gzip:压缩工具 语法·:gzip [选项](参数) 命令说明:运用广泛的压缩程序,文件经它压缩后,其名称后面以“.gz”扩展名 常用命令选项: -N:压缩文件后,保留文件的原文件名和时间 ...

  7. python的列表数据类型及常用操作

    列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现. 列表中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. 列表可以进行的操作包括索 ...

  8. Hadoop(8)-HDFS的读写数据流程以及机架感知

    1. HDFS的写数据流程 1.客户端通过fs模块向NameNode申请文件上传,NameNode检查请求是否合法,如用户权限,目标文件是否已存在,父目录是否存在等等 2.NameNode返回是否可以 ...

  9. Python的matplotlib模块的使用-Github仓库

    import matplotlib.pyplot as plt import numpy as np import requests url='https://api.github.com/searc ...

  10. 网站title标题被改并被百度网址安全中心提醒的解决办法

    国庆假日期间我们Sine安全接到众多网站站长求助网站标题被改导致在百度搜索中百度安全中心提醒被拦截,导致网站正常用户无法浏览网站被跳转到一些菠菜du博网站,而且很明显的一个特征就是在百度中搜索关键词的 ...