
Discover Java persistence with MyBatis, mapping interfaces to SQL for select, insert, update, and delete without writing JDBC code, using Eclipse for small to medium apps.
Learn how to start the H2 database in server mode, run it as a Java application, connect via the H2 console at localhost:8082, and manage a customers table with scripts.
Create a Lombok-powered entity class that maps table columns to fields, auto-generating getters, setters, constructors, hash code, equals, and toString, with Eclipse setup for Lombok integration.
Create a final dao factory with a private constructor that exposes a static getCustomerDao method, which loads the mybatis config, builds a sql session factory, and returns a CustomerDao mapper.
Shows adding a new customer via the deal factory and customer deal interface, setting name, city, image, and phone, then committing or rolling back transactions in a spring mybatis context.
Update customer data by ID, change city to Dallas and the phone number, commit the transaction automatically, and verify the update by retrieving and printing before and after records.
Learn to implement soft delete with an active or discontinued flag in Java persistence using MyBatis, and perform safe hard deletes only after verifying existence.
MyBatis is a Java persistence framework that couples objects with stored procedures or SQL statements using an XML descriptor or annotations. MyBatis is free software that is distributed under the Apache License 2.0. MyBatis is a fork of iBATIS 3.0 and is maintained by a team that includes the original creators of iBATIS. Unlike ORM frameworks, MyBatis does not map Java objects to database tables but Java methods to SQL statements.
MyBatis lets you use all your database functionality like stored procedures, views, queries of any complexity and vendor proprietary features. It is often a good choice for legacy or de-normalized databases or to obtain full control of SQL execution. It simplifies coding compared to JDBC. SQL statements are executed with a single line. MyBatis provides a mapping engine that maps SQL results to object trees in a declarative way.
SQL statements can be built dynamically by using a built-in language with XML-like syntax or with Apache Velocity using the Velocity integration plugin. MyBatis integrates with Spring Framework and Google Guice. This feature allows one to build business code free of dependencies. MyBatis supports declarative data caching. A statement can be marked as cacheable so any data retrieved from the database will be stored in a cache and future executions of that statement will retrieve the cached data instead hitting the database. MyBatis provides a default cache implementation based on a Java HashMap and default connectors for integrating with: OSCache, Ehcache, Hazelcast and Memcached. It provides an API to plug other cache implementations.