Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is spring-data-jdbc complimenting MyBatis?

I would like to understand why is spring-data-jdbc providing integration to MyBatis.

  • What problem is it solving?
  • How is spring-data-jdbc complimenting MyBatis.
  • Is it just a question of conforming the Mapper to Repository or it goes beyond that.
  • Why would I use in combination MyBatis with spring-data-jdbc when I can just use the Mappers.
like image 911
Alexander Petrov Avatar asked May 23 '19 10:05

Alexander Petrov


People also ask

Does MyBatis use JDBC?

MyBatis does four main things: It executes SQL safely and abstracts away all the intricacies of JDBC. It maps parameter objects to JDBC prepared statement parameters. It maps rows in JDBC result sets to objects.

Is MyBatis better than JPA?

Mybatis is a data mapper framework which is completely different framework compared to JPA. In JPA and ORM frameworks, you map Objects /Entities to the corresponding sql tables and you work on objects and not on tables directly unless you use their native queries. In mybatis , you play directly with sql data..

What is MyBatis Spring?

MyBatis is one of the most commonly used open-source frameworks for implementing SQL databases access in Java applications. In this quick tutorial, we'll present how to integrate MyBatis with Spring and Spring Boot.

What is the difference between Spring JDBC and Spring data JDBC?

Spring Data JDBC has less abstractions than Spring Data JPA, but uses Spring Data concepts to make it easier to do CRUD operations than Spring JDBC. It sits closer to the database because it does not contain the most part of the Spring Data magic when querying the database.


Video Answer


1 Answers

In a nutshell spring-data-jdbc provides a Domain Driven Design Repository implementation for the store that provides a JDBC API. It tries to be very simple conceptually (especially if you compare it with JPA). And is similar to mybatis in the sense that it does not try to introduce abstractions that hide the complexity of the ORM.

Here's the quote from spring-data-jdbc documentation:

  • If you load an entity, SQL statements get executed. Once this is done, you have a completely loaded entity. No lazy loading or caching is done.
  • If you save an entity, it gets saved. If you do not, it does not. There is no dirty tracking and no session.
  • There is a simple model of how to map entities to tables. It probably only works for rather simple cases. If you do not like that, you should code your own strategy. Spring Data JDBC offers only very limited support for customizing the strategy with annotations.

spring-data-jdbc can be used without mybatis. The queries are either CRUD queries implemented by spring-data-jdbc itself or are custom queries specified using @Query annotation.

It does provide integration to mybatis and this allows to use the third way to specify queries namely using mybatis mapper with all features available in mybatis. This allows to create more complex mappings while still using automatic queries generation based on repository method name for simple queries.

Sometimes the necessity to create SQL queries even for simple CRUD operations is seen as a limitation or issue in mybatis. spring-data-jdbc allows to solve this by the price of introducing an additional abstraction layer (repositories) to the application. I say additional because it is possible to use mybatis mapper as a DDD repository.

An indeed if the application has a lot of CRUD operations the will be a lot of very similar code or some solutions to make generic CRUD similar to https://github.com/rickcr/mybatis-generic-crud will be introduced and used.

spring-data-jdbc allows to solve this problem rather elegantly with rather low price.

like image 168
Roman Konoval Avatar answered Oct 11 '22 16:10

Roman Konoval