MyBatis (created with the name iBatis) is a very good framework that is used by a lot of developers. Has a lot of functionalities, but we will only see a few in this post.
The code of this page can be found in here: https://github.com/uaihebert/MyBatisCrud
To run your project with MyBatis you will need to instantiate a Session Factory. It is very easy and the documentation says that this factory can be static:
When you run a project with MyBatis you just need to instantiate the Factory one time, that is why it is in a static code. The configuration XML (mybatis.xml) it is very simple and its code can be found below:
The Mapper (an attribute inside the XML above) will hold information about the project queries and how to translate the database result into Java objects. It is possible to create a Mapper in XML or Interface. Let us see below the Mapper found in the file crud_query.xml:
Notice that the file is easy to understand. The first configuration found is a ResultMap that indicates the query result type, and a result class was configured “uai.model.Customer”. In the class we have a attribute with a different name of the database table column, so we need to add a configuration to the ResultMap.
All queries need a ID that will be used by MyBatis session. In the beginning of the file it is possible to see a namespace declared that works as a Java package, this package will wrap all the queries and the ResultMaps found in the XML file.
We could also use a Interface+Annotation instead of the XML. The Mapper found in the crud_query.xml file could be translated in to a Interface like:
Only the Read methods were written in the Interface to make the code smaller, but all the CRUD methods could be written in the Interface.
Let us see first how to execute a query found in the XML file:
The parsing of the object is automatically and the method is easy to read. To run the query all that is needed is to use the combination “namespace + query id” that we saw in the crud_query.xml code above.
If the developer wants to use the Interface approach he could do like below:
With the interface query mode we have a clean code and the developer will not need to instantiate the Interface, the session class of the MyBatis will do the work.
If you want to update, delete or insert a record in the database the code is very easy:
About MyBatis we could say:
Excellent Documentation: Every time that I had a doubt I could answer it just by reading its site documentation
Flexibility: Allowing XML or Interfaces+Annotations the framework gives a huge flexibility to the developer. Notice that if you choose the Interface approach the database portability will be harder, it is easier to choose which XML to send with the deploy artifact rather than an interface
Integration: Has integration with Guice and Spring
Dynamic Query: Allows to create queries in Runtime, like the JPA criteria. It is possible to add “IFs” to a query to decide which attribute will be used in the query
Transaction: If your project is not using Guice of Spring you will need to manually control the transaction