欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mybatis-plus配置攔截器實(shí)現(xiàn)sql完整打印的代碼設(shè)計(jì)

 更新時(shí)間:2024年06月16日 13:47:58   作者:shigen01  
在使用mybatis-plus(mybatis)的時(shí)候,往往需要打印完整的sql語(yǔ)句,然而輸出的日志不是很理想,因?yàn)閟ql語(yǔ)句中的關(guān)鍵字段信息都是用?來(lái)代替的,所以本文分享了一下自己寫(xiě)了一個(gè)攔截器實(shí)現(xiàn)了sql完整的打印,需要的朋友可以參考下

在使用mybatis-plus(mybatis)的時(shí)候,往往需要打印完整的sql語(yǔ)句,然而輸出的日志不是很理想:

因?yàn)閟ql語(yǔ)句中的關(guān)鍵字段信息都是用?來(lái)代替的。那有什么方法實(shí)現(xiàn)完整的sql打印呢?有是有的,我記得IDEA的插件市場(chǎng)有一款插件可以實(shí)現(xiàn)完整sql的打印,但是好像是要收費(fèi)的。今天刷某音的時(shí)候看到了某博主分享了一下自己寫(xiě)了一個(gè)攔截器實(shí)現(xiàn)了sql完整的打印,以下是實(shí)現(xiàn)的效果:

可以看到了sql的執(zhí)行時(shí)間和完整的sql語(yǔ)句。sql的執(zhí)行時(shí)間沒(méi)啥好說(shuō)的,關(guān)鍵是sql語(yǔ)句的完整打印。現(xiàn)在先來(lái)分享一下代碼吧。

代碼

controller的設(shè)計(jì)

這里僅展示關(guān)鍵的代碼,一個(gè)更新的操作,一個(gè)分頁(yè)查詢的操作。

     @PostMapping(value = "update")
     public Result<String> update(@RequestBody @Validated(value = UpdateGroup.class) User user) {
         int update = userMapper.updateById(user);
         return update > 0 ? Result.ok(null) : Result.err(null);
     }
 ?
     @GetMapping(value = "get")
     public Result<List<User>> get(@RequestParam(value = "id", required = false) Integer id,
                                   @RequestParam(value = "name", required = false) String name
     ) {
         LambdaQueryWrapper<User> queryChainWrapper = new LambdaQueryWrapper<>();
         queryChainWrapper.eq(id != null, User::getId, id);
         queryChainWrapper.eq(name != null, User::getUsername, name);
         List<User> records = userMapper.selectPage(new Page<User>(0, 10), queryChainWrapper).getRecords();
         return Result.ok(records);
     }

攔截器設(shè)計(jì)

