How is spring-data-jdbc complimenting MyBatis?
Asked Answered
C

1

7

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.
Capital answered 23/5, 2019 at 10:46 Comment(0)
L
10

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.

Langobard answered 23/5, 2019 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.