首页 > 其他分享 >Springboot 自定义mybatis 拦截器,实现我们要的扩展

Springboot 自定义mybatis 拦截器,实现我们要的扩展

时间:2022-11-07 18:05:07浏览次数:61  
标签:拦截器 Springboot 自定义 MappedStatement builder boundSql ms import class


前言

相信大家对拦截器并不陌生,对mybatis也不陌生。

有用过pagehelper的,那么对mybatis拦截器也不陌生了,按照使用的规则触发sql拦截,帮我们自动添加分页参数 。

那么今天,我们的实践 自定义mybatis拦截器也是如此, 本篇文章实践的效果:


针对一些使用 单个实体类去接收返回结果的 mapper方法,我们拦截检测,如果没写 LIMIT 1 ,我们将自动帮忙填充,达到查找单条数据 效率优化的效果。

ps: 当然,跟着该篇学会了这个之后,那么可以扩展的东西就多了,大家按照自己的想法或是项目需求都可以自己发挥。 我该篇的实践仅仅作为抛砖引玉吧。

正文

实践的准备 :
 

整合mybatis ,然后故意写了3个查询方法, 1个是list 列表数据,2个是 单条数据 。

Springboot 自定义mybatis 拦截器,实现我们要的扩展_intellij-idea

我们通过自己写一个MybatisInterceptor实现 mybatis框架的 Interceptor来做文章:
 

MybatisInterceptor.java :

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.*;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.*;



/**
* @Author JCccc
* @Description
* @Date 2021/12/14 16:56
*/
@Component
@Intercepts({

@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})
})
public class MybatisInterceptor implements Interceptor {

@Override
public Object intercept(Invocation invocation) throws Throwable {
//获取执行参数
Object[] objects = invocation.getArgs();
MappedStatement ms = (MappedStatement) objects[0];
//解析执行sql的map方法,开始自定义规则匹配逻辑
String mapperMethodAllName = ms.getId();
int lastIndex = mapperMethodAllName.lastIndexOf(".");
String mapperClassStr = mapperMethodAllName.substring(0, lastIndex);
String mapperClassMethodStr = mapperMethodAllName.substring((lastIndex + 1));
Class<?> mapperClass = Class.forName(mapperClassStr);
Method[] methods = mapperClass.getMethods();
Class<?> returnType;
for (Method method : methods) {
if (method.getName().equals(mapperClassMethodStr)) {
returnType = method.getReturnType();
if (returnType.isAssignableFrom(List.class)) {
System.out.println("返回类型是 List");
System.out.println("针对List 做一些操作");
} else if (returnType.isAssignableFrom(Set.class)) {
System.out.println("返回类型是 Set");
System.out.println("针对Set 做一些操作");
} else{
BoundSql boundSql = ms.getSqlSource().getBoundSql(objects[1]);
String oldSql = boundSql.getSql().toLowerCase(Locale.CHINA).replace("[\\t\\n\\r]", " ");
if (!oldSql.contains("LIMIT")){
String newSql = boundSql.getSql().toLowerCase(Locale.CHINA).replace("[\\t\\n\\r]", " ") + " LIMIT 1";
BoundSql newBoundSql = new BoundSql(ms.getConfiguration(), newSql,
boundSql.getParameterMappings(), boundSql.getParameterObject());
MappedStatement newMs = newMappedStatement(ms, new MyBoundSqlSqlSource(newBoundSql));
for (ParameterMapping mapping : boundSql.getParameterMappings()) {
String prop = mapping.getProperty();
if (boundSql.hasAdditionalParameter(prop)) {
newBoundSql.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop));
}
}
Object[] queryArgs = invocation.getArgs();
queryArgs[0] = newMs;
System.out.println("打印新SQL语句" + newSql);
}

}
}
}
//继续执行逻辑
return invocation.proceed();
}


@Override
public Object plugin(Object o) {
//获取代理权
if (o instanceof Executor) {
//如果是Executor(执行增删改查操作),则拦截下来
return Plugin.wrap(o, this);
} else {
return o;
}
}

/**
* 定义一个内部辅助类,作用是包装 SQL
*/
class MyBoundSqlSqlSource implements SqlSource {
private BoundSql boundSql;

public MyBoundSqlSqlSource(BoundSql boundSql) {
this.boundSql = boundSql;
}

@Override
public BoundSql getBoundSql(Object parameterObject) {
return boundSql;
}

}

private MappedStatement newMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
MappedStatement.Builder builder = new
MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType());
builder.resource(ms.getResource());
builder.fetchSize(ms.getFetchSize());
builder.statementType(ms.getStatementType());
builder.keyGenerator(ms.getKeyGenerator());
if (ms.getKeyProperties() != null && ms.getKeyProperties().length > 0) {
builder.keyProperty(ms.getKeyProperties()[0]);
}
builder.timeout(ms.getTimeout());
builder.parameterMap(ms.getParameterMap());
builder.resultMaps(ms.getResultMaps());
builder.resultSetType(ms.getResultSetType());
builder.cache(ms.getCache());
builder.flushCacheRequired(ms.isFlushCacheRequired());
builder.useCache(ms.isUseCache());
return builder.build();
}


