MyBatis-Plus 插件-插件主體

2022-03-25 14:24 更新

注意

  • 版本要求:3.4.0 版本以上

MybatisPlusInterceptor

該插件是核心插件,目前代理了 ?Executor#query? 和 ?Executor#update? 和 ?StatementHandler#prepare? 方法

屬性

  • ?private List interceptors = new ArrayList<>();?

InnerInterceptor

我們提供的插件都將基于此接口來(lái)實(shí)現(xiàn)功能

目前已有的功能:

  • 自動(dòng)分頁(yè): ?PaginationInnerInterceptor ?
  • 多租戶(hù): ?TenantLineInnerInterceptor ?
  • 動(dòng)態(tài)表名: ?DynamicTableNameInnerInterceptor ?
  • 樂(lè)觀(guān)鎖: ?OptimisticLockerInnerInterceptor ?
  • sql 性能規(guī)范: ?IllegalSQLInnerInterceptor ?
  • 防止全表更新與刪除: ?BlockAttackInnerInterceptor?

注意:

使用多個(gè)功能需要注意順序關(guān)系,建議使用如下順序

  • 多租戶(hù),動(dòng)態(tài)表名
  • 分頁(yè),樂(lè)觀(guān)鎖
  • sql 性能規(guī)范,防止全表更新與刪除

總結(jié): 對(duì) sql 進(jìn)行單次改造的優(yōu)先放入,不對(duì) sql 進(jìn)行改造的最后放入

使用方式(以分頁(yè)插件舉例)

spring

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
    <!-- 其他屬性 略 -->
    <property name="configuration" ref="configuration"/>
    <property name="plugins">
        <array>
            <ref bean="mybatisPlusInterceptor"/>
        </array>
    </property>
</bean>

<bean id="configuration" class="com.baomidou.mybatisplus.core.MybatisConfiguration">
    <!-- 需配置該值為false,避免1或2級(jí)緩存可能出現(xiàn)問(wèn)題,該屬性會(huì)在舊插件移除后一同移除 -->
    <property name="useDeprecatedExecutor" value="false"/>
</bean>

<bean id="mybatisPlusInterceptor" class="com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor">
    <property name="interceptors">
        <list>
            <ref bean="paginationInnerInterceptor"/>
        </list>
    </property>
</bean>

<bean id="paginationInnerInterceptor" class="com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor">
    <!-- 對(duì)于單一數(shù)據(jù)庫(kù)類(lèi)型來(lái)說(shuō),都建議配置該值,避免每次分頁(yè)都去抓取數(shù)據(jù)庫(kù)類(lèi)型 -->
    <constructor-arg name="dbType" value="H2"/>
</bean>

spring-boot

@Configuration
@MapperScan("scan.your.mapper.package")
public class MybatisPlusConfig {

    /**
     * 新的分頁(yè)插件,一緩和二緩遵循mybatis的規(guī)則,需要設(shè)置 MybatisConfiguration#useDeprecatedExecutor = false 避免緩存出現(xiàn)問(wèn)題(該屬性會(huì)在舊插件移除后一同移除)
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }
}

mybatis-config.xml

<plugins>
  <plugin interceptor="com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor">
    <property name="@page" value="com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor"/>
    <property name="page:dbType" value="h2"/>
  </plugin>
</plugins>

?property的配置說(shuō)明詳見(jiàn) ?MybatisPlusInterceptor#setProperties? 的源碼方法注釋

攔截忽略注解 @InterceptorIgnore

 屬性名  類(lèi)型  默認(rèn)值  描述
 tenantLine  String  ""  行級(jí)租戶(hù)
 dynamicTableName  String  ""  動(dòng)態(tài)表名
 blockAttack  String  ""  攻擊 SQL 阻斷解析器,防止全表更新與刪除
 illegalSql  String  ""  垃圾 SQL 攔截

該注解作用于 ?xxMapper.java? 方法之上 各屬性代表對(duì)應(yīng)的插件 各屬性不給值則默認(rèn)為 ?false設(shè)置為 ?true忽略攔截 更多說(shuō)明詳見(jiàn)源碼注釋


以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)