简体   繁体   English

Spring数据JPA与JDBC的关系

[英]Relationship between Spring Data JPA and JDBC

I understand that SimpleJpaRepository, the basic implementation of Spring Data JPA, uses JPA (provided by Java) internally.我理解SimpleJpaRepository,Spring数据JPA的基本实现,内部使用JPA(Java提供)。

So does JPA use plain JDBC (provided by Java) internally?那么 JPA 是否在内部使用纯 JDBC(由 Java 提供)? Or does JPA use Spring JDBC internally?还是 JPA 在内部使用 Spring JDBC?

Also, does Spring JDBC (JdbcTemplate) use plain JDBC internally?另外, Spring JDBC (JdbcTemplate) 在内部使用纯 JDBC 吗? Is it just that we made plain JDBC easy to use?仅仅是因为我们让普通的 JDBC 易于使用吗?

I expect that the JPA used inside the SimpleJpaRepository will use plain JDBC. Because Jpa itself is provided by Java. And I expect Spring JDBC (JdbcTemplate) use plain JDBC.我希望 SimpleJpaRepository 中使用的 JPA 将使用普通 JDBC。因为 Jpa 本身由 Java 提供。我希望 Spring JDBC (JdbcTemplate) 使用普通 881814

enter image description here在此处输入图像描述

The answer depends a little on the type of database you connect to.答案在一定程度上取决于您连接的数据库类型。 I am assuming you are interested in relational databases such as MySQL, Oracle, etc.我假设您对关系数据库感兴趣,例如 MySQL、Oracle 等。

First some concepts首先是一些概念

JDBC is the low level standard for connecting to a DB directly and running SQL against it, and each database provider (MySQL, Oracle, etc) implements the specifics in order to make that connection possible. JDBC是直接连接到数据库并针对它运行 SQL 的低级标准,每个数据库提供程序(MySQL、Oracle 等)都实现了细节以使该连接成为可能。

The issue with it is that you may end up with a lot of code trying to map ResulSets, parameters, and so on depending on your needs.它的问题是您最终可能会编写大量代码来尝试 map ResulSets、参数等,具体取决于您的需要。

JPA, JDBC Template (or any other ORM for that matter) are higher level standards that helps to "hide" some of the code you would have to write using JDBC alone, allowing the developers to focus on more important things. JPA、JDBC 模板(或与此相关的任何其他 ORM)是更高级别的标准,有助于“隐藏”您必须单独使用 JDBC 编写的一些代码,从而使开发人员可以专注于更重要的事情。 So, in a (little) naïve answer you could say JPA will use JDBC under the hood in order to connect to a DB.因此,在一个(小)天真的答案中,您可以说 JPA 将在后台使用 JDBC 以连接到数据库。

However, this is not 100% of the time, as there are data stores like MongoDB, Neo4J, and others that do not follow JDBC standard for connection, but you may find ORM implementations for them, which will most likely not use JDBC.然而,这不是 100% 的时间,因为有像 MongoDB、Neo4J 和其他不遵循 JDBC 连接标准的数据存储,但你可能会找到它们的 ORM 实现,它们很可能不会使用 JDBC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM