Fundamentals of MyBATIS What is MyBATIS?
Introduction Why use MyBATIS? When not to use MyBATIS Mapping SQL How it works
Overview MyBatis is an open source persistence framework that simplifies the implementation of the persistence layer by abstracting a lot of JDBC boilerplate code and provides a simple and easy-to-use API to interact with the database. MyBatis was formerly known as iBATIS and was started by Clinton Begin in 2002. MyBatis 3 is a complete redesign of iBATIS, with annotations and Mapper support.
Overview The main reason for the popularity of MyBatis is its simplicity and ease of use. In Java applications, the persistence layer involves populating Java objects with data loaded from the database using SQL queries, and persisting the data in Java objects into the database using SQL. MyBatis makes using SQL easy by abstracting low-level JDBC code, automating the process of populating the SQL result set into Java objects, and persisting data into tables by extracting the data from Java objects.
Why use MyBATIS? There are many Java-based persistence frameworks, however MyBatis became popular because of the following reasons: It Eliminates a lot of JDBC boilerplate code It has a low learning curve It works well with legacy databases It embraces SQL It provides support for integration with Spring and Guice frameworks It provides support for integration with third-party cache libraries It induces better performance
When not to use MyBATIS If you design your application starting from an object model, the database platform is a moving target, you have a complex object model with lots of different queries to perform, Hibernate is your choice. On the contrary, if you think relational and the application is a by-product of the database design, then MyBatis should be better.
Mapping SQL The true power of MyBatis is in the Mapped Statements. This is where the magic happens. For all of their power, the Mapper XML files are relatively simple. Certainly if you were to compare them to the equivalent JDBC code, you would immediately see a savings of 95% of the code. MyBatis was built to focus on the SQL, and does its best to stay out of your way.
How It Works In MyBatis 3, we have two types for configuration files: Environment Specification Configuration File: There will be only one file that will define the environmental settings that are to be used by MyBatis mapping framework. This file defines the settings related to database connection, driver, transaction management, connection pooling, other mapping files and a lot more that we will see in upcoming tutorials. Mapping Files: There can be more than one mapping file in one application for MyBatis framework. A mapping file carries information about set of related service that is defined in an Java interface. It defines parametrized SQL Statement to be used for a particular service, inputs to to the SQL parameters and also defines return mapping for the resultant data if returned back by SQL statement.