Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What benefits MyBatis provides over working with plain JDBC

I need to write pretty straight forward DB code and I'm considering MyBatis over plain JDBC (I believe full ORM is an overkill).

Considering that in both MyBatis and plain JDBC you find yourself:

  1. Hand writing SQL statements.
  2. Manually wiring DB rows to JAVA DTO objects (either via code or config).

The MyBatis benefits over JDBC I know of are:

  1. Out-of-the-box table/query caching.
  2. Dynamic SQL.
  3. SQL is stored outside of the code.
  4. Templating SQL for easier DB vendor Independence.

What other MyBatis-Over-JDBC benefits are there to consider?

like image 274
Gili Nachum Avatar asked Apr 24 '12 06:04

Gili Nachum


People also ask

Is MyBatis faster than JDBC?

For an easiest query. Not considering the first query, for the following queries, JDBC always takes about 6ms~10ms, and MyBatis takes about 31~35ms, which is about 3 times. For a more complex query (6 inner joins and an order by in it), JDBC only takes 25~30ms, while MyBatis needs 80~100ms.

Does MyBatis use JDBC?

Background information. MyBatis is a persistence framework for Java that supports custom SQL statements, stored procedures, and advanced mappings. MyBatis eliminates the need to use JDBC code, manually configure parameters, and retrieve result sets.

What is MyBatis used for?

MyBatis is an open source persistence framework which simplifies the implementation of database access in Java applications. It provides the support for custom SQL, stored procedures and different types of mapping relations. Simply put, it's an alternative to JDBC and Hibernate.

What is the difference between hibernate and MyBatis?

Hibernate is an object-relational mapping framework (ORM) which maps Java classes to database tables. MyBatis is a persistence framework – not ORM. It maps SQL statements to Java methods.


2 Answers

I dont know you'll count this one as advantage or not but there's MyBatisGenerator, And It generates all basic needed Queries plus some Advanced Queries too and DTO objects automatically based on a single XML file.

Plus it has Eclipse Plugin For the same.

like image 54
Anuj Patel Avatar answered Oct 18 '22 03:10

Anuj Patel


Most of the times you do not need to map explicity columns to pojos so bullet number 2 is a difference rather than a similarity.

The main difference IMHO is the API that is much simpler in MyBatis than in JDBC. If used with Spring or Guice you will not need to call MyBatis API in your code at all.

Mappers and injection helps the testing a lot because mappers are plain interfaces so easy to mock.

like image 33
Diego Lopez Avatar answered Oct 18 '22 03:10

Diego Lopez