Gili Nachum
Gili Nachum

Reputation: 5568

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?

Upvotes: 7

Views: 9842

Answers (3)

Nestor Hernandez Loli
Nestor Hernandez Loli

Reputation: 1442

About : 'Manually wiring DB rows to JAVA DTO objects (either via code or config).' This is not totally true, if you use conventions you can get an automatic mapping from DB tables to Java classes, example you have a CUSTOMER table that has fields like ID, COMPANY_NAME, PHONE_NUMBER, and a Java class Customer with properties id, companyName and phoneNumber, MyBatis is smart enough to figurate the DB to camel case convention and no mapping is required from you. Great!

  • MyBatis require less code and is cleaner than plain JDBC coding
  • MyBatis supports named parameters, JDBC supports only placeholders? (ugg!)
  • With a single line you can change from Reuse Prepared Statement Mode to Bath Mode, in plain JDBC it will take you a rewrite of your code.

Upvotes: 2

Diego Lopez
Diego Lopez

Reputation: 594

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.

Upvotes: 2

Anuj Patel
Anuj Patel

Reputation: 17859

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.

Upvotes: 3

Related Questions