mybatis--基础--2.6--xml配置--插件(plugins)

mybatis–基础–2.6–xml配置–插件(plugins)


代码地址

https://gitee.com/DanShenGuiZu/learnDemo/tree/mysql_mybaties_DB/mybatis-learn-master

1、插件(plugins)

  1. 允许你对映射语句执行过程中的某一点进行拦截调用。
  2. 默认情况下,MyBatis允许使用插件来拦截方法调用
  3. 试图修改或重写已有方法的行为时,很可能会破坏MyBatis的核心模块,使用插件的时候要特别当心。
  4. 除了用插件来修改MyBatis核心行为以外,还可以继承配置类后覆盖其中的某个方法,再把它传递到SqlSessionFactoryBuilder.build(myConfig)方法即可。这可能会极大影响MyBatis的行为,务请慎之又慎。

2、拦截方法

2.1、Executor

  1. update
  2. query
  3. flushStatements
  4. commit
  5. rollback
  6. getTransaction
  7. close
  8. isClosed

2.2、ParameterHandler

  1. getParameterObject
  2. setParameters

2.3、ResultSetHandler

  1. handleResultSets
  2. handleOutputParameters

2.4、StatementHandler

  1. prepare
  2. parameterize
  3. batch
  4. update

3、案例

3.1、核心代码

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述


package com.cl.mybatis.learn.day08;

import java.sql.Connection;
import java.util.Properties;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.plugin.*;

@Intercepts({
		@Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class })})

public class ExamplePlugin implements Interceptor {
	@Override
	public Object intercept(Invocation invocation)throws Throwable {
		StatementHandler statementHandler =(StatementHandler)invocation.getTarget();
		BoundSql boundSql = statementHandler.getBoundSql();
		String sql = boundSql.getSql();
		System.out.println("拦截的 sql::" + sql);
		return invocation.proceed();
	}
	
	@Override
	public Object plugin(Object target){
		return Plugin.wrap(target, this);
	}
	
	@Override
	public void setProperties(Properties properties){
		String dialect = properties.getProperty("dialect");
		System.out.println("获取的属性为::" + dialect);
	}
}

<plugins>
	<plugin interceptor="com.cl.mybatis.learn.day08.ExamplePlugin">
		<property name="dialect" value="mysql" />
	</plugin>
</plugins>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值