@Override
public void setProperties(Properties properties) {
//读取mybatis配置文件中属性
}


}

简单代码端解析:

① 拿出执行参数,里面有很多东西给我们用的,我本篇主要是用MappedStatement。大家可以发挥自己的想法。 打debug可以自己看看里面的东西。

//获取执行参数 Object[] objects = invocation.getArgs(); MappedStatement ms = (MappedStatement) objects[0];

Springboot 自定义mybatis 拦截器,实现我们要的扩展_java_02

 

Springboot 自定义mybatis 拦截器,实现我们要的扩展_apache_03

 ② 这里我主要是使用了从MappedStatement里面拿出来的id,做切割分别切出来 目前执行的sql的mapper是哪个,然后方法是哪个。

String mapperMethodAllName = ms.getId(); int lastIndex = mapperMethodAllName.lastIndexOf("."); String mapperClassStr = mapperMethodAllName.substring(0, lastIndex); String mapperClassMethodStr = mapperMethodAllName.substring((lastIndex + 1));

③ 这就是我自己随便写的一下,我直接根据切割出来的mapper类找出里面的方法,主要是为了拿出每个方法的返回类型,因为 我这篇实践内容就是,判断出单个pojo接受的mapper方法,加个LIMIT 1 , 其他的 LIST 、SET 、 Page 等等这些,我暂时不做扩展。

Class<?> mapperClass = Class.forName(mapperClassStr); Method[] methods = mapperClass.getMethods(); Class<?> returnType;

 ④这一段代码就是该篇的拓展逻辑了,从MappedStatement里面拿出 SqlSource,然后再拿出BoundSql,然后一顿操作 给加上 LIMIT 1  , 然后new 一个新的 MappedStatement,一顿操作塞回去invocation 里面。

BoundSql boundSql = ms.getSqlSource().getBoundSql(objects[1]); String oldSql = boundSql.getSql().toLowerCase(Locale.CHINA).replace("[\\t\\n\\r]", " "); if (!oldSql.contains("LIMIT")){ String newSql = boundSql.getSql().toLowerCase(Locale.CHINA).replace("[\\t\\n\\r]", " ") + " LIMIT 1"; BoundSql newBoundSql = new BoundSql(ms.getConfiguration(), newSql, boundSql.getParameterMappings(), boundSql.getParameterObject()); MappedStatement newMs = newMappedStatement(ms, new MyBoundSqlSqlSource(newBoundSql)); for (ParameterMapping mapping : boundSql.getParameterMappings()) { String prop = mapping.getProperty(); if (boundSql.hasAdditionalParameter(prop)) { newBoundSql.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop)); } } Object[] queryArgs = invocation.getArgs(); queryArgs[0] = newMs; System.out.println("打印新SQL语句:" + newSql); }

OK,最后简单来试试效果 :
 

Springboot 自定义mybatis 拦截器,实现我们要的扩展_intellij-idea_04

 

Springboot 自定义mybatis 拦截器,实现我们要的扩展_intellij-idea_05

最后再重申一下,本篇文章内容只是一个抛砖引玉,随便想的一些实践效果,大家可以按照自己的想法发挥。

最后再补充一个 解决 mybatis自定义拦截器和 pagehelper 拦截器 冲突导致失效的问题出现的 解决方案:

加上一个拦截器配置类 

MyDataSourceInterceptorConfig.java :

import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
* @Author JCccc
* @Description
* @Date 2021/12/14 16:56
*/
@Component
public class MyDataSourceInterceptorConfig implements ApplicationListener<ContextRefreshedEvent> {

@Autowired
private MybatisInterceptor mybatisInterceptor;

@Autowired
private List<SqlSessionFactory> sqlSessionFactories;

@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
for (SqlSessionFactory factory : sqlSessionFactories) {
factory.getConfiguration().addInterceptor(mybatisInterceptor);
}
}
}

最后最后再补充多一些,

文中 针对拦截的是Executor 这种接口的插件,

其实 还可以使用ParameterHandler、ResultSetHandler、StatementHandler。

每当执行这四种接口对象的方法时,就会进入拦截方法,然后我们可以根据不同的插件去拿不同的参数。

类似:

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

然后可以做个转换,也是可以取出相关的 BoundSql 等等 :

if (!(invocation.getTarget() instanceof RoutingStatementHandler)){
return invocation.proceed();
}
RoutingStatementHandler statementHandler = (RoutingStatementHandler) invocation.getTarget();
BoundSql boundSql = statementHandler.getBoundSql();
String sql = boundSql.getSql().toUpperCase();

ParameterHandler、ResultSetHandler、StatementHandler、Executor ,不同的插件拦截,有不同的使用场景,想深入的看客们,可以深一下。

标签:拦截器,Springboot,自定义,MappedStatement,builder,boundSql,ms,import,class
From: https://blog.51cto.com/u_15753094/5830336

相关文章