实现原理及规范

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. Java程序如何生成Jar 执行文件(1)

    一.用Eclipse生产Jar文件 注意:此方法只能打包简单程序,不包含含有第三方jar包的项目 首先,看一下我的项目的目录结构: 1,项目名字上面点右键,选择Export,在选择java\JAR f ...

  2. kali linux (Raspberry Pi 3b) 更新失败 出现上面的问题

    Invalid signature for Kali Linux repositories : “The following signatures were invalid: EXPKEYSIG ED ...

  3. 寻找AP数

    题目背景 正整数n是无穷的,但其中有些数有神奇的性质,我们给它个名字--AP数. 题目描述 对于一个数字i是AP数的充要条件是所有比它小的数的因数个数都没有i的因数个数多.比如6的因数是1 2 3 6 ...

  4. 19-2-28Python的了解以及变量、常量、数据类型、if语句的结构

    Python目前有两个大版本,一个是2.x版本,一个是3.x版本. Python2x:源码冗余,混乱:且默认ASCII码,只能识别英文字母数字. Python3x:源码整合,美观,清晰,简单.默认ut ...

  5. (转)Dubbo 简单Dome搭建

    (转)原地址https://blog.csdn.net/noaman_wgs/article/details/70214612/ Dubbo背景和简介 Dubbo开始于电商系统,因此在这里先从电商系统 ...

  6. 【c学习-8】

    /*继承结构体*/ #include // 定义子结构体 struct date{ int year; int month; int day; }; //定义父结构体 struct student{ ...

  7. H5混合开发进阶

    混合开发: 原生app里面,IOS 安卓的原生app里面,嵌套h5界面. 通过原生app里的一个webView盒子进行交互.webView是原生app内置的一个XXX,里面可以放置h5界面.可以相互调 ...

  8. java简单web爬虫(网页图片)

    java简单web爬虫(网页图片)效果,执行main()方法后图片就下载道C盘的res文件夹中.没有的话创建一个文件夹代码里的常量根据自己的需求修改,代码附到下面. package com.sinit ...

  9. 基于STM32F103的Max30100心率、血氧检测代码(转载)

    MAX30100是能够读取心率.血氧的传感器,通信方式是通过IIC进行通信.其工作原理是通过红外led灯照射,能够得到心率的ADC值.       MAX30100的寄存器可以分为五类,状态寄存器.F ...

  10. JAVA 泛型方法<T>

    public static void main(String[] args) throws Exception { String[] arr = new String[]{"1", ...