Backend Development 4 min read

Two Ways to Instantiate org.apache.ibatis.jdbc.SQL in MyBatis

This article explains two techniques for creating an org.apache.ibatis.jdbc.SQL object in MyBatis—using an anonymous inner class with an initializer block and using method‑chaining builder style—provides code examples, explains the underlying Java syntax, and summarizes their differences.

Cognitive Technology Team
Cognitive Technology Team
Cognitive Technology Team
Two Ways to Instantiate org.apache.ibatis.jdbc.SQL in MyBatis

The article introduces two ways to instantiate org.apache.ibatis.jdbc.SQL when building SQL statements with MyBatis.

1. Anonymous inner class with an initializer block

By creating an anonymous subclass of SQL and placing the SQL construction calls inside a double‑brace block, the statements are executed during object creation. The following example demonstrates this approach:

package com.example.demo;
import org.apache.ibatis.jdbc.SQL;

public class SQLDemo {
    public static void main(String[] args) {
        String sql = new SQL() {{
            FROM("demo_table");
            SELECT("a");
            SELECT("b");
            WHERE(" id > 1");
            WHERE(" b > 2");
            ORDER_BY("id ");
            LIMIT(10);
        }}.toString();
        System.out.println(sql);

        sql = new SQL() {{
            FROM("demo_table");
            SELECT("a");
            SELECT("b");
            WHERE(" id > 1");
            WHERE(" b > 2");
            ORDER_BY("id ");
            LIMIT(10);
        }}.toString();
        System.out.println(sql);
    }
}

The double braces consist of an anonymous class definition ( new SQL() {} ) and an instance‑initializer block ( { ... } ). Adding static {} would create a static initializer.

Each execution of this pattern generates a separate anonymous class, which can be observed in the compiled bytecode and decompiled class files.

2. Method‑chaining (Builder pattern)

Alternatively, SQL provides fluent methods that return the same instance, enabling a builder‑style chain:

package com.example.demo;
import org.apache.ibatis.jdbc.SQL;

public class SQLDemo {
    public static void main(String[] args) {
        String sql = new SQL()
                .FROM("demo_table")
                .SELECT("a")
                .SELECT("b")
                .WHERE(" id > 1")
                .WHERE(" b > 2")
                .ORDER_BY("id ")
                .LIMIT(10)
                .toString();
        System.out.println(sql);

        sql = new SQL()
                .FROM("demo_table")
                .SELECT("a")
                .SELECT("b")
                .WHERE(" id > 1")
                .WHERE(" b > 2")
                .ORDER_BY("id ")
                .LIMIT(10)
                .toString();
        System.out.println(sql);
    }
}

This style mirrors the classic Builder pattern, where each configuration method returns this to allow chaining.

Internally, SQL implements a helper method such as:

public SQL getSelf() {
    return this;
}

Conclusion

The article compares the anonymous‑class initializer approach and the fluent method‑chaining approach for constructing SQL strings with MyBatis, highlighting their syntax, generated bytecode characteristics, and typical use cases.

JavaSQLMyBatisBuilder PatternMethod Chaining
Cognitive Technology Team
Written by

Cognitive Technology Team

Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.