您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了Mybatis日志參數(shù)如何快速替換占位符工具,內(nèi)容簡(jiǎn)而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來看看吧。
Mybatis執(zhí)行的sql的打印格式為:
2020-08-04 09:16:44 -DEBUG - [io-8888-exec-5] .mapper.operation.OperationMapper.insert. debug 145 : ==> Preparing: INSERT INTO tulu.t_log_operation (id, module, module_description, type, method, operator, operate_time) VALUES (?, ?, ?, ?, ?, ?, unix_timestamp(now()))
2020-08-04 09:16:44 -DEBUG - [io-8888-exec-5] .mapper.operation.OperationMapper.insert. debug 145 : ==> Parameters: 2743672230717162752(Long), 1(Integer), 登錄(String), 3(Integer), com.simba.tuloosa.controller.auth.LoginController.nativeLogin(String), 6d63b98cbe5e42d18c126da149162404(String)
2020-08-04 09:16:44 -DEBUG - [io-8888-exec-5] .mapper.operation.OperationMapper.insert. debug 145 : <== Updates: 1
IDEA里有一個(gè)插件Mybatis log plugin可以幫我們快速的提取參數(shù)拼成完整的SQL語句執(zhí)行,以快速排錯(cuò),但是很可惜,他是收費(fèi)的ε=(´ο`*)))唉,整來整取也沒法破解,算了,不如自己寫一個(gè)掛到公網(wǎng)上,也能復(fù)制sql隨時(shí)拼接,純js即可。
下面我們來逐步分析一下需要的步驟:
很簡(jiǎn)單吧,就這三步即可。接下來動(dòng)手操作。
1、提取statement:只需截取從Preparing到行尾的\n即可
// str為完整的三行或兩行SQL 提取預(yù)編譯語句 let prepare = str.substring(str.indexOf('Preparing') + 11, str.indexOf('\n'))
indexOf(str: string):提取第一個(gè)匹配到的字符串的位置,返回其索引值
2、提取參數(shù):只需截取從Parameters到其行尾的\n即可
這時(shí)我們需要提取到str的第二個(gè)\n換行符,怎么提取某個(gè)字符串中的第n個(gè)字符串呢?
indexOf()函數(shù)在js中是有重載的,默認(rèn)提取第一個(gè)匹配的。它可以接受第二個(gè)參數(shù),可以傳入一個(gè)起始位置,即從position(索引)開始取第一個(gè)匹配的。
// js api indexOf(searchString: string, position?: number): number;
分析:取第二個(gè)\n,我們可以將第一個(gè)\n的索引傳入再加1;取第三個(gè)\n,則將第二個(gè)\n的索引傳入加1,以此類推,所以這是一個(gè)遞歸函數(shù),實(shí)現(xiàn)如下
// 返回字符串str中的第n字符串reg在str中的索引值index function index(str, reg, n) { if (!str || !reg || n <= 0) return -1 // 先求出第一個(gè),再遞歸n-1 if (n === 1) { return str.indexOf(reg) } // 注意n-1的索引后一定要加1,負(fù)責(zé)會(huì)一直是第一個(gè)reg的索引 return str.indexOf(reg, index(str, reg, n - 1) + 1) }
接下來先測(cè)試一下index函數(shù),打印正確
const str = 'hello world ok' const reg = '0' console.log(index(str, reg, 3)) // 求第三個(gè)o的索引,打印結(jié)果是12,正確
完成函數(shù)提取,所以回到上面的步驟,繼續(xù)提取第二個(gè)\n的位置
// 獲取參數(shù)字符串,去掉所有空格 const params = str.substring(str.indexOf('Parameters') + 12, index(str, '\n', 2)).replace(/ /g, '')
獲取參數(shù)后以逗號(hào)切割,返回是一個(gè)字符串參數(shù)數(shù)組
const array = params.split(',') // ['2743672230717162752(Long)','1(Integer)','登錄(String)','3(Integer)']
提取完畢,則進(jìn)行第三步的替換
3、替換參數(shù)
// 遍歷數(shù)組,每次調(diào)用一次replace(old, new)即可,對(duì)字符串參數(shù)需要加上引號(hào) array.map(item => { // 獲取每個(gè)參數(shù)值 let newValue = item.substring(0, item.indexOf('(')) // 獲取參數(shù)類型 const type = item.substring(item.indexOf('(') + 1, item.indexOf(')')) if ('String' === type) { newValue = "'" + newValue + "'" } prepare = prepare .replace('?', newValue) }) // 遍歷完畢,所有的占位符也就被參數(shù)替換完成啦 console.log(prepare) // INSERT INTO tulu.t_log_operation (id, module, module_description, type, method, operator, operate_time) VALUES (2743672230717162752, 1, '登錄', 3, 'com.simba.tuloosa.controller.auth.LoginController.nativeLogin', '6d63b98cbe5e42d18c126da149162404', unix_timestamp(now()))
這樣我們就實(shí)現(xiàn)了整個(gè)的js遍歷、尋找、替換邏輯,整個(gè)過程就是不停的去indexOf(),substring()和replace(),是不是很簡(jiǎn)單呢,手寫一個(gè)就不用了去找插件了。
我把這個(gè)網(wǎng)頁放在了我的公網(wǎng)服務(wù)器上,訪問地址是http://www.feedme.ltd/log.html
方便直接打開使用。
另外懶得寫樣式和DOM,所以在線引用了vue.js和elemenui,可能會(huì)出現(xiàn)有時(shí)候加載js文件速度比較慢的情況。
最后貼出整個(gè)HTML代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Mybatis Log Helper</title> <meta name=viewport content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" /> <link rel="shortcut icon" href="" /> <script src="https://unpkg.com/vue@2.6.11/dist/vue.js"></script> <!-- 引入樣式 --> <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css" rel="external nofollow" > <!-- 引入組件庫 --> <script src="https://unpkg.com/element-ui/lib/index.js"></script> <style> #app { margin-top: 70px; display: flex; justify-content: space-evenly; align-items: center; font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微軟雅黑", Arial, sans-serif; } </style> </head> <body> <div id="app"> <el-input type="textarea" v-model="pre" placeholder="請(qǐng)復(fù)制輸入Mybatis打印的日志,須完整Prepareing語句和Parameter語句" :rows="28" ></el-input> <el-button type="success" @click="convert" >轉(zhuǎn)換</el-button> <el-input type="textarea" v-model="res" placeholder="輸出結(jié)果" :rows="28" ></el-input> </div> <script type="text/javascript"> const app = new Vue({ el: '#app', data() { return { // 原始str pre: '', // 輸出結(jié)果 res: '' } }, methods: { convert() { const str = this.pre if (str.indexOf('Preparing') == -1 || str.indexOf('Parameters') == -1) { this.$message({ message: '請(qǐng)將Preparing和Parameters語句復(fù)制進(jìn)來', type: 'error', center: true }) } // str為完整的三行或兩行SQL 提取預(yù)編譯語句 let prepare = str.substring(str.indexOf('Preparing') + 11, str.indexOf('\n')) // 獲取參數(shù),去空格 const params = str.substring(str.indexOf('Parameters') + 12, index(str, '\n', 2)).replace(/ /g, '') // 參數(shù)數(shù)組 const array = params.split(',') // 循環(huán)替換占位符,字符串方式替換每次替換第一個(gè) array.map(item => { // 獲取每個(gè)參數(shù)值 let newValue = item.substring(0, item.indexOf('(')) // 獲取參數(shù)類型 const type = item.substring(item.indexOf('(') + 1, item.indexOf(')')) if ('String' === type) { newValue = "'" + newValue + "'" } prepare = prepare .replace('?', newValue) }) this.res = prepare } } }) // 返回字符串str中的第n字符串reg在str中的索引值index function index(str, reg, n) { if (!str || !reg || n <= 0) return -1 // 先求出第一個(gè),再遞歸n-1 if (n === 1) { return str.indexOf(reg) } // 注意n-1的索引后一定要加1,負(fù)責(zé)會(huì)一直是第一個(gè)reg的索引 return str.indexOf(reg, index(str, reg, n - 1) + 1) } // 測(cè)試index函數(shù) const str = 'hello world ok' const reg = 'o' console.log(index(str, reg, 3)) </script> </body> </html>
以上就是關(guān)于Mybatis日志參數(shù)如何快速替換占位符工具的內(nèi)容,如果你們有學(xué)習(xí)到知識(shí)或者技能,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。