溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MyBatis插件原理是什么

發(fā)布時間:2021-10-21 15:16:45 來源:億速云 閱讀:161 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“MyBatis插件原理是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“MyBatis插件原理是什么”吧!

如何集成分頁插件

Spring-Boot+Mybatis+PageHelper 。

引入pom依賴

<dependency>    <groupId>com.github.pagehelper</groupId>    <artifactId>pagehelper-spring-boot-starter</artifactId>    <version>1.2.3</version> </dependency>

配置分頁插件配置項

pagehelper:   helperDialect: mysql   reasonable: true   supportMethodsArguments: true   params: count=countSql

service接口代碼中

PageInfo selectUsersByName(int pageIndex, int pageSize);

service實現(xiàn)類代碼中

@Override public PageInfo selectUsersByName(int pageIndex, int pageSize) {     PageHelper.startPage(pageIndex, pageSize);     List<User> users = userMapper.selectUsersByName(null);     return new PageInfo(users); }

Mapper代碼代碼

<select id="selectUsersByName" resultMap="User">     select * from m_user     <where>         <if test="userName != null and userName != ''">             `name` = #{userName}         </if>     </where> </select>
List<User> selectUsersByName(@Param("userName") String userName);

controller中代碼

@GetMapping("/user/name") public PageInfo selectUsersByName(int pageIndex, int pageSize) {     return userService.selectUsersByName(pageIndex, pageSize); }

然后我們訪問

http://localhost:9002/user/name?pageIndex=1&pageSize=10

輸出結(jié)果:

MyBatis插件原理是什么

輸出重要項說明:

  • pageNum:當(dāng)前頁碼。

  • pageSize:每頁數(shù)。

  • list:就是我們返回的業(yè)務(wù)數(shù)據(jù)。

  • total:總數(shù)據(jù)。

  • hasNextPage:是否存在下一頁。

我們在看看輸出SQL:

MyBatis插件原理是什么

發(fā)現(xiàn)其實執(zhí)行了兩條SQL:count和limit。

猜測分頁插件實現(xiàn)

1.這個分頁插件無非就是在我們的查詢條件上拼接了個limit和做了一個count查詢。

2.我們這里使用的是Mysql作為數(shù)據(jù)庫,如果是Oracle的話那就不是limit了,所以這里有多重數(shù)據(jù)庫對應(yīng)的方案。

3.在沒有此插件的前面攔截并做了sql和相關(guān)處理。

根據(jù)官網(wǎng)快速入門插件

下面是來自官網(wǎng)的一段話:

MyBatis 允許你在映射語句執(zhí)行過程中的某一點進行攔截調(diào)用。默認(rèn)情況下,MyBatis 允許使用插件來攔截的方法調(diào)用包括:

  • Executor (update, query, flushStatements, commit, rollback, getTransaction,  close, isClosed)

  • ParameterHandler (getParameterObject, setParameters)

  • ResultSetHandler (handleResultSets, handleOutputParameters)

  • StatementHandler (prepare, parameterize, batch, update, query)

這些類中方法的細(xì)節(jié)可以通過查看每個方法的簽名來發(fā)現(xiàn),或者直接查看 MyBatis  發(fā)行包中的源代碼。如果你想做的不僅僅是監(jiān)控方法的調(diào)用,那么你最好相當(dāng)了解要重寫的方法的行為。因為在試圖修改或重寫已有方法的行為時,很可能會破壞 MyBatis  的核心模塊。這些都是更底層的類和方法,所以使用插件的時候要特別當(dāng)心。

通過 MyBatis 提供的強大機制,使用插件是非常簡單的,只需實現(xiàn) Interceptor 接口,并指定想要攔截的方法簽名即可。

那我們就嘗試著按照官方來寫一個插件。

自定義插件

@Intercepts({@Signature(         type= Executor.class,         method = "update",         args = {MappedStatement.class,Object.class})}) public class TianPlugin implements Interceptor {     private Properties properties = new Properties();      @Override     public Object intercept(Invocation invocation) throws Throwable {         System.out.println("老田寫的一個Mybatis插件--start");         Object returnObject = invocation.proceed();         System.out.println("老田寫的一個Mybatis插件---end");         return returnObject;     } }

然后把插件類注入到容器中。

MyBatis插件原理是什么

這里的自定義完全是官網(wǎng)給出的案例。從自定義的插件類中看到有個update,我們猜測肯定是需要執(zhí)行update才會被攔截到。

訪問前面的代碼:http://localhost:9002/updateUser

