json

48 阅读1分钟
DDL to Java Entity Generator body { font-family: Arial, sans-serif; } #inputSection, #outputSection { padding: 20px; } textarea { width: 100%; height: 150px; } #outputSection { background-color: #f9f9f9; } .output { height: 300px; margin-top: 10px; border: 1px solid #ccc; padding: 5px; background-color: #fff; white-space: pre-wrap; overflow: auto; font-family: monospace; } button { margin-right: 10px; margin-top: 10px; }

MySQL DDL Input

Convert to Java Entity

Generated Java Entity

<script>
    function convertToJava() {
        const ddl = document.getElementById('ddlInput').value;
        const javaOutput = document.getElementById('javaOutput');
        javaOutput.textContent = ''; // Clear previous output

        // Simple DDL to Java field type mappings
        const typeMappings = {
            'int': 'int',
            'varchar': 'String',
            'datetime': 'Date'
        };

        // Start building the Java class
        let javaClass = 'import java.util.Date;\n\n';
        javaClass += 'public class User {\n';

        // Split DDL into lines and process each line
        const lines = ddl.split('\n');
        lines.forEach(line => {
            line = line.trim(); // Trim whitespace

            if (line.toUpperCase().startsWith('CREATE TABLE')) {
                // Extract table name and create Java class name if needed
                // ...
            } else if (line.endsWith(',')) {
                // Process column definition
                const columnDef = line.slice(0, -1).split(' ');
                const columnName = columnDef[0].replace(/`/g, ''); // remove backticks
                const dataType = columnDef[1].split('(')[0]; // extract datatype without length

                // Map SQL data types to Java data types
                const javaType = typeMappings[dataType.toLowerCase()] || 'Object';

                // Convert column name to Java field name (camelCase)
                const javaFieldName = columnName.split('_').map((part, index) => {
                    return index > 0 ? part.charAt(0).toUpperCase() + part.slice(1) : part;
                }).join('');

                // Append field to Java class
                javaClass += `    private ${javaType} ${javaFieldName};\n`;
            }
            // ... Add more processing if needed for other parts of the DDL
        });

        javaClass += '\n    // Getters and Setters\n';
        javaClass += '}\n';

        // Output to the text area
        javaOutput.textContent = javaClass;
    }
</script>