github:https://github.com/nlxs0511/springmybatisplus.git
1.新建MybatisInterceptor
package com.test.mybatisplus.Interceptor;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.plugin.*;
import java.sql.Connection;
import java.util.List;
import java.util.Properties;
/**
* @Description mybatis自定义插件 mybatis只能拦截 Executor StatementHandler ParameterHandler ResultSetHandler
* @project
* @author:hf
* @date:
*/
@Intercepts({@Signature(
type = StatementHandler.class,
method = "prepare",
args = {Connection.class, Integer.class}
)})
public class MybatisInterceptor implements Interceptor {
private String name;
/**
* @param invocation
* @return
* @throws Throwable
*/
@Override
public Object intercept(Invocation invocation) throws Throwable {
//通过StatementHandler获取执行的sql
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
BoundSql boundSql = statementHandler.getBoundSql();
// 获取sql
String sql = boundSql.getSql();
List<ParameterMapping> list=boundSql.getParameterMappings();
long start = System.currentTimeMillis();
Object proceed = invocation.proceed();
long end = System.currentTimeMillis();
System.out.println(name+",您的本次数据库操作时间为::" + (end - start)+"ms");
return proceed;
}
/** 获取到拦截的对象,底层也是通过代理实现的,实际上是拿到一个目标代理对象 */
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
/**
* 获取设置的阈值等参数
*/
@Override
public void setProperties(Properties properties) {
this.name =properties.getProperty("name");
}
}
2.新建MybatisPluConfig
package com.test.mybatisplus.config;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.test.mybatisplus.Interceptor.MybatisInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
//@MapperScan("com.test.mybatisplus.mapper")
public class MybatisPluConfig {
@Value("${MybatisInterceptor.name}")
String name;
/**
* mybatis-plus分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
/**
*自定义mybatis插件 注入方式一
*/
@Bean
public MybatisInterceptor mybatisInterceptor() {
MybatisInterceptor mybatisInterceptor = new MybatisInterceptor();
Properties properties = new Properties();
properties.setProperty("name", name);
mybatisInterceptor.setProperties(properties);
return mybatisInterceptor;
}
/**
*自定义mybatis插件 注入方式二
*/
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return new ConfigurationCustomizer() {
@Override
public void customize(org.apache.ibatis.session.Configuration configuration) {
//插件拦截链采用了责任链模式,执行顺序和加入连接链的顺序有关
MybatisInterceptor mybatisInterceptor = new MybatisInterceptor();
//设置参数,比如阈值等,可以在配置文件中配置,这里直接写死便于测试
Properties properties = new Properties();
properties.setProperty("name", name);
mybatisInterceptor.setProperties(properties);
configuration.addInterceptor(mybatisInterceptor);
}
};
}
/**
* mybatis-plus SQL执行效率插件【生产环境可以关闭】 3.3.2版本移除了该功能,3.0.3和3.0.3之前版本支持。
*/
/* @Bean
public PerformanceInterceptor performanceInterceptor() {
return new PerformanceInterceptor();
}*/
}
方式三:不用新建MybatisPluConfig ,直接在 MybatisInterceptor 中加上@Component