MyJavaCode.com

Tutorial and How to Guide on Java Programming. Topics covered include Java Programming Language - Core Java, Spring, Webservices, REST, Hibernate, Maven and Microservices.

  • Core Java
  • REST API’s
  • Spring boot
  • Hibernate
  • Interview Questions
  • Contact Us
  • Projects
  • Offerings

Externalizing MyBatis SQL Queries in a Spring Boot Application

May 8, 2025 By Prasanna Manjunatha

As applications grow in complexity, managing SQL queries directly within the codebase can become cumbersome and error-prone. MyBatis, a popular persistence framework, offers several options for managing SQL statements in a Spring Boot application. This blog post explores how to externalize SQL queries, including the various methods supported by MyBatis for writing and organizing SQL queries, and discusses the pros and cons of externalization in detail.

Methods Supported by MyBatis for Writing SQL Queries

MyBatis provides flexibility in how SQL queries can be defined and managed. Here are the primary methods:

  1. XML Configuration: Traditionally, MyBatis uses XML files to define SQL queries. This allows SQL to be completely separated from Java code, making it easy to read and modify. XML mappers can be linked directly to mapper interfaces in the Java code.

  2. Annotations: For simpler use cases, MyBatis allows SQL queries to be embedded directly in annotations within mapper interfaces. This method is straightforward and keeps the SQL close to the Java methods that execute them, aiding in understanding and debugging.

  3. Externalized SQL Properties: SQL queries can be externalized into properties files, which is useful for keeping SQL code out of both Java and XML files. This approach can also facilitate easier changes to SQL without redeploying the application.

  4. Dynamic SQL with Scripting: MyBatis supports dynamic SQL generation using scripting elements within XML configurations or annotated methods. This allows for SQL that adapts to different conditions at runtime, such as varying column names, sorting orders, or where conditions.

Also Read: Automate Audit Fields in Spring Boot with MyBatis: A Generic Solution for CRUD Operations

How to Externalize SQL Queries in Spring Boot

Here’s a step-by-step guide on how to manage MyBatis SQL queries in a Spring Boot application by externalizing them into a separate YAML configuration file:

1: Setup Your Spring Boot Project

Start with a Spring Boot setup including dependencies for MyBatis and your database connector.

2: Create a Separate YAML File for SQL Queries

Create sql-queries.yaml in the src/main/resources directory to store your SQL queries.

sql-queries.yaml:

sql:
  userMapper:
    selectUser: |
      SELECT * FROM users WHERE id = #{id}"
    insertUser: |
      INSERT INTO users (name, email) VALUES (#{name}, #{email})

3: Reference the SQL Queries YAML in your main application.yaml

Include sql-queries.yaml in your main configuration.

application.yaml:

spring:
  config:
    import: "classpath:sql-queries.yaml"

Also Read: How to Extract Private Key from Java Keystore (.JKS) or .P12 File

4: Configure SQL Queries in Spring Boot

Setup SqlQueryConfig to load and provide SQL queries.

SqlQueryConfig.java:

@Configuration
@ConfigurationProperties(prefix = "sql")
public class SqlQueryConfig {
private Map<String, Map<String, String>> queries;
public String getQuery(String mapperName, String methodName) {
return queries.getOrDefault(mapperName, Map.of()).getOrDefault(methodName, "Query not found");
}
}

 

5: Use SqlProvider for Dynamic SQL Retrieval

Implement SqlProvider to fetch SQL using reflection.

SqlProvider.java:

public class SqlProvider {
public static String genericSqlProvider(ProviderContext context) {
String mapperClassName = context.getMapperType().getSimpleName();
String methodName = context.getMapperMethod().getName();
return SqlQueryConfig.getQuery(mapperClassName, methodName);
}
}

 

6: Configure MyBatis Mapper Interfaces

Use the @SelectProvider and other provider annotations in your mappers.

UserMapper.java:

@Mapper
public interface UserMapper {
@SelectProvider(type = SqlProvider.class, method = "genericSqlProvider")
User selectUser(int id);
}

Pros of Externalizing SQL Queries

  • Separation of Concerns: SQL and business logic are decoupled, enhancing maintainability.
  • Flexibility: SQL changes do not require Java code modifications, facilitating easier updates and deployments.
  • Enhanced Collaboration: Allows non-developers, such as DBAs, to manage SQL directly.

Cons of Externalizing SQL Queries

  • Initial Setup Complexity: Requires a setup that might be initially complex to implement.
  • Debugging Challenges: Separating SQL from the code can complicate debugging and tracing.
  • Performance Considerations: Depending on the method of loading and accessing SQL, there may be performance implications.

Conclusion

Externalizing SQL queries in a Spring Boot application using MyBatis offers a structured approach to database interaction management. This method supports scalability and improves maintainability, making it ideal for large or growing applications. While it introduces some complexities, particularly during setup and debugging, the organizational benefits often outweigh these challenges.

email
print

About Prasanna Manjunatha

Prasanna is a Toronto based Java Consultant with more than 15 years of Software Development experience. His areas of expertise include Core Java, Spring boot, REST, Microservices, Hibernate, NoSQL, Docker, Kubernetes and AWS.

Currently Trending

Connect with us

  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

TAG CLOUD

Axis2 CodeCoverage Code Coverage cron expression Data Structure Frameworks Hashing HashMap Hibernate IntelliJ java Java Mail jboss jpa 2.1 Maven MyBatis MySQL PDF Quartz REST REST API SOAP Spring boot Springboot SQL Tools Tutorial web service

All time Popular Posts

Copyright © 2025 MyJavaCode.com