Turn MyBatis Log Lines into Runnable SQL with a Single-Page Web Tool

What the tool does

Paste raw MyBatis log fragments that contain both the Preparing: line and the Parameters: line, and the page will stitch the placeholders and values together into a ready-to-run SQL statement. The tool is a single HTML file that works entirely in the browser—no server, no installlation.

Quick start

  1. Copy the HTML source below into a new file named mybatis-sql-parser.html.
  2. Open the file in any modern browser.
  3. Copy the log snipppet from your terminal or log file (keep the line breaks). Paste it into the top text area.
  4. Click Parse SQL. The executable SQL appears in the lower text area and can be copied to the clipboard.

Limitations

  • Does not generate multi-row INSERT statements. For bulk inserts see the Java helper at the end of this article.
  • Log must contain both Preparing: and Parameters: on separate lines.

Standalone HTML page

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>MyBatis SQL Parser</title>
  <style>
    body{font-family:Arial,Helvetica,sans-serif;margin:20px}
    textarea{width:100%;font-family:monospace;font-size:14px}
    button{margin:4px;padding:6px 12px}
  </style>
</head>
<body>
  <h2>Paste MyBatis log</h2>
  <textarea id="logInput" rows="10"></textarea>
  <br>
  <button onclick="clearLog()">Clear</button>
  <button onclick="parseSql()">Parse SQL</button>

  <h2>Executable SQL</h2>
  <textarea id="sqlOutput" rows="10" readonly></textarea>
  <br>
  <button onclick="copySql()">Copy</button>
  <span id="msg" style="color:green"></span>

  <script>
    function clearLog() { document.getElementById('logInput').value = ''; }
    function copySql() {
      const out = document.getElementById('sqlOutput');
      out.select();
      document.execCommand('copy');
      document.getElementById('msg').textContent = 'Copied!';
      setTimeout(() => document.getElementById('msg').textContent = '', 2000);
    }
    function parseSql() {
      const raw = document.getElementById('logInput').value;
      const prepLine = raw.match(/Preparing:\s*(.+)/);
      const paramLine = raw.match(/Parameters:\s*(.+)/);
      if (!prepLine || !paramLine) {
        alert('Cannot find Preparing: or Parameters: line');
        return;
      }
      let sql = prepLine[1].trim();
      const params = paramLine[1].split(',').map(p => {
        const [val, type] = p.trim().split(/[()]/);
        return { val: val.trim(), type: type.trim() };
      });
      params.forEach(p => {
        const replacement = /String|Timestamp|Date/.test(p.type)
          ? `'${p.val}'`
          : p.val;
        sql = sql.replace('?', replacement);
      });
      document.getElementById('sqlOutput').value = sql;
    }
  </script>
</body>
</html>

Generating bulk-insert statements in Java

When you need to insert thousands of rows and the web tool is not enough, the snippet below builds multi-value INSERT statements in batches of N rows.

@Test
public void buildBulkInserts() throws IOException {
    List<Category> all = categoryService.list(new QueryWrapper<Category>().last("limit 6"));
    if (all.isEmpty()) return;

    List<List<Category>> chunks = Lists.partition(all, 3);   // Guava
    for (List<Category> chunk : chunks) {
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO pms_category(name,parent_cid,cat_level,show_status,sort,icon,unit,count) VALUES ");
        for (int i = 0; i < chunk.size(); i++) {
            Category c = chunk.get(i);
            sb.append(String.format("('%s',%s,%s,%s,%s,'%s','%s',%s)",
                c.getName(), c.getParentCid(), c.getCatLevel(),
                c.getShowStatus(), c.getSort(), c.getIcon(), "unit", c.getProductCount()));
            if (i < chunk.size() - 1) sb.append(',');
        }
        sb.append(';');
        System.out.println(sb);
        Files.write(Paths.get("bulk-insert.sql"), sb.append(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
    }
}

Printing SQL in MyBatis-Plus

Three common ways to enable SQL logging:

  1. application.yml

    mybatis-plus:
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    
    
  2. application.yml – package-level logging

    logging:
      level:
        com.example.mapper: debug
    
    
  3. P6Spy (recommended for dev)
    Add dependency: ``` <groupId>p6.spy</groupId> <artifactId>p6spy</artifactId> 3.9.1

    
    Change driver and URL:  
    

    spring: datasource: driver-class-name: com.p6spy.engine.spy.P6SpyDriver url: jdbc:p6:spy:mysql://localhost:3306/db

    
    Create `spy.properties` with the content shown earlier in the article.
    
    

Note: P6Spy incurs overhead; disable it in production.

Tags: MyBatis sql html javascript P6Spy

Posted on Fri, 04 Sep 2026 16:26:41 +0000 by Carlo Gambino