The original link

During the epidemic, the big house at home is not fast suffocating out of the “disease” ~~

The company opened VPN, mobile phone and computer can be connected, mobile phone installed APP test package, can work, so Walking has been at home for 11 days since he entered Beijing on February 1, 2020. Telecommuting these two days, and I predict working from home next week.

During the recent isolation period at home, I actually studied less and played more. This is not, today when looking at Mybatis source code found a good Mybatis plug-in and a conversion tool, today I rushed to write out to share with everyone.

We should all have used Mybatis in our work. Sometimes when we debug locally, we will open the SQL log printing of Mybatis, so the printed SQL is like the following figure

You can see pre-compiled SQL conditions using placeholders (?). Select * from User where id =? Select * from User where id = 1;

If we want to get real SQL, we can manually spell the values of the parameters as shown in the figure above, but what if we have too many parameters? Is not their own manual filling is very troublesome.

Preparing and Parameters for Mybatis logs into executable SQL.

One is mybatis log plugin for IDEA, the other is no condition to install this plugin or no IDEA, a static page.

The first Mybatis log plugin

Search mybatis log Plugin in setting-plugins of Idea, as shown below, click Install, and restart Idea as prompted. (The following installation has been completed)

We can then right-click the SQL log and select Restore SQL from Selection

Then you can see the actual SQL in the Mybatis Log window

Then it can be copied and executed elsewhere

Second static page tool

Another clever student created a static page to be used when the plugin was not available.

The original author’s article address: blog.csdn.net/Zale_J/arti…

Just need to make an HTML file, and then the author of the source pasted in, and then save, open with a browser, and then mybatis log pasted in click “convert” to get the real SQL. I also posted the source code below.

Effect:

The source code:

 
      
 <html>
 <head>
   <meta charset="utf-8">
   <title></title>
   <script type="text/javascript">
     function f(obj){
       var textVa = obj.value;
       // Get the SQL statement with the question mark
       var statementStartIndex = textVa.indexOf('Preparing: ');
       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);
       // Get parameters
       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++) {
         // Other logic will be used if there are parentheses in the data
         tempStr = parametersStr[i].substring(0, parametersStr[i].indexOf("("));
         // Get the contents of the parentheses
         typeStr = parametersStr[i].substring(parametersStr[i].indexOf("(") +1,parametersStr[i].indexOf(")"));
         // If it is a character type
         if (typeStr == "String" || typeStr == "Timestamp") {
           statementStr = statementStr.replace("?"."'"+tempStr.trim()+"'");
         }else{
           // A numeric type
           statementStr = statementStr.replace("?", tempStr.trim()); }}console.log(statementStr);
       document.getElementById("d1").innerHTML = statementStr;
       return textVa;
     }
</script>
 </head>
 <body>
    <textarea   name="getStr" id="1" rows="4" cols="100"></textarea>
    <button type="submit" onclick="f(document.getElementById('1'))">conversion</button>
  <div id="d1"></div>
 </body>
 </html>
Copy the code

There is another netizen, said the original author has a bug, specific what bug also did not say, should do some optimization of what bar. I didn’t verify anything.

Do the optimization of the original: www.cnblogs.com/n031/p/1117…

Here’s how it looks:

Optimized code:


      
<html>
<head>
    <meta charset="utf-8">
    <title>Mybatis conversion</title>
    <script type="text/javascript">
        function f(obj) {
            var textVa = obj.value;
            // Get the SQL statement with the question mark
            var statementStartIndex = textVa.indexOf('Preparing: ');
            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);
            // Get parameters
            var parametersStartIndex = textVa.indexOf('Parameters: ');
            var parametersEndIndex = textVa.length;
            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);
            console.log(parametersStr);
            // Parameter list
            var parametersStrArr = parametersStr.split(",");
            console.log(parametersStrArr);
            for (var i = 0; i < parametersStrArr.length; i++) {
                tempStr = parametersStrArr[i].substring(0, parametersStrArr[i].indexOf("("));
                // no "(" is null
                if(tempStr == ' '){
                    tempStr = "null";
                }
                // If there are parentheses in the data, you need to determine the parameter type
                typeStr = parametersStrArr[i].substring(parametersStrArr[i].indexOf("(") + 1, parametersStrArr[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;
            return true;
        }
</script>
</head>
<body>
    <button type="submit" onclick="f(document.getElementById('1'))">conversion</button><br><br>
    <textarea style="border:blue solid 2px;" name="getStr" id="1" rows="25" cols="150"></textarea><br>
    <p style="color:red; font:30px bold;">Your SQL</p>
    <div style="border:red solid 2px;" id="d1"></div>
</body>
</html>
Copy the code

The specific use of these two methods depends on the situation. Thanks to both authors for their ingenuity and dedication.

If you find it useful, share it with your friends, or save it if you don’t have time to do it now

Come on, Wuhan! Come on China!!


Welcome to the public number: Programming avenue