
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.
Map interface methods to SQL statements with annotations in the MyBatis persistence framework. Create runtime mapper implementations via a factory, use JDBC for database access, and enable declarative caching.
Create a new Eclipse Maven project and add MyBatis, H2, and Lombok dependencies. Configure the Maven compiler plugin to set source and target to 1.8, then update the project.
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.
Configure the MyBatis API to connect to the underlying database by creating a boilerplate config under src/main/resources with a data source, and define a mapper interface for the customer deal.
Build a customer dao mapper interface in MyBatis, mapping CRUD methods with @Insert, @Select, @Update, @Delete and SQL to a Customer object, including auto-generated keys and city queries.
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.
Learn to retrieve a customer by id with Java persistence using MyBatis, handling null results, and printing either the customer details or a no data message.
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.
Learn how to get all customers and get customers by city in java persistence using mybatis, returning a list and printing each customer's name and city.
Learn to fetch customers by city using MyBatis, declare a city variable, retrieve a list of customers, and handle empty results without extra JDBC code.
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.