What is SQL to MyBatis Mapper Generator?
MyBatis is a widely adopted Java persistence framework that takes a semi-automated approach to ORM — developers write SQL directly while MyBatis handles the mapping between result sets and Java objects. Unlike full ORMs such as Hibernate, MyBatis gives you complete control over SQL statements, making it ideal for complex queries, stored procedures, and performance-critical applications. This converter produces three artifacts from your SQL CREATE TABLE statements: an XML mapper file with resultMap definitions and parameterized CRUD operations, a Java entity class with proper type mappings and optional Lombok annotations, and a typed Mapper interface with method signatures annotated with @Mapper.
How to Use
- Enter your Java package name (e.g., com.example.project) and author name in the configuration bar
- Select the target database type — MySQL, PostgreSQL, Oracle, or SQL Server — to get dialect-appropriate JDBC type mappings
- Toggle Lombok mode to generate @Data, @Builder, @NoArgsConstructor, and @AllArgsConstructor annotations instead of explicit getters and setters
- Paste your SQL CREATE TABLE statements and click Generate MyBatis to produce all three output files
- Switch between the XML Mapper, Entity Class, and Mapper Interface tabs to view and copy each generated file independently
Why Use This Tool?
Tips & Best Practices
- The generated <set> tag in updateByPrimaryKey only updates columns with non-null values, preventing accidental null overwrites of existing data
- For Oracle and SQL Server databases, adjust the generated SQL syntax if you need sequence-based ID generation instead of auto-increment
- Add @Param annotations manually when your mapper methods accept multiple parameters — the generated interface uses single-entity parameters
- Consider adding a <sql id="example_Where_Clause"> fragment for complex conditional queries beyond the basic selectByCondition method
Frequently Asked Questions
How are SQL types mapped to Java types in MyBatis?
The converter follows JDBC conventions: VARCHAR, CHAR, and TEXT map to String; INTEGER and SMALLINT map to Integer; BIGINT maps to Long; DECIMAL and NUMERIC map to java.math.BigDecimal for precise financial calculations; TIMESTAMP and DATETIME map to java.time.LocalDateTime; DATE maps to java.time.LocalDate; BOOLEAN maps to Boolean; and BLOB maps to byte[]. Nullable columns use wrapper types rather than primitives.
When should I avoid using MyBatis and choose a full ORM instead?
If your application has simple CRUD operations with no complex joins, a full ORM like Hibernate or JPA can reduce boilerplate further with auto-generated SQL. MyBatis shines when you need fine-grained SQL control, complex reporting queries, stored procedure calls, or database-specific optimizations that ORMs struggle to express.
Is my SQL data sent to a server?
No. All parsing and code generation runs entirely in your browser. Your SQL schema never leaves your device, and no network requests are made during the conversion process.
What is the resultMap element in the XML mapper?
A resultMap defines how SQL result set columns map to Java object properties. It specifies the column name, the Java property name, and the JDBC type. MyBatis uses this mapping to automatically populate your entity objects when executing SELECT queries.
Should I use Lombok in production code?
Lombok is widely used in production Java applications. It reduces boilerplate code for data classes, builders, and logging. The main trade-off is that it requires a compiler plugin or annotation processor, and IDE support must be configured. If your team prefers plain Java, disable Lombok to generate traditional getters and setters.
Real-world Examples
Generating a persistence layer for an e-commerce product catalog
An e-commerce platform needs MyBatis mappers for a products table with DECIMAL pricing, TEXT descriptions, and DATETIME timestamps, using Lombok for concise entity classes.
CREATE TABLE products ( id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT 'Product ID', name VARCHAR(200) NOT NULL COMMENT 'Product name', price DECIMAL(10,2) NOT NULL COMMENT 'Price in USD', description TEXT COMMENT 'Product description', created_at DATETIME DEFAULT CURRENT_TIMESTAMP );
@Data @Builder @NoArgsConstructor @AllArgsConstructor
public class Products {
/** Product ID */
private Long id;
/** Product name */
private String name;
/** Price in USD */
private BigDecimal price;
/** Product description */
private String description;
private LocalDateTime createdAt;
}Creating a mapper with dynamic SQL for conditional user searches
A user management system needs a selectByCondition query that filters by any combination of username, email, and active status using MyBatis dynamic SQL tags.
CREATE TABLE users ( id BIGINT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50) NOT NULL, email VARCHAR(255) NOT NULL, is_active TINYINT(1) DEFAULT 1 );
<select id="selectByCondition" parameterType="com.example.entity.Users" resultMap="UsersResultMap">
SELECT <include refid="Base_Column_List"/>
FROM users
<where>
<if test="username != null">
AND username = #{username, jdbcType=VARCHAR}
</if>
<if test="email != null">
AND email = #{email, jdbcType=VARCHAR}
</if>
<if test="isActive != null">
AND is_active = #{isActive, jdbcType=TINYINT}
</if>
</where>
</select>