成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

Mybatis Interceptor 攔截器

nemo / 1398人閱讀

摘要:攔截器的使用場(chǎng)景主要是更新數(shù)據(jù)庫(kù)的通用字段,分庫(kù)分表,加解密等的處理。攔截器均需要實(shí)現(xiàn)該接口。攔截器攔截器的使用需要查看每一個(gè)所提供的方法參數(shù)。對(duì)應(yīng)構(gòu)造器,為,為,為??蓞⒖紨r截器原理探究。

攔截器(Interceptor)在 Mybatis 中被當(dāng)做插件(plugin)對(duì)待,官方文檔提供了 Executor(攔截執(zhí)行器的方法),ParameterHandler(攔截參數(shù)的處理),ResultSetHandler(攔截結(jié)果集的處理),StatementHandler(攔截Sql語(yǔ)法構(gòu)建的處理) 共4種,并且提示“這些類中方法的細(xì)節(jié)可以通過(guò)查看每個(gè)方法的簽名來(lái)發(fā)現(xiàn),或者直接查看 MyBatis 發(fā)行包中的源代碼”。

攔截器的使用場(chǎng)景主要是更新數(shù)據(jù)庫(kù)的通用字段,分庫(kù)分表,加解密等的處理。

1. Interceptor

攔截器均需要實(shí)現(xiàn)該 org.apache.ibatis.plugin.Interceptor 接口。

2. Intercepts 攔截器
@Intercepts({
        @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
})

攔截器的使用需要查看每一個(gè)type所提供的方法參數(shù)。

Signature 對(duì)應(yīng) Invocation 構(gòu)造器,type 為 Invocation.Object,method 為 Invocation.Method,args 為 Invocation.Object[]。

method 對(duì)應(yīng)的 update 包括了最常用的 insert/update/delete 三種操作,因此 update 本身無(wú)法直接判斷sql為何種執(zhí)行過(guò)程。

args 包含了其余所有的操作信息, 按數(shù)組進(jìn)行存儲(chǔ), 不同的攔截方式有不同的參數(shù)順序, 具體看type接口的方法簽名, 然后根據(jù)簽名解析。

3. Object 對(duì)象類型

args 參數(shù)列表中,Object.class 是特殊的對(duì)象類型。如果有數(shù)據(jù)庫(kù)統(tǒng)一的實(shí)體 Entity 類,即包含表公共字段,比如創(chuàng)建、更新操作對(duì)象和時(shí)間的基類等,在編寫代碼時(shí)盡量依據(jù)該對(duì)象來(lái)操作,會(huì)簡(jiǎn)單很多。該對(duì)象的判斷使用

Object parameter = invocation.getArgs()[1];
if (parameter instanceof BaseEntity) {
    BaseEntity entity = (BaseEntity) parameter;
}

即可,根據(jù)語(yǔ)句執(zhí)行類型選擇對(duì)應(yīng)字段的賦值。

如果參數(shù)不是實(shí)體,而且具體的參數(shù),那么 Mybatis 也做了一些處理,比如 @Param("name") String name 類型的參數(shù),會(huì)被包裝成 Map 接口的實(shí)現(xiàn)來(lái)處理,即使是原始的 Map 也是如此。使用

Object parameter = invocation.getArgs()[1];
if (parameter instanceof Map) {
    Map map = (Map) parameter;
}

即可,對(duì)具體統(tǒng)一的參數(shù)進(jìn)行賦值。

4. SqlCommandType 命令類型

Executor 提供的方法中,update 包含了 新增,修改和刪除類型,無(wú)法直接區(qū)分,需要借助 MappedStatement 類的屬性 SqlCommandType 來(lái)進(jìn)行判斷,該類包含了所有的操作類型

public enum SqlCommandType {
  UNKNOWN, INSERT, UPDATE, DELETE, SELECT, FLUSH;
}

畢竟新增和修改的場(chǎng)景,有些參數(shù)是有區(qū)別的,比如創(chuàng)建時(shí)間和更新時(shí)間,update 時(shí)是無(wú)需兼顧創(chuàng)建時(shí)間字段的。

MappedStatement ms = (MappedStatement) invocation.getArgs()[0];
SqlCommandType commandType = ms.getSqlCommandType();
5. 實(shí)例

自己編寫的小項(xiàng)目中,需要統(tǒng)一給數(shù)據(jù)庫(kù)字段屬性賦值:

public class BaseEntity {
    private int id;
    private int creator;
    private int updater;
    private Long createTime;
    private Long updateTime;
}

dao 操作使用了實(shí)體和參數(shù)的方式,個(gè)人建議還是統(tǒng)一使用實(shí)體比較簡(jiǎn)單,易讀。

使用實(shí)體:

int add(BookEntity entity);

