最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mybatis 實現(xiàn) SQL 查詢攔截修改詳解

 更新時間:2019年07月16日 10:33:56   作者:coderstory  
這篇文章主要介紹了mybatis 實現(xiàn) SQL 查詢攔截修改詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

前言

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

Mybatis攔截器設(shè)計的一個初衷就是為了供用戶在某些時候可以實現(xiàn)自己的邏輯而不必去動Mybatis固有的邏輯。比如我想針對所有的SQL執(zhí)行某個固定的操作,針對SQL查詢執(zhí)行安全檢查,或者記錄相關(guān)SQL查詢?nèi)罩镜鹊取?/p>

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

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

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

intercept方法為具體的攔截對象的處理方法,傳入的Invocation包含了攔截目標(biāo)類的實力,攔截的方法和方法的入?yún)?shù)組。使用Invocation的procced執(zhí)行原函數(shù)。

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

當(dāng)執(zhí)行代理對象的接口方法時,會調(diào)用Plugin的invoke方法,它會把要執(zhí)行的對象,方法和參數(shù)打包成Invocation對象傳給攔截器的intercept方法。Invocation定義了一個procced方法,用于執(zhí)行被攔截的原方法。

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 方法顧名思義,用于設(shè)置屬性的。bean的屬性初始化方法有很多,這是其中的一種。

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

@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標(biāo)注攔截了StatementHandler類下的入?yún)⑹且粋€Connection的名為prepare的方法。

第二個Signature標(biāo)注攔截StatementHandler類中包含2個入?yún)ⅲǚ謩e為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>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • mysql字符串函數(shù)詳細(xì)匯總

    mysql字符串函數(shù)詳細(xì)匯總

    這篇文章主要介紹了mysql字符串函數(shù)詳細(xì)匯總,字符串函數(shù)主要用來處理數(shù)據(jù)庫中的字符串?dāng)?shù)據(jù),更多相關(guān)內(nèi)容需要的朋友可以參考一下
    2022-07-07
  • MySql,MVCC實現(xiàn)及其機制,快照讀在RC,RR下的區(qū)別說明

    MySql,MVCC實現(xiàn)及其機制,快照讀在RC,RR下的區(qū)別說明

    這篇文章主要介紹了MySql,MVCC實現(xiàn)及其機制,快照讀在RC,RR下的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • 升級到MySQL5.7后開發(fā)不得不注意的一些坑

    升級到MySQL5.7后開發(fā)不得不注意的一些坑

    這篇文章主要給大家介紹了關(guān)于升級到MySQL5.7后開發(fā)不得不注意的一些坑,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • mysql共享鎖與排他鎖用法實例分析

    mysql共享鎖與排他鎖用法實例分析

    這篇文章主要介紹了mysql共享鎖與排他鎖用法,結(jié)合實例形式分析了mysql共享鎖與排他鎖相關(guān)概念、原理、用法及操作注意事項,需要的朋友可以參考下
    2019-09-09
  • MySQL 8.0.18 Hash Join不支持left/right join左右連接問題

    MySQL 8.0.18 Hash Join不支持left/right join左右連接問題

    在MySQL 8.0.18中,增加了Hash Join新功能,它適用于未創(chuàng)建索引的字段,做等值關(guān)聯(lián)查詢。這篇文章給大家介紹MySQL 8.0.18 Hash Join不支持left/right join左右連接,感興趣的朋友一起看看吧
    2019-11-11
  • Ubuntu上mysql的安裝及使用(通用版)

    Ubuntu上mysql的安裝及使用(通用版)

    今天小編就為大家分享一篇關(guān)于Ubuntu上mysql的安裝及使用(通用版),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • 基于Windows安裝MySQL 8.0.12圖文教程

    基于Windows安裝MySQL 8.0.12圖文教程

    這篇文章主要為大家詳細(xì)介紹了基于Windows安裝MySQL 8.0.12圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • MySql command line client命令操作大全

    MySql command line client命令操作大全

    這篇文章主要介紹了MySql command line client命令操作大全,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-01-01
  • MySQL字符集utf8修改為utf8mb4的方法步驟

    MySQL字符集utf8修改為utf8mb4的方法步驟

    這篇文章主要給大家介紹了關(guān)于MySQL字符集utf8修改為utf8mb4的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Mysql Error Code : 1436 Thread stack overrun

    Mysql Error Code : 1436 Thread stack overrun

    I meet with the error while calling stored procedures from the MySql in my Mac system server. It similar as the description below
    2011-07-07

最新評論

星子县| 无棣县| 尤溪县| 辰溪县| 呼玛县| 阳朔县| 马尔康县| 九江市| 连州市| 和静县| 盐山县| 定西市| 新巴尔虎左旗| 孝昌县| 绍兴县| 灌南县| 襄垣县| 武清区| 张掖市| 长乐市| 铜鼓县| 原平市| 蓝田县| 濮阳市| 汝州市| 佛山市| 长治市| 天峨县| 涟水县| 忻城县| 乌拉特前旗| 天镇县| 沙洋县| 准格尔旗| 富顺县| 武乡县| 兴国县| 平凉市| 古田县| 勐海县| 大同市|