好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > mybatis 實現 SQL 查詢攔截修改詳解

mybatis 實現 SQL 查詢攔截修改詳解

熱門標簽:真人語音電話機器人 怎么在地圖標注位置生成圖片 400電話可以免費申請嗎 美國反騷擾電話機器人 騰訊地圖標注提升 悟空科技電話機器人 銅陵防封電銷卡 福建外呼系統定制化 電銷卡外呼系統供應商

前言

截器的一個作用就是我們可以攔截某些方法的調用,我們可以選擇在這些被攔截的方法執行前后加上某些邏輯,也可以在執行這些被攔截的方法時執行自己的邏輯而不再執行被攔截的方法。

Mybatis攔截器設計的一個初衷就是為了供用戶在某些時候可以實現自己的邏輯而不必去動Mybatis固有的邏輯。比如我想針對所有的SQL執行某個固定的操作,針對SQL查詢執行安全檢查,或者記錄相關SQL查詢日志等等。

Mybatis為我們提供了一個Interceptor接口,可以實現自定義的攔截器。

 public interface Interceptor {
 Object intercept(Invocation invocation) throws Throwable;
 Object plugin(Object target);
 void setProperties(Properties properties);
}

接口中包含了三個方法定義

intercept方法為具體的攔截對象的處理方法,傳入的Invocation包含了攔截目標類的實力,攔截的方法和方法的入參數組。使用Invocation的procced執行原函數。

plugin 中執行判斷是否要進行攔截進,如果不需要攔截,直接返回target,如果需要攔截則調用Plugin類中的wrap靜態方法,如果當前攔截器實現了任意接口,則返回一個代理對象,否則直接返回(回憶代理模式的設計)。代理對象實際是一個Plugin類實例,它實現了InvocationHandler接口 ,InvocationHandler接口僅包含invoke方法用于回調方法。

當執行代理對象的接口方法時,會調用Plugin的invoke方法,它會把要執行的對象,方法和參數打包成Invocation對象傳給攔截器的intercept方法。Invocation定義了一個procced方法,用于執行被攔截的原方法。

Plugin類定義

public class Plugin implements InvocationHandler {
 
 private Object target;
 private Interceptor interceptor;
 private Map, Set> signatureMap;
 
 private Plugin(Object target, Interceptor interceptor, Map, Set> signatureMap) {
  this.target = target;
  this.interceptor = interceptor;
  this.signatureMap = signatureMap;
 }
 
 public static Object wrap(Object target, Interceptor interceptor) {
  Map, Set> signatureMap = getSignatureMap(interceptor);
  Class type = target.getClass();
  Class[] interfaces = getAllInterfaces(type, signatureMap);
  if (interfaces.length > 0) {
   return Proxy.newProxyInstance(
     type.getClassLoader(),
     interfaces,
     new Plugin(target, interceptor, signatureMap));
  }
  return target;
 }
 
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  try {
   Set methods = signatureMap.get(method.getDeclaringClass());
   if (methods != null  methods.contains(method)) {
    return interceptor.intercept(new Invocation(target, method, args));
   }
   return method.invoke(target, args);
  } catch (Exception e) {
   throw ExceptionUtil.unwrapThrowable(e);
  }
 }
 
 private static Map, Set> getSignatureMap(Interceptor interceptor) {
  Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class);
  if (interceptsAnnotation == null) { // issue #251
   throw new PluginException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName());   
  }
  Signature[] sigs = interceptsAnnotation.value();
  Map, Set> signatureMap = new HashMap, Set>();
  for (Signature sig : sigs) {
   Set methods = signatureMap.get(sig.type());
   if (methods == null) {
    methods = new HashSet();
    signatureMap.put(sig.type(), methods);
   }
   try {
    Method method = sig.type().getMethod(sig.method(), sig.args());
    methods.add(method);
   } catch (NoSuchMethodException e) {
    throw new PluginException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e);
   }
  }
  return signatureMap;
 }
 
 private static Class[] getAllInterfaces(Class type, Map, Set> signatureMap) {
  Set> interfaces = new HashSet>();
  while (type != null) {
   for (Class c : type.getInterfaces()) {
    if (signatureMap.containsKey(c)) {
     interfaces.add(c);
    }
   }
   type = type.getSuperclass();
  }
  return interfaces.toArray(new Class[interfaces.size()]);
 }
 
}

