java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Mybatis SQL日志转换可执行sql

Mybatis SQL日志如何转换为可执行sql

作者:nacrt

这篇文章主要介绍了Mybatis SQL日志如何转换为可执行sql问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

Mybatis SQL日志转换为可执行sql

mybatis log --> sql

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <script type="text/javascript">
        function f(obj) {
			getSql(obj.value);
        }
        function getSql(textVa) {
            // 获取带问号的SQL语句
            var statementStartIndex = textVa.indexOf('Preparing: ');
			if (statementStartIndex < 0) return;
            var statementEndIndex = textVa.length - 1;
            for (var i = statementStartIndex; i < textVa.length; i++) {
                if (textVa[i] == "\n") {
                    statementEndIndex = i;
                    break;
                }
            }
            var statementStr = textVa.substring(statementStartIndex + "Preparing: ".length, statementEndIndex);
            console.log(statementStr);
            //获取参数
            var parametersStartIndex = textVa.indexOf('Parameters: ');
            var parametersEndIndex = textVa.length - 1;
            for (var i = parametersStartIndex; i < textVa.length; i++) {
                if (textVa[i] == "\n") {
                    parametersEndIndex = i;
                    break;
                } else {
                    console.log(textVa[i]);
                }
            }
            var parametersStr = textVa.substring(parametersStartIndex + "Parameters: ".length, parametersEndIndex);
            parametersStr = parametersStr.split(",");
            console.log(parametersStr);
            for (var i = 0; i < parametersStr.length; i++) {
                // 如果数据中带括号将使用其他逻辑
                tempStr = parametersStr[i].substring(0, parametersStr[i].indexOf("("));
                // 获取括号中内容
                typeStr = parametersStr[i].substring(parametersStr[i].indexOf("(") + 1, parametersStr[i].indexOf(")"));
                // 如果为字符类型或时间 加''
                if (typeStr == "String" || typeStr == "Timestamp") {
                    statementStr = statementStr.replace("?", "'" + tempStr.trim() + "'");
                } else {
                    // 数值类型
                    statementStr = statementStr.replace("?", tempStr.trim());
                }
            }
            console.log(statementStr);
            document.getElementById("d1").innerHTML += statementStr + "\n-- ---------------------------------------------------------------------------------------------------------------------\n";
			var newText = textVa.substring(parametersStartIndex + parametersStr.length);
			if (newText <= 0) return;
			getSql(newText);
        }
        function copySQL() {
            var SQL = document.getElementById("d1");
            SQL.select(); // 选择对象
            document.execCommand("Copy"); // 执行浏览器复制命令
            var msg = document.getElementById("msg");
            msg.innerHTML = "已复制到剪切板";
            setTimeout(function () {
                msg.innerHTML = "";
            }, 3000);
        }
        function clearLog(obj) {
            obj.select();
            obj.value = "";
        }
        function clearOut(obj) {
			obj.innerHTML = "";
        }
    </script>
</head>
<body>
<h2><font color="#00bfff"> 输入Mybatis SQL日志:</font></h2>
<textarea id="sqlLog" rows="13" cols="140" style="font-size:20px"></textarea>
<div style="border:0px deepskyblue solid;width:1425px;height:50px;text-align:right">
    <button style="color:mediumblue;width:100px;height:60px" type="button"
            onclick="clearLog(document.getElementById('sqlLog'))">
        清空输入
    </button>
    <button style="color:mediumblue;width:100px;height:60px" type="button"
            onclick="clearOut(document.getElementById('d1'))">
        清空输出
    </button>
    <button style="color:mediumblue;width:100px;height:60px" type="submit"
            onclick="f(document.getElementById('sqlLog'))">
        解析SQL
    </button>
</div>
<h2><font color="#32cd32">解析为可执行SQL:</font></h2>
<textarea id="d1" rows="13" cols="140" style="font-size:20px" readonly></textarea>
<div style="border:0px deepskyblue solid;width:1425px;height:50px;text-align:right">
    <button style="color:mediumblue;width:100px;height:60px" type="button" onclick="copySQL()">复制SQL</button>
</div>
<div id="msg"
     style="color:cornflowerblue;border:0px black solid;width:800px;height:20px;text-align:right;font-style: initial;font-size: large">
</div>
</body>
</html>

效果如图:

mybatis sql日志转换

ssm框架中,mybatis的sql语句日志输出

mybatis-config.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
         <!--打印查询语句-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
</configuration>

注意:

在增加mybatis-config.xml后,需要在spring与mybatis的整合配置文件(spring-mybatis.xml)中增加映射才可生效,

如下:

<!-- spring和MyBatis整合-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!-- 自动扫描mybatis配置文件 -->
    <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    <!-- 自动扫描mapping.xml文件 -->
    <property name="mapperLocations" value="classpath:com/merch/mapping/*.xml"></property>
</bean>

log4j.properties

#log4j.rootLogger=DEBUG,CONSOLE,A
#log4j.addivity.org.apache=false
#log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
#log4j.appender.CONSOLE.Threshold=DEBUG
#log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} -%-4r [%t] %-5p  %x - %m%n
#log4j.appender.CONSOLE.Target=System.out
#log4j.appender.CONSOLE.Encoding=UTF-8
#log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
#log4j.appender.A=org.apache.log4j.DailyRollingFileAppender
##log4j.appender.A.File=D:/logs/log.log4j
#log4j.appender.A.DatePattern=yyyy-MM-dd'.log'
#log4j.appender.A.layout=org.apache.log4j.PatternLayout
#log4j.appender.A.layout.ConversionPattern=[FH_sys]  %d{yyyy-MM-dd HH\:mm\:ss} %5p %c{1}\:%L \: %m%n
# 这是更简单的配置 , 忽略debug信息 , 只专注于业务
log4j.rootLogger = WARN,C
## console ###
log4j.appender.C = org.apache.log4j.ConsoleAppender
log4j.appender.C.Target = System.out
# override the  value of rootLogger
log4j.appender.C.Threshold =DEBUG
log4j.appender.C.layout = org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern = [%p][%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文