DAO 和 JDBC 的关系?

发布于 2024-11-29 17:05:14 字数 94 浏览 1 评论 0原文

我知道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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

§普罗旺斯的薰衣草 2024-12-06 17:05:14

DAO 不是映射。 DAO 代表数据访问对象。它看起来像这样:

public interface UserDAO {

    public User find(Long id) throws DAOException;

    public void save(User user) throws DAOException;

    public void delete(User user) throws DAOException;

    // ...
}

对于 DAO,JDBC 只是一个实现细节。

public class UserDAOJDBC implements UserDAO {

    public User find(Long id) throws DAOException {
        // Write JDBC code here to return User by id.
    }

    // ...
}

Hibernate 可能是另一种。

public class UserDAOHibernate implements UserDAO {

    public User find(Long id) throws DAOException {
        // Write Hibernate code here to return User by id.
    }

    // ...
}

JPA可能是另一种(如果您要将现有的遗留应用程序迁移到 JPA;对于新应用程序,这会有点奇怪,因为 JPA 本身实际上是 DAO,例如 Hibernate 和 EclipseLink作为可用的实现)。

public class UserDAOJPA implements UserDAO {

    public User find(Long id) throws DAOException {
        // Write JPA code here to return User by id.
    }

    // ...
}

它允许您切换 UserDAO 实现,而无需更改使用 DAO 的业务代码(当然,前提是您针对接口正确编码)。

对于 JDBC,您只需要编写很多行来查找/保存/删除所需的信息,而使用 Hibernate 只需要几行。 Hiberenate 作为一个 ORM,完全从您手中接走了令人讨厌的 JDBC 工作,无论您是否使用 DAO。

另请参阅:

DAO isn't a mapping. DAO stands for Data Access Object. It look something like this:

public interface UserDAO {

    public User find(Long id) throws DAOException;

    public void save(User user) throws DAOException;

    public void delete(User user) throws DAOException;

    // ...
}

For DAO, JDBC is just an implementation detail.

public class UserDAOJDBC implements UserDAO {

    public User find(Long id) throws DAOException {
        // Write JDBC code here to return User by id.
    }

    // ...
}

Hibernate could be another one.

public class UserDAOHibernate implements UserDAO {

    public User find(Long id) throws DAOException {
        // Write Hibernate code here to return User by id.
    }

    // ...
}

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).

public class UserDAOJPA implements UserDAO {

    public User find(Long id) throws DAOException {
        // Write JPA code here to return User by id.
    }

    // ...
}

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:

入画浅相思 2024-12-06 17:05:14

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文