Dynamic SQL Techniques in MyBatis
Understandign Dynamic SQL in MyBatis
When building real-world applications, SQL statements often need to adapt based on varying conditions. For instance, during user registration, some fields may be required while others are optional. This variability makes static SQL insufficient, which is where MyBatis dynamic SQL becomes invaluable.
This gui ...
Posted on Sun, 18 Oct 2026 16:12:59 +0000 by stephenalistoun
MyBatis Essential Patterns and Best Practices
Insert Operations
Retrieving Auto-Generated Primary Keys
<insert id="saveCustomer" useGeneratedKeys="true" keyProperty="customerId"
parameterType="com.example.domain.Customer">
INSERT INTO customer(name, points, created_at)
VALUES (#{name}, #{points}, #{createdAt})
</insert>
Batch ...
Posted on Mon, 21 Sep 2026 16:18:09 +0000 by desmortes
Dynamic SQL Date and Loop Queries
Dynamic SQL for Date Range and List Queries
When building dynamic queries in MyBatis, it's common to filter by date ranges and iterate over a list of values. Below is an example showing how to handle both scenarios using MyBatis dynamic SQL with where, if, and foreach tags.
SQL Mapping
<select id="selectQuotaManagement" resultType= ...
Posted on Wed, 09 Sep 2026 16:57:14 +0000 by theDog
MyBatis ORM Framework Implementation
MyBatis Core Concepts
MyBatis is a Java persistence framework that simplifies database interactions through SQL mapping. As a semi-automated ORM tool, it bridges Java objects and relasional databases by mapping:
Objects: Java application entities
Relations: Database table structures
Mappping: Bidirectional conversion between records and object ...
Posted on Sun, 09 Aug 2026 16:38:31 +0000 by carsonk
MyBatis SQL Query Implementation Techniques
UNION operations combine results from multiple tables while eliminating duplicate records:
<resultMap id="TransactionResult" type="com.example.Transaction">
<id column="txn_id" property="id"/>
<result column="order_ref" property="orderReference"/>
<result col ...
Posted on Tue, 14 Jul 2026 16:38:23 +0000 by r3dn3ck
Leveraging MyBatis Where and Trim Tags for Robust Dynamic Query Construction
When building dynamic conditional queries in MyBatis, manual WHERE clause construction can introduce syntax errors from leading AND/OR operators or a dagnling WHERE keyword when no filters apply. For example, this flawed query snippet demonstrates both issues:
<select id="fetchBlogEntry" resultType="BlogEntity">
SELE ...
Posted on Fri, 12 Jun 2026 16:51:00 +0000 by Mike521
MyBatis Interface Proxy and Advanced Features
Interface Proxy Implementation
Proxy Development Approach
MyBatis enables DAO layer creation through interface proxies. The framework dynamical generates proxy objects implementing mapper interfaces, eliminating manual DAO implementations.
Development Requirements:
XML mapper namespace must match the interface's fully qualified name
Method nam ...
Posted on Wed, 10 Jun 2026 17:15:56 +0000 by faraco