实现 DAO 类的正确方法?

发布于 2024-12-26 02:36:57 字数 1319 浏览 1 评论 0原文

我有许多与数据库中的表相对应的普通实体类,其结构类似于:

package project.src.entities;
public class ClassName{
/** variables */
private type var1;
private type var2;
/** ...variable ends */
/** Default constructor */
public ClassName(){
}
/** Custom constructor */
public ClassName(type var1, type var2 /*, ... */){
this.var1 = var1;
..
}
/** follows getters and setters of all fields */
}


现在我创建了一个新包:

package project.src.dao;

这个包将包含所有通过 JDBC 与数据库交互的方法。 例如:

public class ClassNameDAO {
  /** @return an object of ClassName of given id */
  public static ClassName getClassName(String id){
  . . .
  return className;
  }
  /** @return an ArrayList<ClassName> of objects of ClassName */
  public static ArrayList<ClassName> getAllClassName(){
  . . .
  return classNameList;
  }
  /* Similarly, methods add(ClassName className), update(id, newClassName) and delete(id) follows */
}

现在我的问题是:

  1. 这是在 Swing 应用程序中实现的正确方法吗?
  2. 建议一种更好的方式来实现 DAO,我们衷心欢迎示例?
  3. 我可以在 DAO 中拥有所有static 方法吗?有什么问题吗?如果是,那为什么呢?
  4. 您还有什么想额外指出的吗?
    谢谢。


编辑: 找到了我需要的这里 ....谢谢@BalusC

I have a number of plain entity classes corresponding to the tables in database, there structure is similar as:

package project.src.entities;
public class ClassName{
/** variables */
private type var1;
private type var2;
/** ...variable ends */
/** Default constructor */
public ClassName(){
}
/** Custom constructor */
public ClassName(type var1, type var2 /*, ... */){
this.var1 = var1;
..
}
/** follows getters and setters of all fields */
}

Now I created a new package:

package project.src.dao;

This package will contain all the methods that interacts with the database through JDBC.
such as:

public class ClassNameDAO {
  /** @return an object of ClassName of given id */
  public static ClassName getClassName(String id){
  . . .
  return className;
  }
  /** @return an ArrayList<ClassName> of objects of ClassName */
  public static ArrayList<ClassName> getAllClassName(){
  . . .
  return classNameList;
  }
  /* Similarly, methods add(ClassName className), update(id, newClassName) and delete(id) follows */
}

Now My questions are:

  1. Is this the right way of implementation in a swing application?
  2. Suggest A nicer way to implement DAOs, examples are heartly welcomed?
  3. Can i have all static methods in DAO? Is there any problem? If Yes, then why so?
  4. Anything in extra you want to point out?

    Thanks.

EDIT :
Found Exactly What I need Here .... Thanks @BalusC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

红墙和绿瓦 2025-01-02 02:36:57

这基本上是正确的,只是我不会在 DAO 中使用静态方法。我还会有一个 DAO 接口,以及一个实现该接口的 DAO 类。

不使用静态方法的一个重要原因是为了可测试性。在某些时候,您需要对调用 DAO 方法的类进行单元测试;为此,您需要模拟 DAO 类。您无法轻松模拟具有大量静态方法的类。

将来,您可能希望为某些 DAO 方法提供不同的实现。例如,您可能需要一些特定于特定数据库实现的 SQL(例如 Oracle 提示),或者您可能希望将数据写入文件系统或 Web 服务。如果您使用一个 DAO 接口和许多实现它的 DAO 类,那么在一个应用程序中使用多个实现会更容易。

This is basically right, except that I wouldn't use static methods in the DAO. I would also have an interface for the DAO, and a DAO class that implements the interface.

One important reason not to use static methods is for testability. At some point, you'll want to unit test the classes that call the DAO methods; and for this, you'll want to mock the DAO classes. You can't easily mock classes that have a lot of static methods.

In the future, you may want to supply different implementations for some of your DAO methods. For example, you may want to have some SQL that's specific to a particular database implementation (such as an Oracle Hint), or you may want to write data to the file system, or to a web service. If you use one DAO interface and many DAO classes that implement it, then it's easier to use the multiple implementations within the one application.

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