AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

一篇聊聊Mybatis插件開發

來源: 責編: 時間:2023-09-21 20:48:26 285觀看
導讀Mybatis的插件,主要用于在執行sql前后,對sql進行封裝加工,或者在sql執行后,對數據進行加工處理。常用于一些公共數據操作處理,例如:分頁插件,在執行sql查詢前增加分頁參數多租戶系統中,增加租戶ID參數。增加更新時間、創建時

Mybatis的插件,主要用于在執行sql前后,對sql進行封裝加工,或者在sql執行后,對數據進行加工處理。常用于一些公共數據操作處理,例如:cXd28資訊網——每日最新資訊28at.com

  1. 分頁插件,在執行sql查詢前增加分頁參數
  2. 多租戶系統中,增加租戶ID參數。
  3. 增加更新時間、創建時間、更新人、創建人的參數信息。
  4. 數據權限中,增加參數查詢。

插件開發過程

確定需要攔截的簽名

指定需要攔截的方法,通過方法簽名來指定,方法簽名即指定哪個類的哪個方法+方法參數。這里的類不能隨便寫,只能從以下幾個類中選,也就是說,MyBatis 插件可以攔截四大對象中的任意一個。cXd28資訊網——每日最新資訊28at.com

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

我們來看以下mybatisplus的插件配置的簽名:cXd28資訊網——每日最新資訊28at.com

@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指定四大類型中的任意一個,method指定攔截類型中方法,args指定方法參數。例如:cXd28資訊網——每日最新資訊28at.com

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

指定了攔截StatementHandler的prepare方法,方法有兩個參數,一個是Connection類型,另一個是Integer類型。cXd28資訊網——每日最新資訊28at.com

public interface StatementHandler {  Statement prepare(Connection connection, Integer transactionTimeout)      throws SQLException;            //....      }

插件接口定義

在 MyBatis 中開發插件,需要實現 Interceptor 接口。接口的定義如下:cXd28資訊網——每日最新資訊28at.com

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

實現插件

創建個類實現Interceptor接口,并且在實現類上指定方法簽名即可。cXd28資訊網——每日最新資訊28at.com

最后需要在mybatis配置文件中配置插件cXd28資訊網——每日最新資訊28at.com

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

最后建議看一下MybatisPlusInterceptor的實現,里面還使用到了責任鏈設計模式。cXd28資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-10908-0.html一篇聊聊Mybatis插件開發

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 一起聊聊GraalVM for JDK 21

下一篇: C++函數式編程:提高代碼表達力和可維護性

標簽:
  • 熱門焦點
Top