使用參數(shù):

int update(@Param("id") int id, @Param("url") String url, @Param("description") String description, @Param("playCount") int playCount, @Param("creator") int creator, @Param("updateTime") long updateTime);

完整的例子:

package com.github.zhgxun.talk.common.plugin;

import com.github.zhgxun.talk.common.util.DateUtil;
import com.github.zhgxun.talk.common.util.UserUtil;
import com.github.zhgxun.talk.entity.BaseEntity;
import com.github.zhgxun.talk.entity.UserEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Properties;

/**
 * 全局?jǐn)r截?cái)?shù)據(jù)庫(kù)創(chuàng)建和更新
 * 

* Signature 對(duì)應(yīng) Invocation 構(gòu)造器, type 為 Invocation.Object, method 為 Invocation.Method, args 為 Invocation.Object[] * method 對(duì)應(yīng)的 update 包括了最常用的 insert/update/delete 三種操作, 因此 update 本身無(wú)法直接判斷sql為何種執(zhí)行過(guò)程 * args 包含了其余多有的操作信息, 按數(shù)組進(jìn)行存儲(chǔ), 不同的攔截方式有不同的參數(shù)順序, 具體看type接口的方法簽名, 然后根據(jù)簽名解析, 參見官網(wǎng) * * @link http://www.mybatis.org/mybatis-3/zh/configuration.html#plugins 插件 *

* MappedStatement 包括了SQL具體操作類型, 需要通過(guò)該類型判斷當(dāng)前sql執(zhí)行過(guò)程 */ @Component @Intercepts({ @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}) }) @Slf4j public class NormalPlugin implements Interceptor { @Override @SuppressWarnings("unchecked") public Object intercept(Invocation invocation) throws Throwable { // 根據(jù)簽名指定的args順序獲取具體的實(shí)現(xiàn)類 // 1. 獲取MappedStatement實(shí)例, 并獲取當(dāng)前SQL命令類型 MappedStatement ms = (MappedStatement) invocation.getArgs()[0]; SqlCommandType commandType = ms.getSqlCommandType(); // 2. 獲取當(dāng)前正在被操作的類, 有可能是Java Bean, 也可能是普通的操作對(duì)象, 比如普通的參數(shù)傳遞 // 普通參數(shù), 即是 @Param 包裝或者原始 Map 對(duì)象, 普通參數(shù)會(huì)被 Mybatis 包裝成 Map 對(duì)象 // 即是 org.apache.ibatis.binding.MapperMethod$ParamMap Object parameter = invocation.getArgs()[1]; // 獲取攔截器指定的方法類型, 通常需要攔截 update String methodName = invocation.getMethod().getName(); log.info("NormalPlugin, methodName; {}, commandType: {}", methodName, commandType); // 3. 獲取當(dāng)前用戶信息 UserEntity userEntity = UserUtil.getCurrentUser(); // 默認(rèn)測(cè)試參數(shù)值 int creator = 2, updater = 3; if (parameter instanceof BaseEntity) { // 4. 實(shí)體類 BaseEntity entity = (BaseEntity) parameter; if (userEntity != null) { creator = entity.getCreator(); updater = entity.getUpdater(); } if (methodName.equals("update")) { if (commandType.equals(SqlCommandType.INSERT)) { entity.setCreator(creator); entity.setUpdater(updater); entity.setCreateTime(DateUtil.getTimeStamp()); entity.setUpdateTime(DateUtil.getTimeStamp()); } else if (commandType.equals(SqlCommandType.UPDATE)) { entity.setUpdater(updater); entity.setUpdateTime(DateUtil.getTimeStamp()); } } } else if (parameter instanceof Map) { // 5. @Param 等包裝類 // 更新時(shí)指定某些字段的最新數(shù)據(jù)值 if (commandType.equals(SqlCommandType.UPDATE)) { // 遍歷參數(shù)類型, 檢查目標(biāo)參數(shù)值是否存在對(duì)象中, 該方式需要應(yīng)用編寫有一些統(tǒng)一的規(guī)范 // 否則均統(tǒng)一為實(shí)體對(duì)象, 就免去該重復(fù)操作 Map map = (Map) parameter; if (map.containsKey("creator")) { map.put("creator", creator); } if (map.containsKey("updateTime")) { map.put("updateTime", DateUtil.getTimeStamp()); } } } // 6. 均不是需要被攔截的類型, 不做操作 return invocation.proceed(); } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { } }

6. 感受

其它幾種類型,后面在看,尤其是分頁(yè)。

在編寫代碼的過(guò)程中,有時(shí)候還是需要先知道對(duì)象的類型,比如 Object.class 跟 Interface 一樣,很多時(shí)候僅僅代表一種數(shù)據(jù)類型,需要明確知道后再進(jìn)行操作。

@Param 標(biāo)識(shí)的參數(shù)其實(shí)會(huì)被 Mybatis 封裝成 org.apache.ibatis.binding.MapperMethod$ParamMap 類型,一開始我就是使用

for (Field field:parameter.getClass().getDeclaredFields()) {

}

的方式獲取,以為這些都是對(duì)象的屬性,通過(guò)反射獲取并修改即可,實(shí)際上發(fā)現(xiàn)對(duì)象不存在這些屬性,但是打印出的信息中,明確有這些字段的,網(wǎng)上參考一些信息后,才知道這是一個(gè) Map 類型,問(wèn)題才迎刃而解。

可參考MyBatis攔截器原理探究。

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/72716.html

相關(guān)文章

  • 【深入淺出MyBatis筆記】插件

    摘要:插件插件接口在中使用插件,我們必須實(shí)現(xiàn)接口。它將直接覆蓋你所攔截對(duì)象原有的方法,因此它是插件的核心方法。插件在對(duì)象中的保存插件的代理和反射設(shè)計(jì)插件用的是責(zé)任鏈模式,的責(zé)任鏈?zhǔn)怯扇ザx的。 插件 1、插件接口 在MyBatis中使用插件,我們必須實(shí)現(xiàn)接口Interceptor。 public interface Interceptor { // 它將直接覆蓋你所攔截對(duì)象原有的方法,因...

    leon 評(píng)論0 收藏0
  • 關(guān)于Mybatis截器對(duì)結(jié)果集的攔截

    摘要:剛學(xué)習(xí)攔截器方面,在網(wǎng)上找了很多關(guān)于攔截器方面的文章,自己也嘗試過(guò)寫過(guò)幾個(gè),但是關(guān)于結(jié)果集的攔截始終沒(méi)有找到合適的不要噴我,畢竟是新手。 剛學(xué)習(xí)Mybatis攔截器方面,在網(wǎng)上找了很多關(guān)于Mybatis攔截器方面的文章,自己也嘗試過(guò)寫過(guò)幾個(gè),但是關(guān)于結(jié)果集的攔截始終沒(méi)有找到合適的(PS: 不要噴我,畢竟是新手)。也在segmentfault 上提問(wèn)過(guò),依然沒(méi)有找到一個(gè)易于理解的,后來(lái)自...

    kohoh_ 評(píng)論0 收藏0
  • 大白話講解Mybatis的plugin(Interceptor)的使用

    摘要:提供了一個(gè)入口,可以讓你在語(yǔ)句執(zhí)行過(guò)程中的某一點(diǎn)進(jìn)行攔截調(diào)用。? ? ? ? mybatis提供了一個(gè)入口,可以讓你在語(yǔ)句執(zhí)行過(guò)程中的某一點(diǎn)進(jìn)行攔截調(diào)用。官方稱之為插件plugin,但是在使用的時(shí)候需要實(shí)現(xiàn)Interceptor接口,默認(rèn)情況下,MyBatis 允許使用插件來(lái)攔截的方法調(diào)用包括以下四個(gè)對(duì)象的方法:Executor (update, query, flushStatements...

    laznrbfe 評(píng)論0 收藏0
  • 不得不知的責(zé)任鏈設(shè)計(jì)模式

    世界上最遙遠(yuǎn)的距離,不是生與死,而是它從你的世界路過(guò)無(wú)數(shù)次,你卻選擇視而不見,你無(wú)情,你冷酷啊...... showImg(https://segmentfault.com/img/remote/1460000019550563); 被你忽略的就是責(zé)任鏈設(shè)計(jì)模式,希望它再次經(jīng)過(guò)你身旁你會(huì)猛的發(fā)現(xiàn),并對(duì)它微微一笑...... 責(zé)任鏈設(shè)計(jì)模式介紹 抽象介紹 初次見面,了解表象,深入交流之后(看完文中的...

    raise_yang 評(píng)論0 收藏0
  • springmvc項(xiàng)目轉(zhuǎn)為springboot

    摘要:說(shuō)明如果你的項(xiàng)目連項(xiàng)目都不是,請(qǐng)自行轉(zhuǎn)為項(xiàng)目,在按照本教程進(jìn)行。本教程適用于的項(xiàng)目。處理攔截資源文件問(wèn)題。 說(shuō)明 如果你的項(xiàng)目連maven項(xiàng)目都不是,請(qǐng)自行轉(zhuǎn)為maven項(xiàng)目,在按照本教程進(jìn)行。本教程適用于spring+springmvc+mybatis+shiro的maven項(xiàng)目。1.修改pom文件依賴 刪除之前的spring依賴,添加springboot依賴 or...

    wqj97 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

nemo

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<