setProperties 方法顧名思義,用于設置屬性的。bean的屬性初始化方法有很多,這是其中的一種。

mybatis提供了@Intercepts注解用于聲明當前類是攔截器,其值為@Signature數組,表明要攔截的接口、方法以及對應的參數類型

@Intercepts({@Signature(method = "prepare", type = StatementHandler.class, args = {Connection.class}),
    @Signature(method = "query", type = StatementHandler.class, args = {java.sql.Statement.class, ResultHandler.class})})
public class TenantInterceptor implements Interceptor {
.....

例如上面的類聲明,第一個Signature標注攔截了StatementHandler類下的入參是一個Connection的名為prepare的方法。

第二個Signature標注攔截StatementHandler類中包含2個入參(分別為Statement和ResultHandler類型)的名為query的方法。

最后,聲明的Interceptor需要注冊到mybatis的plug中才能生效。

  !-- 配置mybatis -->
  bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    property name="dataSource" ref="dataSource"/>
    property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
    !-- mapper掃描 -->
    property name="mapperLocations" value="classpath:mybatis/*/*.xml"/>
    property name="plugins">
      array>
        !-- 注冊自己的攔截器 -->
        bean id="paginationInterceptor" class="xxx.xxx.TenantInterceptor">
        /bean>
      /array>
    /property>
  /bean>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • MyBatis實現模糊查詢的幾種方式
  • Mybatis多表關聯查詢的實現(DEMO)
  • mybatis 模糊查詢的實現方法
  • Mybatis 一對多和多對一關聯查詢問題
  • MyBatis之自查詢使用遞歸實現 N級聯動效果(兩種實現方式)
  • Mybatis查詢記錄條數的實例代碼
  • MyBatis實現動態查詢、模糊查詢功能
  • Mybatis查詢語句結果集的總結大全
  • mybatis分頁及模糊查詢功能實現
  • MyBatis查詢時屬性名和字段名不一致問題的解決方法

標簽:烏海 湖南 白銀 聊城 武威 臨汾 云浮 湖北

巨人網絡通訊聲明:本文標題《mybatis 實現 SQL 查詢攔截修改詳解》,本文關鍵詞  mybatis,實現,SQL,查詢,攔截,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《mybatis 實現 SQL 查詢攔截修改詳解》相關的同類信息!
  • 本頁收集關于mybatis 實現 SQL 查詢攔截修改詳解的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 大色综合色综合网站| 无套中出丰满人妻无码99蜜AV | 亚洲欧洲自拍拍偷精品美利坚| 欧美bbwhd老太大| 男人小j进女人屁股视频| 无遮挡18禁啪啪成人片娼年| 激情偷拍网| 亚洲精品国产精品乱码不97| 巨胸喷奶水视频www网站软件| 99精品人妻无码专区在线视频区 | 美国一级毛片aa| 中文字幕有码无码理论片| 日本一级特黄大片做受的推荐理由| 韩国三级hd中文字幕有哪些| freehdxxxxcartoon游戏| 撕开丫鬟亵裤泄欲h| 两男一女3p刺激视频国内| 色屁屁影院WWW国产高清麻豆| 美女黄色在线看| 日韩欧美精品综合一区二区三区| 积积对积积的桶30分软件| 精品99一区二区三区麻豆的更新时间 | 精品99久久久久成人网站| av高潮喷水一区二区三区| 琪琪电影网20理片| 国产精品久久久久jk制服| 久久精品国产AV麻豆五月丁香 | 美女脱个精光图片| 尤物网视频| 国产伦精品午夜500部| ChinaXXX美女XXXHD| 国产高清不卡一区二区| 东北老妇露脸xxx| 美女主播露胸 揉胸| 欧美黑粗硬大| 韩国19禁电影推荐资源 | 巴西一级婬片A片免费放| 中文字幕理伦片免费看| 天天射色综合| 一女多男双龙np高h辣肉| 国产麻豆剧果冻传媒浮生|