今天看了别人的mybatis的教学视频,自己手写了一个简单的自定义的插件,有些细节记录一下。

先看下mybatis的插件的一些说明:

MyBatis 允许你在已映射语句执行过程中的某一点进行拦截调用。默认情况下,MyBatis 允许使用插件来拦截的方法调用包括:

Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)          --执行sql

ParameterHandler (getParameterObject, setParameters)                     --获取、设置参数

ResultSetHandler (handleResultSets, handleOutputParameters)          --处理结果集

StatementHandler (prepare, parameterize, batch, update, query)          --记录sql

这里需要注意的是,这4个类型是固定的,里面的方法也是固定的,不能再被改变的,具体的信息,可以相关的类(Executor.class、ParameterHandler .class、ResultSetHandler .class、StatementHandler .class)查看。

-----------------------------------------------------------------------------------------------------

先定义一个插件拦截器,代码如下:

package com.drafire.testall.interceptor;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*; import java.util.Properties; /**
* mybatis 自定义插件
*/
@Intercepts(value = {@Signature(
type= Executor.class, //这里对应4个类
method = "update", //这里对应4个类里面的参数
args = {MappedStatement.class,Object.class})}) //这里的参数类型,是对应4个类中的各种方法的参数。如果方法没有参数,这里直接写{}就可以了
public class DrafirePlugin implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
System.out.println("mybatis插件打印了乐乐");
return invocation.proceed();
} @Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
} @Override
public void setProperties(Properties properties) { }
}

配置mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeHandlers>
<typeHandler handler="com.drafire.testall.handler.DrafireStringHandler"></typeHandler>
</typeHandlers> <plugins>
<plugin interceptor="com.drafire.testall.interceptor.DrafirePlugin">
</plugin>
</plugins> <environments default="development">
<environment id="sell">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${ds.sell.driverClassName}"/>
<property name="url" value="${ds.sell.url}"/>
<property name="username" value="${ds.sell.username}"/>
<property name="password" value="${ds.sell.password}"/>
</dataSource>
</environment> <environment id="bank">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${ds.bank.driverClassName}"></property>
<property name="url" value="${ds.bank.url}"></property>
<property name="username" value="${ds.bank.username}"></property>
<property name="password" value="${ds.bank.password}"></property>
</dataSource>
</environment>
</environments>
<mappers>
<!--<mapper resource="org/mybatis/example/BlogMapper.xml"/>-->
</mappers>
</configuration>

测试代码如下,测试

package com.drafire.testall.Sevice;

import com.drafire.testall.model.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest
@RunWith(SpringRunner.class)
public class UserServiceTest { @Autowired
private UserService userService; //@Test
public void addUser() {
User user=new User();
user.setId(1);
user.setAmount(110L);
user.setName("李四");
userService.add(user);
} @Test
public void updateUser(){
User user=new User();
user.setId(1);
user.setAmount(50L);
user.setName("王五123");
userService.update(user);
}
}

通过

