Mybatis学习系列(四)Mapper接口动态代理
实现原理及规范
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接口动态代理的更多相关文章
- Mybatis学习第二天——mapper的动态代理
传统的Dao层开发通过接口与实现类的方式,Mybatis中通过mapper动态代理是需要定义接口. 1.传统Dao层封装 那么可以将公共资源提取出来,剩余的封装成方法来实现.下面是UserDaoImp ...
- Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring
Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring 非原创[只为记录],原博文地址:https://www.cnblogs.com/ ...
- (转)Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring
Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring Mybatis在与Spring集成的时候可以配置MapperFactoryBea ...
- Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring - 大新博客 - 推酷 - 360安全浏览器 7.1
Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring - 大新博客 时间 2014-02-11 21:08:00 博客园-所有随笔区 ...
- Spring学习(四)—— java动态代理(JDK和cglib)
JAVA的动态代理 代理模式 代理模式是常用的java设计模式,他 的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等.代理类与委托 ...
- Mybatis学习系列(三)动态SQL
在mapper配置文件中,有时需要根据查询条件选择不同的SQL语句,或者将一些使用频率高的SQL语句单独配置,在需要使用的地方引用.Mybatis的一个特性:动态SQL,来解决这个问题. mybati ...
- mybatis学习系列四--mybatis generator逆向工程
采用命令行方式执行逆向工程 1.配置文件generatorConfig.xml 保存在目录:D:\E\workspace\eclipse\mybatis_generator <?xmlversi ...
- 【MyBatis学习04】mapper代理方法开发dao
上一篇博文总结了mybatis使用 原始dao的方法存在的一些弊端,我们肯定不会去用它,那么mybatis中该如何开发dao呢?如题所述,这篇博文主要来总结一下使用mapper代理的方法来开发dao的 ...
- Mybaits 源码解析 (十一)----- 设计模式精妙使用:静态代理和动态代理结合使用:@MapperScan将Mapper接口生成代理注入到Spring
上一篇文章我们讲了SqlSessionFactoryBean,通过这个FactoryBean创建SqlSessionFactory并注册进Spring容器,这篇文章我们就讲剩下的部分,通过Mapper ...
随机推荐
- Oracle数据库补充
约束: 什么是约束以及约束的作用: 为保证数据的完整性(一致性,准确性),需要对数据进行限制,这个限制就叫做约束 目的:保证数据的完整性(一致性,正确性),使数据符合业务规则(业务逻辑) 约束 ...
- C#实现文件上传以及文件下载
public ActionResult Upload() { // var pathUrl = "http://" + Request.Url.Authority; var fil ...
- ABAP术语-Implementation
Implementation 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/22/1077076.html The concrete cod ...
- JavaScript--动态添加元素
在网页中,使用JavaScript动态创建元素的方式有三种: 1.document.write() 2.Element.innerHTML 3.document.createElement() 在上述 ...
- android Service服务(二)
1.1 活动和服务进行通信 上一节中我们学习了启动和停止服务的方法.不知道你又没有发现,虽然服务是在活动里启动的,但在启动了服务之后,活动和服务基本上就没关系了,确实如此,我们在活动里调用了start ...
- 一次 group by + order by 性能优化分析
一次 group by + order by 性能优化分析 最近通过一个日志表做排行的时候发现特别卡,最后问题得到了解决,梳理一些索引和MySQL执行过程的经验,但是最后还是有5个谜题没解开,希望大家 ...
- 二、Django需要的知识点
1.请求(request): 客户端到服务器端. 响应(response):服务器端到客户端. HTTP/1.1 协议共定义了 8 种请求方式,分别是: OPTIONS. HEAD. GET. POS ...
- webug学习(1)
webug的题目,比较简单,拿来巩固一哈. 1. 一看就知道是注入漏洞了,啥也不说sqlmap直接开炮. 先-u 之后-u 网址 --current-db 获取当前网址的数据库 所以当前数据库就是 p ...
- C语言实例解析精粹学习笔记——31
实例31: 判断字符串是否是回文 思路解析: 引入两个指针变量(head和tail),开始时,两指针分别指向字符串的首末字符,当两指针所指字符相等时,两指针分别向后和向前移动一个字符位置,并继续比较, ...
- FPGA算法学习(1) -- Cordic(圆周系统之向量模式)
旋转模式用来解决三角函数,实现极坐标到直角坐标的转换,基础理论请参考Cordic算法--圆周系统之旋转模式.那么,向量模式则用来解决反三角函数的问题,体现的应用主要是直角坐标向极坐标转换,即已知一点的 ...