spring-data-jdbc 是如何赞美 MyBatis 的?

How is spring-data-jdbc complimenting MyBatis?

我想了解为什么 spring-data-jdbc 提供与 MyBatis 的集成。

简而言之,spring-data-jdbc 为提供 JDBC API 的商店提供领域驱动设计 Repository 实施。它试图在概念上非常简单(尤其是当您将它与 JPA 进行比较时)。并且在某种意义上类似于mybatis,它不会尝试引入隐藏ORM复杂性的抽象。

引用自 spring-data-jdbc 文档:

  • 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可以不用mybatis。这些查询要么是由 spring-data-jdbc 本身实现的 CRUD 查询,要么是使用 @Query 注释指定的自定义查询。

它确实提供了与 mybatis 的集成,这允许使用第三种方式来指定查询,即使用 mybatis 映射器和 mybatis 中可用的所有功能。这允许创建更复杂的映射,同时仍然使用基于存储库方法名称的自动查询生成来进行简单查询。

有时即使是简单的 CRUD 操作也需要创建 SQL 查询,这在 mybatis 中被视为限制或问题。 spring-data-jdbc 允许通过向应用程序引入额外的抽象层(存储库)的代价来解决这个问题。我说 additional 因为可以将 mybatis 映射器用作 DDD 存储库。

事实上,如果应用程序有很多 CRUD 操作,将会引入和使用很多非常相似的代码或一些解决方案来制作类似于 https://github.com/rickcr/mybatis-generic-crud 的通用 CRUD。

spring-data-jdbc 可以用相当低的价格相当优雅地解决这个问题。