雖然這里是mybatis-plus框架,但是還是需要使用到mybatis的功能。

 /**
  * @author shigenfu
  * @date 2024/6/16 10:01
  */
 @Intercepts({
         @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}),
         @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
 })
 @Slf4j
 public class SqlInterceptor implements Interceptor {
 ?
     @Override
     public Object intercept(Invocation invocation) throws Throwable {
         // 統(tǒng)計(jì)sql執(zhí)行耗時(shí)
         long startTime = System.currentTimeMillis();
         Object proceed = invocation.proceed();
         long endTime = System.currentTimeMillis();
 ?
         String printSql = null;
         try {
             printSql = generateSql(invocation);
         } catch (Exception exception) {
             log.error("獲取sql異常", exception);
         } finally {
             long costTime = endTime - startTime;
             log.info("\n 執(zhí)行SQL耗時(shí):{}ms \n 執(zhí)行SQL:{}", costTime, printSql);
         }
         return proceed;
     }
 ?
     private static String generateSql(Invocation invocation) {
 ?
         MappedStatement statement = (MappedStatement) invocation.getArgs()[0];
         Object parameter = null;
         if (invocation.getArgs().length > 1) {
             parameter = invocation.getArgs()[1];
         }
         Configuration configuration = statement.getConfiguration();
         BoundSql boundSql = statement.getBoundSql(parameter);
 ?
         // 獲取參數(shù)對(duì)象
         Object parameterObject = boundSql.getParameterObject();
         // 獲取參數(shù)映射
         List<ParameterMapping> params = boundSql.getParameterMappings();
         // 獲取到執(zhí)行的SQL
         String sql = boundSql.getSql();
         // SQL中多個(gè)空格使用一個(gè)空格代替
         sql = sql.replaceAll("[\s]+", " ");
         if (!ObjectUtils.isEmpty(params) && !ObjectUtils.isEmpty(parameterObject)) {
             // TypeHandlerRegistry 是 MyBatis 用來(lái)管理 TypeHandler 的注冊(cè)器 TypeHandler 用于在 Java 類型和 JDBC 類型之間進(jìn)行轉(zhuǎn)換
             TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
             // 如果參數(shù)對(duì)象的類型有對(duì)應(yīng)的 TypeHandler,則使用 TypeHandler 進(jìn)行處理
             if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
                 sql = sql.replaceFirst("\?", Matcher.quoteReplacement(getParameterValue(parameterObject)));
             } else {
                 // 否則,逐個(gè)處理參數(shù)映射
                 for (ParameterMapping param : params) {
                     // 獲取參數(shù)的屬性名
                     String propertyName = param.getProperty();
                     MetaObject metaObject = configuration.newMetaObject(parameterObject);
                     // 檢查對(duì)象中是否存在該屬性的 getter 方法,如果存在就取出來(lái)進(jìn)行替換
                     if (metaObject.hasGetter(propertyName)) {
                         Object obj = metaObject.getValue(propertyName);
                         sql = sql.replaceFirst("\?", Matcher.quoteReplacement(getParameterValue(obj)));
                         // 檢查 BoundSql 對(duì)象中是否存在附加參數(shù)
                     } else if (boundSql.hasAdditionalParameter(propertyName)) {
                         Object obj = boundSql.getAdditionalParameter(propertyName);
                         sql = sql.replaceFirst("\?", Matcher.quoteReplacement(getParameterValue(obj)));
                     } else {
                         // SQL匹配不上,帶上“缺失”方便找問(wèn)題
                         sql = sql.replaceFirst("\?", "缺失");
                     }
                 }
             }
         }
         return sql;
     }
 ?
     private static String getParameterValue(Object object) {
         String value = "";
         if (object instanceof String) {
             value = "'" + object + "'";
         } else if (object instanceof Date) {
             DateFormat format = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);
             value = "'" + format.format((Date) object) + "'";
         } else if (!ObjectUtils.isEmpty(object)) {
             value = object.toString();
         }
         return value;
     }
 ?
     @Override
     public Object plugin(Object target) {
         return Plugin.wrap(target, this);
     }
 ?
 }

直接貼的代碼,其實(shí)就是在sql執(zhí)行完畢之后,根據(jù)sql的template和sql參數(shù)進(jìn)行?的替換。

這里不分析代碼,希望能親自debug看一下。

配置類

這里的配置我都寫(xiě)在了mybatis-plus的配置代碼里邊。

 @Configuration
 @MapperScan(value = "main.java.shigen.demo.dao")
 public class MybatisPlusConfig {
 ?
     @Bean
     public MybatisPlusInterceptor mybatisPlusInterceptor() {
         MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
         // 分頁(yè)插件
         interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
         // 樂(lè)觀鎖插件
         interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
         return interceptor;
     }
 ?
     @Bean
     public ConfigurationCustomizer configurationCustomizer() {
         return configuration -> {
             configuration.addInterceptor(new SqlInterceptor());
         };
     }
 }

以上就是核心的代碼了,實(shí)測(cè)遇到的問(wèn)題有一個(gè):

  • 分頁(yè)查詢的時(shí)候,無(wú)法顯示limit 0,10這個(gè)sql后綴

