Java DAO模式 – 在不使用多个类的情况下分离数据库通信责任

huangapple go评论29阅读模式
英文:

Java DAO Pattern - Separate Database Communication Responsibility Without Using Multiple Classes

问题

以下是您要翻译的内容:

"希望利用社区的知识帮助我回答关于Java编程中的DAO模式的问题。

在谷歌上搜索实现DAO模式示例的结果如下:DAO模式示例

public class DaoPatternDemo {
   public static void main(String[] args) {
      StudentDao studentDao = new StudentDaoImpl();

      //打印所有学生
      for (Student student : studentDao.getAllStudents()) {
         System.out.println("学生: [学号 : " + student.getRollNo() + ", 姓名 : " + student.getName() + " ]");
      }

      //更新学生
      Student student = studentDao.getAllStudents().get(0);
      student.setName("Michael");
      studentDao.updateStudent(student);

      //获取学生
      studentDao.getStudent(0);
      System.out.println("学生: [学号 : " + student.getRollNo() + ", 姓名 : " + student.getName() + " ]");
   }
}

我的问题是:是否有一种清晰的方法,只使用Student模型/类来代表学生与数据库通信,而不是实现一个"StudentDao"类呢?我更愿意像这样做:

student.setName("Michael");

并且在调用setName()方法时,让Student类处理所有与数据库的通信,而不必像这样做:

student.setName("Michael");
studentDao.updateStudent(student);

似乎如果遵循DAO模式,你必须做两次一样的事情:更新Student对象和数据库中的Student表。只需让Student类的方法负责更新数据库会不会更容易?这样设计的缺点是什么呢?

(示例代码和图像来自:https://www.tutorialspoint.com/design_pattern/data_access_object_pattern.htm)

英文:

Hoping to leverage the knowledge of the community to help me answer a question on the DAO pattern in Java Programming.

Searching google for examples of implementing the DAO pattern give results like this:Example DAO Pattern

  public class DaoPatternDemo {
   public static void main(String[] args) {
      StudentDao studentDao = new StudentDaoImpl();

      //print all students
      for (Student student : studentDao.getAllStudents()) {
         System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");
      }


      //update student
      Student student =studentDao.getAllStudents().get(0);
      student.setName("Michael");
      studentDao.updateStudent(student);

      //get the student
      studentDao.getStudent(0);
      System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");		
   }
}

My question is this: Is there a clean way to utilize just the Student model/class to communicate with the database on behalf of students rather than implement a "StudentDao" class as well? I would much rather do something like this:

student.setName("Michael");

and have the Student class handle all the database communication to set the name of the student upon calling the setName() method rather than have to do this:

student.setName("Michael");
studentDao.updateStudent(student);

It seems like if you follow the DAO pattern you have to do everything twice: Update the Student Object and the Student Table in the Database. Wouldn't it be easier to just have the methods in the Student Class take care of updating the database as well? What would be the drawbacks of a design like that?

(Example code and image taken from: https://www.tutorialspoint.com/design_pattern/data_access_object_pattern.htm)

答案1

得分: 1

你正在寻找的是一种称为Active Record(https://en.m.wikipedia.org/wiki/Active_record_pattern)的东西,其中模型本身负责管理其持久性。我没有在Java中使用过它,但我记得这个库。看一看,也许这就是你要找的东西。
https://javalite.io/activejdbc

缺点是你将你的领域模型直接绑定到持久性层,你可能会使你的模型变成纯粹的数据容器,只有getter和setter。对于简单的CRUD应用程序,我认为这种模式还可以,但我不会用它来建模复杂的领域。

英文:

What you are looking for is something called Active Record (https://en.m.wikipedia.org/wiki/Active_record_pattern), where the model itself is responsible for managing its persistence. i haven‘t worked with it in java but i remember this library. have a look, maybe thats what you‘re looking for.
https://javalite.io/activejdbc

drawback is that you‘re binding your domain model directly to your persistence layer and you risk that your models become purely data containers with only getters and setters. for simple crud applications i think this pattern is ok, but i wouldn’t use it for modeling complex domains.

huangapple
  • 本文由 发表于 2020年7月23日 07:31:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63044612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定