Session-Level Cache Every SqlSession holds an internal cache that is active by default. This local storage avoids redundant database calls when identical queries run inside the same session. Internal Mechanics The cache is backed by a Map scoped to the session. When a query arrives, MyBatis checks t...
MyBatis Parameter Passing Techniques 1. Single Primitive Data Type 2. Multiple Primitive Data Types 3. Single Reference Data Type 4. Map Collection Data Type 5. Multiple Reference Data Types Interface package com.msb.mapper; import com.msb.pojo.Employee; import org.apache.ibatis.annotations.Param; i...
Issue Origin Recently, I took over a project that uses MyBatis and MyBatis-Plus for database operations. We needed to add new functionality to this system. During development, everything went smoothly, and I quickly completed my part of the module. Then, I started integrating with the front end. How...
This article covers fundamental database operations using MyBatis, including delete, insert, update, and select. It also discusses parameter placeholders, primary key retrieval, and XML mapping. Delete by Primary Key @Delete("delete from emp where id = #{id}") public void delete(Integer id...
Understanding ORM and MyBatis Object-Relational Mapping (ORM) bridges the gap between object-oriented programming and relational databases by linking persistent data to entity objects. A schematic illustrates the correspondence between database tables and class fields, while another diagram depicts...
<if></if> <where></where> (along with trim and set) <choose></choose> (with when and otherwise) <foreach></foreach> Important Note: When working with XML mappers, special characters such as greater than (>), less than (<), greater than or equal...
To enhance maintainability, database connection properties should be decoupled from the core MyBatis configuration file. Instead of hardcoding values, we can utilize a dedicated properties file. First, create a file named database.properties in your resources directory: ``` jdbc.driver=com.mysql.cj...
Introduction to MyBatis Annotation Development MyBatis annotation-based development has become the preferred approach in modern Java projects, particularly those built with Spring Boot. This method allows developers to bypass the cumbersome XML configuration files and write data access layers in a m...
Database Schema Setup Create the user table with the following SQL definition: CREATE TABLE `tb_user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary Key', `username` varchar(50) NOT NULL COMMENT 'User Name', `age` int(11) NOT NULL COMMENT 'User Age', `ctm` datetime NOT NULL COMMENT 'Creatio...
MyBatis is a lightweight persistence framework that supports custom SQL, stored procedures, and advanced mappings. It eliminates most of the boilerplate JDBC code, such as parameter setting and result set handling. With minimal XML configuration or annotations, MyBatis maps plain Java objects (POJOs...