MybatisPlus 之 插件

1、mybatis的插件机制

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

1. Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
2. ParameterHandler (getParameterObject, setParameters)
3. ResultSetHandler (handleResultSets, handleOutputParameters)
4. StatementHandler (prepare, parameterize, batch, update, query)

        我们看到了可以拦截Executor接口的部分方法,比如update,query,commit,rollback等方法,还有其他接口的一些方法等。

总体概括为:

  1. 拦截执行器的方法
  2. 拦截参数的处理
  3. 拦截结果集的处理
  4. 拦截Sql语法构建的处理

拦截器示例: 

    @Intercepts({@Signature(
            type= Executor.class,
            method = "update",
            args = {MappedStatement.class,Object.class})})
    public class MyInterceptor implements Interceptor {
        @Override
        public Object intercept(Invocation invocation) throws Throwable {
//拦截⽅法,具体业务逻辑编写的位置
            return invocation.proceed();
        }
        @Override
        public Object plugin(Object target) {
//创建target对象的代理对象,⽬的是将当前拦截器加⼊到该对象中
            return Plugin.wrap(target, this);
        }
        @Override
        public void setProperties(Properties properties) {
//属性设置
        }
    }

注入到Spring容器:

/**
* ⾃定义拦截器
*/
@Bean
public MyInterceptor myInterceptor(){
    return new MyInterceptor();
}

或者通过xml配置,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>
    <plugins>
        <plugin interceptor="com.lagou.mp.plugins.MyInterceptor"></plugin>
    </plugins>
</configuration>

2、执行分析插件

        在MP中提供了对SQL执行的分析的插件,可用作阻断全表更新、删除的操作,注意:该插件仅适用于开发环境,不适用于生产环境。

SpringBoot配置: 

   @Bean
    public SqlExplainInterceptor sqlExplainInterceptor(){
        SqlExplainInterceptor sqlExplainInterceptor = new SqlExplainInterceptor();
        List<ISqlParser> sqlParserList = new ArrayList<>();
    // 攻击 SQL 阻断解析器、加⼊解析链
        sqlParserList.add(new BlockAttackSqlParser());
        sqlExplainInterceptor.setSqlParserList(sqlParserList);
        return sqlExplainInterceptor;
    }

测试:

    @Test
    public void testUpdate(){
        User user = new User();
        user.setAge(20);
        int result = this.userMapper.update(user, null);
        System.out.println("result = " + result);
    }

结果:

        可以看到,当执行全表更新时,会抛出异常,这样有效防止了一些误操作。

3、性能分析插件

        性能分析拦截器,用于输出每条 SQL 语句及其执行时间,可以设置最大执行时间,超过时间会抛出异常。 (该插件只用于开发环境,不建议生产环境使用。

配置: 

        javaconfig方式

   /*
        性能分析插件
    */

   @Bean
   public PerformanceInterceptor performanceInterceptor(){

       PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        // 设置sql语句的最大执行时间
       performanceInterceptor.setMaxTime(100);
       // 设置sql是否格式化显示
       performanceInterceptor.setFormat(true);

       return performanceInterceptor;
   }

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>
    <plugins>
    <!-- SQL 执⾏性能分析,开发环境使⽤,线上不推荐。 maxTime 指的是 sql 最⼤执⾏时⻓ -->
        <plugin
 interceptor="com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor">
            <property name="maxTime" value="100" />
            <!--SQL是否格式化 默认false-->
            <property name="format" value="true" />
        </plugin>
    </plugins>
</configuration>

执行结果:

可以看到,执行时间为11ms。如果将maxTime设置为1,那么,该操作会抛出异常。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MybatisPlus Idea插件是一款针对Java开发者,基于IntelliJ IDEA开发MybatisPlus增强插件。 该插件的出现,旨在为Java开发者提供更加便捷、高效的MybatisPlus使用体验。 MybatisPlus是基于Mybatis进行一系列增强,比如自动生成基础代码,更加丰富的查询、更新、删除操作等。这使得MybatisPlus在实际开发中被广泛应用。MybatisPlus Idea插件的出现,让Java开发者在使用MybatisPlus时,无需手动编写基础代码。通过简单的配置,即可轻松生成Mapper、Entity、Service、Controller等基础代码,节省了大量时间和精力。 MybatisPlus Idea插件的功能非常丰富,具有如下特点: 1、快速生成基础代码,为Java开发者提供生成Mapper、Entity、Service、Controller等基础代码的功能,省去了重复劳动。 2、提供高级代码生成模版,能够自定义生成模板。通过模板,用户可以自定义生成的代码,满足不同的需求。 3、具有智能提示功能,通过智能提示功能,用户可以快速获取Mapper、Entity、Service、Controller等类及其方法,并快速但准确地编写代码。 4、自动注入配置信息,可以自动注入MybatisPlus的配置文件,进一步提高开发效率。 总之,MybatisPlus Idea插件极大提高了开发人员的工作效率,让开发者更加专注于业务逻辑的开发。如果您正在使用MybatisPlus开发Java项目,那么这是一款不可错过的插件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悠然予夏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值