mybatis(mybatis-plus)使用sql拦截器和自定义注解获取sql和参数

注解 SqlLogs

package com.ruoyi.common.annotation;

import java.lang.annotation.*;

/**
 * 获取sql注解
 *
 * @author ruoyi
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SqlLogs
{
    /**
     * 是否打印sql
     */
    public boolean hasSqlLog() default false;



}

sql拦截器 SqlLogsInterceptor

package com.ruoyi.framework.config;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.baomidou.mybatisplus.extension.handlers.AbstractSqlParserHandler;

import com.ruoyi.common.annotation.SqlLogs;

import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.baomidou.mybatisplus.extension.handlers.AbstractSqlParserHandler;
import com.ruoyi.common.annotation.SqlLogs;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.text.DateFormat;
import java.util.*;
import java.util.concurrent.locks.StampedLock;


@Slf4j
@AllArgsConstructor
//@Aspect
@Intercepts({
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
        @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})
})
@Component
public class SqlLogsInterceptor extends AbstractSqlParserHandler implements Interceptor {

    private DataSource dataSource;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Map<String,Object> sqlMap = new HashMap<>();
        Boolean hasSqlLogs = false;
        StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
        MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
        this.sqlParser(metaObject);
        // 先判断是不是SELECT操作 不是直接过滤
        MappedStatement mappedStatement =
                (MappedStatement) metaObject.getValue("delegate.mappedStatement");
        if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
            return invocation.proceed();
        }

        final Object[] args = invocation.getArgs();
        //获取执行方法的位置
        String namespace = mappedStatement.getId();
        //获取mapper名称
        String className = namespace.substring(0,namespace.lastIndexOf("."));
        //获取方法名
        String methedName= namespace.substring(namespace.lastIndexOf(".") + 1,namespace.length());
        //获取当前mapper 的方法
        Method[] ms = Class.forName(className).getMethods();
        for(Method m : ms){
            if(m.getName().equals(methedName)){
          //获取注解  来判断是不是要储存sql
                SqlLogs annotation = m.getAnnotation(SqlLogs.class);
                if(annotation != null){
                    hasSqlLogs = annotation.hasSqlLog();
                }
            }
        }
        //如果是有注解值为true,便获取sql处理参数
        if(hasSqlLogs){
            BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
            // 执行的SQL语句
            String originalSql = boundSql.getSql();
            // SQL语句的参数
            Object parameterObject = boundSql.getParameterObject();
            if(parameterObject != null){
                originalSql = showSql(configuration, boundSql);
            }
            System.err.println("sql :"+originalSql);
        }
        return invocation.proceed();

    }

    /**
     * 生成拦截对象的代理
     *
     * @param target 目标对象
     * @return 代理对象
     */
    @Override
    public Object plugin(Object target) {
        if (target instanceof StatementHandler) {
            return Plugin.wrap(target, this);
        }
        return target;
    }

    /**
     * 获取参数值
     * @param obj
     * @return
     */
    private static String getParameterValue(Object obj) {
        String value = null;
        if (obj instanceof String) {
            value = "'" + obj.toString() + "'";
        } else if (obj instanceof Date) {
            DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);
            value = "to_date('" + formatter.format(obj) + "','yyyy-MM-dd hh24:mi:ss')";
        } else {
            if (obj != null) {
                value = obj.toString();
            } else {
                value = "";
            }

        }
        System.err.println("获取值: "+value);
        return value;
    }

    /***
     * sql 参数替换
     * @param configuration
     * @param boundSql
     * @return
     */
    public static String showSql(Configuration configuration, BoundSql boundSql) {
        //获取参数对象
        Object parameterObject = boundSql.getParameterObject();
        //获取当前的sql语句有绑定的所有parameterMapping属性
        List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
        //去除空格
        String sql = boundSql.getSql().replaceAll("[\\s]+", " ");
        if (parameterMappings.size() > 0 && parameterObject != null) {
            TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
            /* 如果参数满足:org.apache.ibatis.type.TypeHandlerRegistry#hasTypeHandler(java.lang.Class<?>)
            org.apache.ibatis.type.TypeHandlerRegistry#TYPE_HANDLER_MAP
            * 即是不是属于注册类型(TYPE_HANDLER_MAP...等/有没有相应的类型处理器)
             * */
            if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
                sql = sql.replaceFirst("\\?", getParameterValue(parameterObject));
            } else {
                //装饰器,可直接操作属性值 ---》 以parameterObject创建装饰器
                //MetaObject 是 Mybatis 反射工具类,通过 MetaObject 获取和设置对象的属性值
                MetaObject metaObject = configuration.newMetaObject(parameterObject);
                //循环 parameterMappings 所有属性
                for (ParameterMapping parameterMapping : parameterMappings) {
                    //获取property属性
                    String propertyName = parameterMapping.getProperty();
                    System.err.println("propertyName: "+propertyName);
                    //是否声明了propertyName的属性和get方法
                    if (metaObject.hasGetter(propertyName)) {
                        Object obj = metaObject.getValue(propertyName);
                        sql = sql.replaceFirst("\\?", getParameterValue(obj));
                    } else if (boundSql.hasAdditionalParameter(propertyName)) {
                        //判断是不是sql的附加参数
                        Object obj = boundSql.getAdditionalParameter(propertyName);
                        sql = sql.replaceFirst("\\?", getParameterValue(obj));
                    }
                }
            }
        }
        return sql;
    }

}

在MybatisPlusConfig中注册bean

    @Bean
    @ConditionalOnMissingBean //有此实例便不进行注册
    public SqlLogsInterceptor dataScopeInterceptor(DataSource dataSource) {
        return new SqlLogsInterceptor(dataSource);
    }
 

在mapper中使用注解

    @SqlLogs(hasSqlLog = true)
    List<XgYyxxVo> selectByLqw(@Param(Constants.WRAPPER) Wrapper queryWrapper);

效果

在这里插入图片描述

相关推荐

参数相关解析
mybatis3.x源码解析

MyBatis-Plus是一个基于MyBatis的增强工具,提供了许多便捷的功能,其中包括数据拦截器(Data Interceptor)。 数据拦截器MyBatis-Plus提供的一个特性,用于在SQL语句执行前后对数据进行拦截和处理。通过数据拦截器,可以在执行SQL之前对参数进行修改,或者在执行SQL之后对结果进行处理。 使用数据拦截器可以实现一些常见的需求,比如对敏感字段进行加密解密、对某些特定条件进行数据过滤等。 要使用数据拦截器,首先需要创建一个实现了`com.baomidou.mybatisplus.core.plugins.Interceptor`接口的拦截器类。然后,在MyBatis的配置文件中配置该拦截器: ```xml <configuration> <plugins> <plugin interceptor="com.example.MyInterceptor"/> </plugins> </configuration> ``` 其中`com.example.MyInterceptor`是你自定义的拦截器类的全限定名。 在自定义的拦截器类中,你可以通过重写`intercept`方法来实现对SQL执行前后的处理逻辑。`intercept`方法接收一个`Invocation`对象作为参数,通过该对象可以获取SQL语句、参数等相关信息。 ```java public class MyInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { // 在SQL执行前的处理逻辑 // ... // 执行SQL Object result = invocation.proceed(); // 在SQL执行后的处理逻辑 // ... return result; } } ``` 需要注意的是,如果你使用的是Spring Boot,可以通过`@Bean`注解拦截器类注入到Spring容器中。如果是非Spring Boot项目,则需要在MyBatis的配置文件中显式配置拦截器。 以上就是使用MyBatis-Plus数据拦截器的基本介绍,希望对你有所帮助。如有更多问题,请继续提问。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值