自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一篇聊聊Mybatis插件開發(fā)

開發(fā) 前端
允許在 plugin 元素中配置所需參數(shù),方法在插件初始化的時(shí)候就被調(diào)用了一次,然后把插件對(duì)象存入到配置中,以便后面再取出。

Mybatis的插件,主要用于在執(zhí)行sql前后,對(duì)sql進(jìn)行封裝加工,或者在sql執(zhí)行后,對(duì)數(shù)據(jù)進(jìn)行加工處理。常用于一些公共數(shù)據(jù)操作處理,例如:

  1. 分頁插件,在執(zhí)行sql查詢前增加分頁參數(shù)
  2. 多租戶系統(tǒng)中,增加租戶ID參數(shù)。
  3. 增加更新時(shí)間、創(chuàng)建時(shí)間、更新人、創(chuàng)建人的參數(shù)信息。
  4. 數(shù)據(jù)權(quán)限中,增加參數(shù)查詢。

插件開發(fā)過程

確定需要攔截的簽名

指定需要攔截的方法,通過方法簽名來指定,方法簽名即指定哪個(gè)類的哪個(gè)方法+方法參數(shù)。這里的類不能隨便寫,只能從以下幾個(gè)類中選,也就是說,MyBatis 插件可以攔截四大對(duì)象中的任意一個(gè)。

  • Executor 是執(zhí)行 SQL 的全過程,包括組裝參數(shù),組裝結(jié)果集返回和執(zhí)行 SQL 過程,都可以攔截。
  • StatementHandler 是執(zhí)行 SQL 的過程,我們可以重寫執(zhí)行 SQL 的過程。
  • ParameterHandler 是攔截執(zhí)行 SQL 的參數(shù)組裝,我們可以重寫組裝參數(shù)規(guī)則。
  • ResultSetHandler 用于攔截執(zhí)行結(jié)果的組裝,我們可以重寫組裝結(jié)果的規(guī)則。

我們來看以下mybatisplus的插件配置的簽名:

@Intercepts(
    {
        @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}),
        @Signature(type = StatementHandler.class, method = "getBoundSql", args = {}),
        @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}),
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
    }
)
public class MybatisPlusInterceptor implements Interceptor {
//...
}

type指定四大類型中的任意一個(gè),method指定攔截類型中方法,args指定方法參數(shù)。例如:

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

指定了攔截StatementHandler的prepare方法,方法有兩個(gè)參數(shù),一個(gè)是Connection類型,另一個(gè)是Integer類型。

public interface StatementHandler {

  Statement prepare(Connection connection, Integer transactionTimeout)
      throws SQLException;
      
      //....
      }

插件接口定義

在 MyBatis 中開發(fā)插件,需要實(shí)現(xiàn) Interceptor 接口。接口的定義如下:

public interface Interceptor {
 
  Object intercept(Invocation invocation) throws Throwable;
 
  Object plugin(Object target);
 
  void setProperties(Properties properties);
 
}
  • intercept 方法:它將直接覆蓋你所攔截對(duì)象原有的方法,因此它是插件的核心方法。通過 invocation 參數(shù)可以反射調(diào)度原來對(duì)象的方法。
  • plugin 方法:target 是被攔截對(duì)象,它的作用是給被攔截對(duì)象生成一個(gè)代理對(duì)象,并返回它。為了方便 MyBatis 使用 org.apache.ibatis.plugin.Plugin 中的 wrap 靜態(tài)方法提供生成代理對(duì)象。
  • setProperties 方法:允許在 plugin 元素中配置所需參數(shù),方法在插件初始化的時(shí)候就被調(diào)用了一次,然后把插件對(duì)象存入到配置中,以便后面再取出。

實(shí)現(xiàn)插件

創(chuàng)建個(gè)類實(shí)現(xiàn)Interceptor接口,并且在實(shí)現(xiàn)類上指定方法簽名即可。

最后需要在mybatis配置文件中配置插件

<plugins>
        <plugin interceptor="com.yjw.demo.mybatis.common.page.PageInterceptor">
        </plugin>
    </plugins>

最后建議看一下MybatisPlusInterceptor的實(shí)現(xiàn),里面還使用到了責(zé)任鏈設(shè)計(jì)模式。

責(zé)任編輯:武曉燕 來源: 今日頭條
相關(guān)推薦

2023-07-31 07:48:43

Java內(nèi)存虛擬機(jī)

2023-08-03 07:34:34

格式化字符串參數(shù)

2022-06-02 07:11:13

JVMJava

2021-10-30 07:55:00

BLE 藍(lán)牙開發(fā)

2023-12-08 08:26:05

數(shù)據(jù)存儲(chǔ)持久性

2021-11-15 07:47:40

字符串位置存儲(chǔ)

2022-10-08 15:07:06

ChatOps運(yùn)維

2021-04-16 07:46:13

Serverless 云開發(fā)FaaS

2021-07-12 06:11:14

SkyWalking 儀表板UI篇

2022-10-26 07:39:36

MVCC數(shù)據(jù)庫RR

2022-12-19 08:14:30

注解開發(fā)配置

2022-01-02 08:43:46

Python

2021-05-20 06:57:16

RabbitMQ開源消息

2023-04-20 08:00:00

ES搜索引擎MySQL

2022-02-07 11:01:23

ZooKeeper

2020-11-20 10:15:05

TensorFlow

2022-03-18 07:48:58

GhostNode.js開源

2021-11-03 14:49:20

開發(fā)摸魚側(cè)邊欄

2022-10-20 18:00:00

MyBatis緩存類型

2021-02-26 11:54:38

MyBatis 插件接口
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)