MyBatis XML ↔ Java Converter

Convert MyBatis Mapper XML to Java interface and vice versa.

ui.input
ui.output

What is MyBatis XML ↔ Java Conversion?

MyBatis is a persistence framework that maps SQL statements to Java methods using either XML mapper files or annotation-based Java interfaces. This tool converts between the two styles — if you have an XML mapper and want to migrate to annotation-based development, or vice versa, this tool generates the boilerplate for you instantly.

The XML style keeps SQL separated from Java code in *Mapper.xml files, while the Java annotation style embeds SQL directly in the interface using @Select, @Insert, @Update, and @Delete annotations. Both approaches are fully supported by MyBatis-Plus and Spring Boot's MyBatis integration.

XML vs Annotation Style

  • XML style — SQL is in separate .xml files under resources/mapper/. Easier to manage complex SQL, supports dynamic SQL with <if>, <foreach>, <choose>. Better for long queries.
  • Annotation style — SQL is in the Java interface. Less files to manage, faster for simple CRUD. Dynamic SQL is harder to write (requires @SelectProvider).
  • MyBatis-Plus — Provides BaseMapper<T> with built-in CRUD methods. Custom queries can use either style on top of it.
  • Mixed approach — Many Spring Boot projects use MyBatis-Plus for CRUD and XML for complex custom queries. This is the most common real-world pattern.

Spring Boot Configuration

  • XML mapper location: set mybatis.mapper-locations=classpath:mapper/*.xml in application.properties
  • Mapper scan: add @MapperScan("com.example.mapper") on the Spring Boot main class
  • For annotation style: just add @Mapper on the interface, no XML needed
  • Result mapping: use mybatis.configuration.map-underscore-to-camel-case=true to auto-map user_nameuserName

Frequently Asked Questions

Should I use XML or annotation style in new projects?

For new Spring Boot projects, use MyBatis-Plus with annotation/BaseMapper for standard CRUD, and XML for complex queries with dynamic conditions. Avoid putting multi-line SQL in annotations — it becomes unreadable. A good rule: if the SQL fits on one line, use an annotation; if it needs dynamic conditions or JOINs, use XML.

How do I handle dynamic SQL in annotations?

Use @SelectProvider(type = UserSqlProvider.class, method = "buildQuery") and implement the SQL builder in a separate provider class using MyBatis's SQL DSL. This keeps dynamic SQL out of annotations while avoiding XML files. For most cases though, XML <if> tags are simpler and more readable.

What is the difference between #{ } and ${ } in MyBatis?

#{param} is a parameterized placeholder — MyBatis uses a PreparedStatement and the value is safely escaped, preventing SQL injection. ${param} is string substitution — the value is inserted directly into the SQL string. Never use ${} with user-provided values. Only use it for things like dynamic table names or column names that cannot be parameterized.

How do I map a query result to a nested object?

Use <resultMap> in XML with <association> for one-to-one and <collection> for one-to-many relationships. In annotation style, use @Results with @One or @Many. For simple flat mappings, map-underscore-to-camel-case=true handles most column-to-field name differences automatically.