希望有時(shí)間的時(shí)候能夠再次優(yōu)化一下。同時(shí),也沒(méi)有經(jīng)過(guò)實(shí)際的項(xiàng)目測(cè)試,只是簡(jiǎn)單的demo測(cè)試。僅具有參考價(jià)值,無(wú)法保證實(shí)際的應(yīng)用。

后記

到此這篇關(guān)于mybatis-plus配置攔截器實(shí)現(xiàn)sql完整打印的代碼設(shè)計(jì)的文章就介紹到這了,更多相關(guān)mybatis-plus攔截器實(shí)現(xiàn)sql打印內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java檢查非空的三種方法總結(jié)

    Java檢查非空的三種方法總結(jié)

    這篇文章主要介紹了Java檢查非空的三種方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • SpringBoot2.x集成Dozer的示例代碼

    SpringBoot2.x集成Dozer的示例代碼

    本文主要介紹了SpringBoot2.x集成Dozer的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Java中List轉(zhuǎn)Map List實(shí)現(xiàn)的幾種姿勢(shì)

    Java中List轉(zhuǎn)Map List實(shí)現(xiàn)的幾種姿勢(shì)

    本文主要介紹了Java中List轉(zhuǎn)Map List實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • 深入理解Java設(shè)計(jì)模式之單例模式

    深入理解Java設(shè)計(jì)模式之單例模式

    這篇文章主要介紹了JAVA設(shè)計(jì)模式之單例模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2021-11-11
  • Redis緩存,泛型集合與json字符串的相互轉(zhuǎn)換實(shí)例

    Redis緩存,泛型集合與json字符串的相互轉(zhuǎn)換實(shí)例

    這篇文章主要介紹了Redis緩存,泛型集合與json字符串的相互轉(zhuǎn)換實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • java自定義異常打印內(nèi)容詳解

    java自定義異常打印內(nèi)容詳解

    這篇文章主要為大家詳細(xì)介紹了java自定義異常打印內(nèi)容的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • 通過(guò)簡(jiǎn)單步驟實(shí)現(xiàn)SpringMVC文件上傳

    通過(guò)簡(jiǎn)單步驟實(shí)現(xiàn)SpringMVC文件上傳

    這篇文章主要介紹了通過(guò)簡(jiǎn)單步驟實(shí)現(xiàn)SpringMVC文件上傳,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Spring?Boot實(shí)現(xiàn)登錄驗(yàn)證碼功能的案例詳解

    Spring?Boot實(shí)現(xiàn)登錄驗(yàn)證碼功能的案例詳解

    驗(yàn)證碼的作用可以有效防止其他人對(duì)某一個(gè)特定的注冊(cè)用戶用特定的程序暴力破解方式進(jìn)行不斷的登錄嘗試,接下來(lái)通過(guò)本文給大家介紹Spring?Boot實(shí)現(xiàn)登錄驗(yàn)證碼功能,需要的朋友可以參考下
    2022-08-08
  • JavaWeb Struts文件上傳功能實(shí)現(xiàn)詳解

    JavaWeb Struts文件上傳功能實(shí)現(xiàn)詳解

    這篇文章主要為大家詳細(xì)介紹了JavaWeb Struts文件上傳功能實(shí)現(xiàn)過(guò)程,思路清晰,供大家參考,感興趣的小伙伴們可以參考一下
    2016-06-06
  • java的SimpleDateFormat線程不安全的幾種解決方案

    java的SimpleDateFormat線程不安全的幾種解決方案

    但我們知道SimpleDateFormat是線程不安全的,處理時(shí)要特別小心,要加鎖或者不能定義為static,要在方法內(nèi)new出對(duì)象,再進(jìn)行格式化,本文就介紹了幾種方法,感興趣的可以了解一下
    2021-08-08

最新評(píng)論