mybatis 自定义插件的使用的更多相关文章

  1. Mybatis框架(9)---Mybatis自定义插件生成雪花ID做为表主键项目

    Mybatis自定义插件生成雪花ID做为主键项目 先附上项目项目GitHub地址 spring-boot-mybatis-interceptor 有关Mybatis雪花ID主键插件前面写了两篇博客作为 ...

  2. mybatis自定义插件(拦截器)开发详解

    mybatis插件(准确的说应该是around拦截器,因为接口名是interceptor,而且invocation.proceed要自己调用,配置中叫插件)功能非常强大,可以让我们无侵入式的对SQL的 ...

  3. mybatis自定义插件动态修改sql语句

    step1:定义Interceptor实现org.apache.ibatis.plugin.Interceptor import org.apache.commons.logging.Log; imp ...

  4. 浅析MyBatis(三):聊一聊MyBatis的实用插件与自定义插件

    在前面的文章中,笔者详细介绍了 MyBatis 框架的底层框架与运行流程,并且在理解运行流程的基础上手写了一个自己的 MyBatis 框架.看完前两篇文章后,相信读者对 MyBatis 的偏底层原理和 ...

  5. SSM 使用 mybatis 分页插件 pagehepler 实现分页

    使用分页插件的原因,简化了sql代码的写法,实现较好的物理分页,比写一段完整的分页sql代码,也能减少了误差性. Mybatis分页插件 demo 项目地址:https://gitee.com/fre ...

  6. 2017.12.25 Mybatis物理分页插件PageHelper的使用(二)

    参考来自: 官方文档的说明:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md 上篇博客 ...

  7. mybatis的插件机制

    一.mybatis的插件介绍 关于mybatis的插件,我想大家也都用过,就比如最常用的逆向工程,根据表结构生成model,dao,xml文件,还有分页插件,那这些插件的工作原理是怎么样的呢,就比如分 ...

  8. 一、mybatis的插件介绍

    摘自:https://www.cnblogs.com/qm-article/p/11785350.html mybatis的插件机制   一.mybatis的插件介绍 关于mybatis的插件,我想大 ...

  9. mybatis分页插件PageHelp的使用

    1.简介 ​ PageHelper 是国内非常优秀的一款开源的 mybatis 分页插件,它支持基本主流与常用的数据库,例如 mysql.oracle.mariaDB.DB2.SQLite.Hsqld ...

随机推荐

  1. [c语言]左移和右移

    左移和右移都是位运算的概念.我们知道计算机是基于二进制保存数据的,因此左移和右移的概念十分重要.本文约定是32位的机器. [左移] 丢弃最高位,0补最低位 左移是把一个数按照二进制每位向左移动若干位, ...

  2. 【C# .Net GC】条件自动垃圾回收 HandleCollector类

    条件自动回收 达到指定条件后自动执行GC回收垃圾. GC中的方法AddMemoryPressure和RemoveMemoryPressure 本机资源有时会消耗大量内存,但用于包装它的托管对象只占用很 ...

  3. windbg调试命令

    重要 (1) windbg命令分为标准命令(40个左右),元命令(一百多个)和扩展命令. 标准命令提供最基本的调试功能,不区分大小写.如:bp g dt dv k等 元命令提供标准命令没有提供的功能, ...

  4. 【基础知识】CPU指令周期

    完整执行一条指令所需要的时间 基本概念 指令周期,读取-执行周期(fetch-and-execute cycle)是指CPU要执行指令经过的步骤. 计算机之所以能自动地工作,是因为CPU能从存放程序的 ...

  5. spring 中<ref parent="">标签是什么意思;ref标签与ref属性有什么不同;子容器如何引用父容器的bean

    spring的配置文件可能会有多个<property name="a" ref="b" />就是找当前配置文件里的bean 也就是id为b的 < ...

  6. appium1-macOS10.12下如何丝滑的使用appium?

    1.下载或者更新Homebrew:homebrew官网 macOS 不可或缺的套件管理器 $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githu ...

  7. WPF关于绑定与更新修改

    看到一些资料与教程视频,在这里记录一下, 首先 我们先做好一个公共的INotifyPropertyChanged事件,也就是通知更新 public class ViewModelBase : INot ...

  8. Java学习笔记:02面向对象-重写_this_super_抽象类

    02面向对象-重写/this/super/抽象类 ****1.this和super 作用: this: 区分本类的成员变量和局部变量同名情况 super:区分父类的成员变量和局部变量同名情况 用法: ...

  9. 用两行代码实现重试功能,spring-retry真是简单而优雅

    背景 最近做的一个需求,需要调用第三方接口.正常情况下,接口的响应是符合要求的,只有在网络抖动等极少数的情况下,会存在超时情况.因为是小概率事件,所以一次超时之后,进行一次重试操作应该就可以了.重试很 ...

  10. Mariadb开启密码复杂度

    mariadb开启密码复杂度 #安装插件# INSTALL SONAME 'simple_password_check'; #设置输入错误多少次锁定# set global max_password_ ...