springboot單獨(dú)在指定地方輸出sql的方法
一般線上項(xiàng)目都是將日志進(jìn)行關(guān)閉,因?yàn)閙ybatis日志打印,時(shí)間長(zhǎng)了,會(huì)占用大量的內(nèi)存,如果我想在我指定的地方進(jìn)行打印sql情況,怎么玩呢!
下面這個(gè)場(chǎng)景:
某天線上的項(xiàng)目出bug了,日志打印出來(lái)是更新成功的,但是數(shù)據(jù)庫(kù)的數(shù)據(jù)卻沒(méi)有更新,這時(shí)候我想給執(zhí)行這條sql打印出來(lái),方便查看,但是一旦開(kāi)啟mybatis的日志打印功能,就回去全局打印所有的日志,我只想在我想打印的地方打印sql,這時(shí)候怎么辦呢!見(jiàn)下面的列子:
我想在下面紅色箭頭處打印這句sql
這時(shí)候怎么做呢,咱們可以這樣,先將SqlSessionFactory對(duì)象注入進(jìn)來(lái),
如下圖:
private final SqlSessionFactory sqlSessionFactory; @Autowired public LoginService(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; }
然后在需要打印的地方加上下面的代碼即可:
// 獲取執(zhí)行的SQL語(yǔ)句并打印 try (SqlSession sqlSession = sqlSessionFactory.openSession()) { Configuration sqlSessionConfiguration = sqlSession.getConfiguration(); BoundSql boundSql = sqlSessionConfiguration .getMappedStatement("com.green.testlocalhost.mapper.AdminUserMapper.selectOneByExample") .getSqlSource() .getBoundSql(userExample); String sql = StringUtils.getExecSql(sqlSessionConfiguration, boundSql); System.out.println("本次執(zhí)行的sql語(yǔ)句::" + sql); }
注意:com.green.testlocalhost.mapper.AdminUserMapper.selectOneByExample 這個(gè)是StatementId,也就是你主動(dòng)調(diào)用db框架的那個(gè)類或者接口的包路徑(com.green.testlocalhost.mapper.AdminUserMapper),然后在路徑后面拼接調(diào)用的方法(selectOneByExample )即可
用到的工具方法如下:
/** * 獲取執(zhí)行的sql語(yǔ)句 * @param configuration * @param boundSql * @return */ public static String getExecSql(Configuration configuration, BoundSql boundSql) { try { Object parameterObject = boundSql.getParameterObject(); List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); String sql = boundSql.getSql().replaceAll("[\\s]+", " "); if (!parameterMappings.isEmpty() && parameterObject != null) { TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry(); if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { sql = sql.replaceFirst("\\?", getParameterValue(parameterObject)); } else { MetaObject metaObject = configuration.newMetaObject(parameterObject); for (ParameterMapping parameterMapping : parameterMappings) { String propertyName = parameterMapping.getProperty(); Object obj; if (metaObject.hasGetter(propertyName)) { obj = metaObject.getValue(propertyName); sql = sql.replaceFirst("\\?", getParameterValue(obj)); } else if (boundSql.hasAdditionalParameter(propertyName)) { obj = boundSql.getAdditionalParameter(propertyName); sql = sql.replaceFirst("\\?", getParameterValue(obj)); } } } } return sql; } catch (Exception var11) { return ""; } } private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static String getParameterValue(Object obj) { String value; if (obj instanceof String) { value = "'" + obj + "'"; } else if (obj instanceof Date) { value = "'" + SIMPLE_DATE_FORMAT.format(obj) + "'"; } else if (obj != null) { value = obj.toString(); } else { value = ""; } return value; }
這樣就可以在你指定的代碼地方打印sql而不是全局打印sql了
以上就是springboot單獨(dú)在指定地方輸出sql的方法的詳細(xì)內(nèi)容,更多關(guān)于springboot輸出sql的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳談jpa中表的@OneToMany等關(guān)聯(lián)關(guān)系
這篇文章主要介紹了詳談jpa中表的@OneToMany等關(guān)聯(lián)關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Java將文件夾保留目錄打包為 ZIP 壓縮包并下載的教程詳解
這篇文章主要介紹了Java將文件夾保留目錄打包為 ZIP 壓縮包并下載的教程詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Java實(shí)現(xiàn)的漢語(yǔ)拼音工具類完整實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)的漢語(yǔ)拼音工具類,結(jié)合完整實(shí)例形式分析了java基于pinyin4j包實(shí)現(xiàn)編碼轉(zhuǎn)換的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11Spring Boot項(xiàng)目中實(shí)現(xiàn)文件上傳功能的示例
這篇文章主要介紹了Spring Boot項(xiàng)目中實(shí)現(xiàn)文件上傳功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Spring項(xiàng)目集成RabbitMQ及自動(dòng)創(chuàng)建隊(duì)列
這篇文章主要介紹了Spring項(xiàng)目集成RabbitMQ及自動(dòng)創(chuàng)建隊(duì)列,本文內(nèi)容分別在Spring(V5.2.6)和Spring Boot(V2.5.14)兩個(gè)項(xiàng)目中經(jīng)過(guò)了驗(yàn)證,需要的朋友可以參考下2024-02-02