I’ve done some research on my own and understand the basic concept. But some insights can only be gained through actual experience.
What are the advantages of myBatis that would make it worth learning a new framework?
In what case would you avoid using it?
1
Consider what you’re trying to achieve. Typically, the Command Query Response Segregation
model works well for complex domains.
The reason is that you’re trying to do one of two things typically:
- Create/Update/Delete some complex domain entities
- Run analytic fetch queries (i.e. summation/aggregation queries)
Hibernate works well for case 1 allowing you to just make a POJO and persist/update it. It also does this quickly, unless your domain is quite large.
myBatis is great for fetch queries (case 2) where you just want an answer. Hibernate would attempt to load the entire object graph and you’d need to start tuning queries with LazyLoading tricks to keep it working on a large domain. This is important when running complex analytic queries that don’t even return entity objects. Hibernate only offers SqlQuery and bean Transformers in this case with huge default types like BigDecimal, while myBatis could easily map to a simple POJO non-entity.
These two cases are the difference between Commands where you want to change the domain data and Responses where you just want to fetch some data.
So, consider these two cases and what your application does. If you have a simple domain and just fetch information, use myBatis. If you have a complex domain and persist entities, use Hibernate. If you do both, consider a hybrid approach. That’s what we use on our project that has thousands of entities to keep it under control. 😉
7
MyBatis is SQL centric. It heps you calling SQL statements and mapping results (tables) to object trees.
The main benefit is that it is not an ORM. It does not map tables to object so does not suffer the orm impedance mismatch. Fits well for complex or legacy databases or to use db features like stored procedures, views and so.
It is quite simple and easy to learn so fits also well for low skilled teams because there is no need to have an hibernate guru among them.
Have a look at jpetstore 6 http://mybatis.org/spring/sample.html
2
Since the question refers to my comment, here’s what I had in mind writing it.
First of all, it is derived from the context of your original question. In other circumstances I could give a different advice. The point that made me suggest MyBatis is this:
…we encountered some performance problems.
We decided to drop hibernate in favor of plain Jdbc to gain database performance…
In one of past projects, our team has been considering moving from Hibernate for the reasons like you describe. Similar to you, we were going to switch to JDBC, but colleagues from another project recommended us MyBatis. Team decided to give it a try, while keeping JDBC as a fallback option in the case if things go wrong.
At that moment, I knew nothing about MyBatis but had enough experience with JDBC to be sure it will do the job. Despite this, I had been strongly supporting idea of trying MyBatis, main reason being that per my past experience, amount of boilerplate code we would have to write with JDBC would be just daunting.
- To be fair, I like JDBC for it being simple to understand, reliable and for giving good feel of control over database interaction, but the price one pays for it is really high. My fingers start aching every time I recall how much boilerplate I had to type with JDBC.
Anyway, we tried MyBatis and it worked as advertised. That’s why I wrote the comment you ask about.
In case if you expect me to give a detailed overview of the technology, or somehow praise its superiority – sorry I can’t do that. If I could – I’d already write that in a separate answer to your original question, instead of giving short comment. I mentioned I knew nothing about MyBatis back then – well I still have quite little knowledge of it sorry. Transition from Hibernate was done by other team members and it did not impact the code I’ve been working on. I only recalled key takeaways (based on which I made my comment), namely that 1) MyBatis resolved the issues we had with Hibernate, 2) it did not introduce issues of its own and 3) it allowed us to avoid writing boilerplate code I was expecting in case if we switch to JDBC. That’s all.
0
Hibernate is well know for too much magic, unexpected behaviour and big learning curve. There are other frameworks out there which are more focused on simplicity and will let you be in control.
myBatis is one of them, my project MentaBean is another one. I’ve written a blog post about it that might help.
2
I used Hibernate for a data loading and data transformation project 5 years ago using Hibernate 3 and I thought it was wonderful. I am doing a small e-commerce application and I tried using Hibernate 4 and I was extremely disappointed. They have removed the tools and tightly integrated to the IDE. I tried MyBatis and I go everything working in a single night and I am very please with how easy it is to integrate into an application. I think Hibernate has become too bloated and I would use EJB 3 over Hibernate at this point.
1