MyBatis插件原理是什么

成功了。

這是大家肯定會聯(lián)想到我們剛剛開始學(xué)動態(tài)代理的時候,不就是在要調(diào)用的方法的前面和后面做點小東東嗎?

Mybatis的插件確實就是這樣的。

我們來分析一下官方的那段話和我們自定義的插件。

分析

首先,我們自定義的插件必須是針對下面這四個類以及方法。

  • Executor (update, query, flushStatements, commit, rollback, getTransaction,  close, isClosed)

  • ParameterHandler (getParameterObject, setParameters)

  • ResultSetHandler (handleResultSets, handleOutputParameters)

  • StatementHandler (prepare, parameterize, batch, update, query)

其次,我們必須實現(xiàn)Mybatis的Interceptor。

MyBatis插件原理是什么

Interceptor中三個方法的作用:

  • intercept():執(zhí)行攔截內(nèi)容的地方,比如:在調(diào)用某類方法前后做一些自己的處理,簡單就是打印日志。

  • plugin():決定是否觸發(fā)intercept()方法。

  • setProperties():給自定義的攔截器傳遞我們配置的屬性參數(shù)(這個可以暫時不管他,后面我們寫一個相對完整點的插件,你就明白是干啥的了)。

plugin方法

default Object plugin(Object target) {     return Plugin.wrap(target, this);   }

默認(rèn)實現(xiàn)方法,里面調(diào)用了Plugin.wrap()方法。

public class Plugin implements InvocationHandler {    private Object target;   private Interceptor interceptor;   private Map<Class<?>, Set<Method>> signatureMap;    private Plugin(Object target, Interceptor interceptor, Map<Class<?>, Set<Method>> signatureMap) {     this.target = target;     this.interceptor = interceptor;     this.signatureMap = signatureMap;   }    public static Object wrap(Object target, Interceptor interceptor) {     Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);     Class<?> type = target.getClass();     Class<?>[] interfaces = getAllInterfaces(type, signatureMap);     if (interfaces.length > 0) {       // 創(chuàng)建JDK動態(tài)代理對象       return Proxy.newProxyInstance(           type.getClassLoader(),           interfaces,           new Plugin(target, interceptor, signatureMap));     }     return target;   }    @Override   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {     try {       Set<Method> methods = signatureMap.get(method.getDeclaringClass());       // 判斷是否是需要攔截的方法(很重要)       if (methods != null && methods.contains(method)) {         // 回調(diào)intercept()方法         return interceptor.intercept(new Invocation(target, method, args));       }       return method.invoke(target, args);     } catch (Exception e) {       throw ExceptionUtil.unwrapThrowable(e);     }   } //...省略其他不相關(guān)代碼 }

這不就是一個JDK動態(tài)代理嗎?

Map

所以,我們不要動不動就說反射性能很差,那是因為你沒有像Mybatis一樣去緩存一個對象的反射結(jié)果。

判斷是否是需要攔截的方法,這句注釋很重要,一旦忽略了,都不知道Mybatis是怎么判斷是否執(zhí)行攔截內(nèi)容的,要記住。

Plugin.wrap(target, this)是干什么的?

使用JDK的動態(tài)代理,給target對象創(chuàng)建一個delegate代理對象,以此來實現(xiàn)方法攔截和增強功能,它會回調(diào)intercept()方法。

為什么要寫注解?注解都是什么含義?

在我們自定義的插件上有一堆注解,別害怕。

Mybatis規(guī)定插件必須編寫Annotation注解,是必須,而不是可選。

@Intercepts({@Signature( type= Executor.class, method = "update",                         args = {MappedStatement.class,Object.class})}            ) public class TianPlugin implements Interceptor {

@Intercepts注解:裝載一個@Signature列表,一個@Signature其實就是一個需要攔截的方法封裝。那么,一個攔截器要攔截多個方法,自然就是一個@Signature列表。

type= Executor.class, method = "update",args = {MappedStatement.class,Object.class}

解釋:要攔截Executor接口內(nèi)的query()方法,參數(shù)類型為args列表。

MyBatis插件原理是什么

那如果想攔截多個方法呢?

@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface Intercepts {   Signature[] value(); }

這就簡單了吧,我們在@Intercepts注解中可以存放多個@Signature注解。

比如說前面分頁插件中就是攔截多個方法的。

MyBatis插件原理是什么

為什么攔截兩個都是query方法呢?因為在Executor中有兩個query方法。

MyBatis插件原理是什么

到此,相信大家對“MyBatis插件原理是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI