DAO 和 JDBC 的关系?
我知道Hibernate实现了ORM(对象关系映射),那么JDBC实现了什么类型的映射?它实现了 DAO 吗?我不完全理解 DAO 如何/是否与 JDBC 相关......?
I know that Hibernate implements ORM (Object Relational Mapping), what type of mapping does JDBC implement? Does it implement DAO? I don't totally understand how/if DAO is related to JDBC...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DAO 不是映射。 DAO 代表数据访问对象。它看起来像这样:
对于 DAO,JDBC 只是一个实现细节。
Hibernate 可能是另一种。
JPA可能是另一种(如果您要将现有的遗留应用程序迁移到 JPA;对于新应用程序,这会有点奇怪,因为 JPA 本身实际上是 DAO,例如 Hibernate 和 EclipseLink作为可用的实现)。
它允许您切换
UserDAO
实现,而无需更改使用 DAO 的业务代码(当然,前提是您针对接口正确编码)。对于 JDBC,您只需要编写很多行来查找/保存/删除所需的信息,而使用 Hibernate 只需要几行。 Hiberenate 作为一个 ORM,完全从您手中接走了令人讨厌的 JDBC 工作,无论您是否使用 DAO。
另请参阅:
DAO isn't a mapping. DAO stands for Data Access Object. It look something like this:
For DAO, JDBC is just an implementation detail.
Hibernate could be another one.
JPA could be another one (in case you're migrating an existing legacy app to JPA; for new apps, it would be a bit weird as JPA is by itself actually the DAO, with e.g. Hibernate and EclipseLink as available implementations).
It allows you for switching of
UserDAO
implementation without changing the business code which is using the DAO (only if you're properly coding against the interface, of course).For JDBC you'll only need to write a lot of lines to find/save/delete the desired information while with Hibernate it's a matter of only a few lines. Hiberenate as being an ORM takes exactly that nasty JDBC work from your hands, regardless of whether you're using a DAO or not.
See also:
DAO 是访问数据的抽象,其思想是将数据访问的技术细节与应用程序的其余部分分开。它可以适用于任何类型的数据。
JDBC 是使用 Java 访问关系数据库的 API。
JDBC 比 ORM 更底层,它将一些 Java 类型映射到 SQL 类型,但仅此而已,它只接受 DDL 和 DML,执行它并返回结果集。这取决于你的程序来理解它。
DAO is an abstraction for accessing data, the idea is to separate the technical details of data access from the rest of the application. It can apply to any kind of data.
JDBC is an API for accessing relational databases using Java.
JDBC is more low-level than an ORM, it maps some Java types to SQL types but no more than that, it just takes DDL and DML, executes it, and returns result sets. It's up to your program to make sense of it.