当前位置: 移动技术网 > IT编程>开发语言>Java > Mybatis日志参数快速替换占位符工具的详细步骤

Mybatis日志参数快速替换占位符工具的详细步骤

2020年08月05日  | 移动技术网IT编程  | 我要评论

mybatis log printf工具网页地址:

mybatis执行的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里有一个插件mybatis log plugin可以帮我们快速的提取参数拼成完整的sql语句执行,以快速排错,但是很可惜,他是收费的ε=(´ο`*)))唉,整来整取也没法破解,算了,不如自己写一个挂到公网上,也能复制sql随时拼接,纯js即可。

下面我们来逐步分析一下需要的步骤:

  1. 首先需要提取出preparedstatement语句
  2. 提取出所有的参数,string类型还需要手动添加引号
  3. 将statement中的占位符?替换为对应的参数

很简单吧,就这三步即可。接下来动手操作。

1、提取statement:只需截取从preparing到行尾的\n即可

// str为完整的三行或两行sql 提取预编译语句
let prepare = str.substring(str.indexof('preparing') + 11, str.indexof('\n'))

indexof(str: string):提取第一个匹配到的字符串的位置,返回其索引值

2、提取参数:只需截取从parameters到其行尾的\n即可

这时我们需要提取到str的第二个\n换行符,怎么提取某个字符串中的第n个字符串呢?

indexof()函数在js中是有重载的,默认提取第一个匹配的。它可以接受第二个参数,可以传入一个起始位置,即从position(索引)开始取第一个匹配的。

// js api
indexof(searchstring: string, position?: number): number;

分析:取第二个\n,我们可以将第一个\n的索引传入再加1;取第三个\n,则将第二个\n的索引传入加1,以此类推,所以这是一个递归函数,实现如下

// 返回字符串str中的第n字符串reg在str中的索引值index
function index(str, reg, n) {
	if (!str || !reg || n <= 0) return -1
	// 先求出第一个,再递归n-1
	if (n === 1) {
		return str.indexof(reg)
	}
	// 注意n-1的索引后一定要加1,负责会一直是第一个reg的索引
	return str.indexof(reg, index(str, reg, n - 1) + 1)
}

接下来先测试一下index函数,打印正确

const str = 'hello world ok'
const reg = '0'
console.log(index(str, reg, 3)) // 求第三个o的索引,打印结果是12,正确

完成函数提取,所以回到上面的步骤,继续提取第二个\n的位置

// 获取参数字符串,去掉所有空格
const params = str.substring(str.indexof('parameters') + 12, index(str, '\n', 2)).replace(/ /g, '')

获取参数后以逗号切割,返回是一个字符串参数数组

const array = params.split(',')
// ['2743672230717162752(long)','1(integer)','登录(string)','3(integer)']

提取完毕,则进行第三步的替换

3、替换参数

// 遍历数组,每次调用一次replace(old, new)即可,对字符串参数需要加上引号
array.map(item => {
	// 获取每个参数值
	let newvalue = item.substring(0, item.indexof('('))
	// 获取参数类型
	const type = item.substring(item.indexof('(') + 1, item.indexof(')'))
	if ('string' === type) {
		newvalue = "'" + newvalue + "'"
	}
	prepare = prepare .replace('?', newvalue)
	
})
// 遍历完毕,所有的占位符也就被参数替换完成啦
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())) 

这样我们就实现了整个的js遍历、寻找、替换逻辑,整个过程就是不停的去indexof(),substring()和replace(),是不是很简单呢,手写一个就不用了去找插件了。

我把这个网页放在了我的公网服务器上,访问地址是http://www.feedme.ltd/log.html
方便直接打开使用。

另外懒得写样式和dom,所以在线引用了vue.js和elemenui,可能会出现有时候加载js文件速度比较慢的情况。

最后贴出整个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="请复制输入mybatis打印的日志,须完整prepareing语句和parameter语句"
  :rows="28" style="width: 600px"></el-input>
 <el-button type="success" @click="convert" style="height: 60px; width: 80px;">转换</el-button>
 <el-input type="textarea" v-model="res" placeholder="输出结果"
  :rows="28" style="width: 600px"></el-input>
 </div>

 <script type="text/javascript">
 const app = new vue({
 el: '#app',
 data() {
 return {
  // 原始str
  pre: '',
  // 输出结果
  res: ''
 }
 },
 methods: {
 convert() {
  const str = this.pre
  if (str.indexof('preparing') == -1 || str.indexof('parameters') == -1) {
  this.$message({
  message: '请将preparing和parameters语句复制进来',
  type: 'error',
  center: true
  })
  }
  // str为完整的三行或两行sql 提取预编译语句
  let prepare = str.substring(str.indexof('preparing') + 11, str.indexof('\n'))
  // 获取参数,去空格
  const params = str.substring(str.indexof('parameters') + 12, index(str, '\n', 2)).replace(/ /g, '')
  // 参数数组
  const array = params.split(',')
  // 循环替换占位符,字符串方式替换每次替换第一个
  array.map(item => {
  // 获取每个参数值
  let newvalue = item.substring(0, item.indexof('('))
  // 获取参数类型
  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
 // 先求出第一个,再递归n-1
 if (n === 1) {
 return str.indexof(reg)
 }
 // 注意n-1的索引后一定要加1,负责会一直是第一个reg的索引
 return str.indexof(reg, index(str, reg, n - 1) + 1)
 }
	// 测试index函数
 const str = 'hello world ok'
 const reg = 'o'
 console.log(index(str, reg, 3))
 </script>
</body>
</html>

总结

到此这篇关于mybatis日志参数快速替换占位符工具的文章就介绍到这了,更多相关mybatis日志参数替换占位符内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网