mybatis-plus配置攔截器實(shí)現(xiàn)sql完整打印的代碼設(shè)計(jì)
在使用mybatis-plus(mybatis)的時(shí)候,往往需要打印完整的sql語句,然而輸出的日志不是很理想:


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

可以看到了sql的執(zhí)行時(shí)間和完整的sql語句。sql的執(zhí)行時(shí)間沒啥好說的,關(guān)鍵是sql語句的完整打印。現(xiàn)在先來分享一下代碼吧。
代碼
controller的設(shè)計(jì)
這里僅展示關(guān)鍵的代碼,一個(gè)更新的操作,一個(gè)分頁查詢的操作。
@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 用來管理 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 方法,如果存在就取出來進(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匹配不上,帶上“缺失”方便找問題
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看一下。
配置類
這里的配置我都寫在了mybatis-plus的配置代碼里邊。
@Configuration
@MapperScan(value = "main.java.shigen.demo.dao")
public class MybatisPlusConfig {
?
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 分頁插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
// 樂觀鎖插件
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
?
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> {
configuration.addInterceptor(new SqlInterceptor());
};
}
}
以上就是核心的代碼了,實(shí)測(cè)遇到的問題有一個(gè):
- 分頁查詢的時(shí)候,無法顯示
limit 0,10這個(gè)sql后綴
希望有時(shí)間的時(shí)候能夠再次優(yōu)化一下。同時(shí),也沒有經(jīng)過實(shí)際的項(xiàng)目測(cè)試,只是簡單的demo測(cè)試。僅具有參考價(jià)值,無法保證實(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)文章希望大家以后多多支持腳本之家!
- MybatisPlus攔截器如何實(shí)現(xiàn)數(shù)據(jù)表分表
- MyBatis-Plus攔截器實(shí)現(xiàn)數(shù)據(jù)權(quán)限控制的方法
- Mybatis-plus通過添加攔截器實(shí)現(xiàn)簡單數(shù)據(jù)權(quán)限
- MybatisPlusInterceptor實(shí)現(xiàn)sql攔截器超詳細(xì)教程
- MyBatis-Plus攔截器實(shí)現(xiàn)數(shù)據(jù)權(quán)限控制的示例
- mybatis-plus 攔截器敏感字段加解密的實(shí)現(xiàn)
- MyBatis-Plus攔截器對(duì)敏感數(shù)據(jù)實(shí)現(xiàn)加密
- mybatis-plus攔截器、字段填充器、類型處理器、表名替換、SqlInjector(聯(lián)合主鍵處理)
- mybatisplus 的SQL攔截器實(shí)現(xiàn)關(guān)聯(lián)查詢功能
- Mybatis Plus 3.4.0分頁攔截器的用法小結(jié)
相關(guān)文章
Java中List轉(zhuǎn)Map List實(shí)現(xiàn)的幾種姿勢(shì)
本文主要介紹了Java中List轉(zhuǎn)Map List實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Redis緩存,泛型集合與json字符串的相互轉(zhuǎn)換實(shí)例
這篇文章主要介紹了Redis緩存,泛型集合與json字符串的相互轉(zhuǎn)換實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
通過簡單步驟實(shí)現(xiàn)SpringMVC文件上傳
這篇文章主要介紹了通過簡單步驟實(shí)現(xiàn)SpringMVC文件上傳,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Spring?Boot實(shí)現(xiàn)登錄驗(yàn)證碼功能的案例詳解
驗(yàn)證碼的作用可以有效防止其他人對(duì)某一個(gè)特定的注冊(cè)用戶用特定的程序暴力破解方式進(jìn)行不斷的登錄嘗試,接下來通過本文給大家介紹Spring?Boot實(shí)現(xiàn)登錄驗(yàn)證碼功能,需要的朋友可以參考下2022-08-08
JavaWeb Struts文件上傳功能實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了JavaWeb Struts文件上傳功能實(shí)現(xiàn)過程,思路清晰,供大家參考,感興趣的小伙伴們可以參考一下2016-06-06
java的SimpleDateFormat線程不安全的幾種解決方案
但我們知道SimpleDateFormat是線程不安全的,處理時(shí)要特別小心,要加鎖或者不能定義為static,要在方法內(nèi)new出對(duì)象,再進(jìn)行格式化,本文就介紹了幾種方法,感興趣的可以了解一下2021-08-08

