Java/SQL实验DAO实现

cu6pst1q  于 2023-06-28  发布在  Java
关注(0)|答案(1)|浏览(157)

我正在一个Java(Web)应用程序中进行数据库的DAO实现。只是我遇到了点小问题。
当前代码:
Account.java:

  1. package beans;
  2. public class Account {
  3. private int accountId;
  4. private String name;
  5. private String password;
  6. private String email;
  7. public Account() {
  8. }
  9. public Account(final String name, final String password, final String email) {
  10. this.name = name;
  11. this.password = password;
  12. this.email = email;
  13. }
  14. public int getAccountId() {
  15. return accountId;
  16. }
  17. public void setAccountId(int accountId) {
  18. this.accountId = accountId;
  19. }
  20. public String getName() {
  21. return name;
  22. }
  23. public void setName(String name) {
  24. this.name = name;
  25. }
  26. public String getPassword() {
  27. return password;
  28. }
  29. public void setPassword(String password) {
  30. this.password = password;
  31. }
  32. public String getEmail() {
  33. return email;
  34. }
  35. public void setEmail(String email) {
  36. this.email = email;
  37. }
  38. }

DAO.java:

  1. package dao;
  2. import java.util.Collection;
  3. public interface DAO<T> {
  4. public int insert(T t);
  5. public boolean update(T t);
  6. public T get();
  7. public Collection<T> search();
  8. public boolean delete(T t);
  9. }

AccountDAO.java:

  1. package dao;
  2. import beans.Account;
  3. import java.util.Collection;
  4. public class AccountDAO implements DAO<Account> {
  5. @Override
  6. public int insert(Account t) {
  7. throw new UnsupportedOperationException("Not supported yet.");
  8. }
  9. @Override
  10. public boolean update(Account t) {
  11. throw new UnsupportedOperationException("Not supported yet.");
  12. }
  13. @Override
  14. public Account get() {
  15. throw new UnsupportedOperationException("Not supported yet.");
  16. }
  17. @Override
  18. public Collection<Account> search() {
  19. throw new UnsupportedOperationException("Not supported yet.");
  20. }
  21. @Override
  22. public boolean delete(Account t) {
  23. throw new UnsupportedOperationException("Not supported yet.");
  24. }
  25. }

问题是,我很确定我不想在DAO接口中使用SQL字符串,但接下来我会遇到如何正确实现get()search()的问题?
由于它是一个通用接口,我还不能使用列名,我想将get()search()方法保留在接口中,以确保它们将被实现,除非真的有必要将它们从接口中删除。
我的建议是通过一个范围或一个未完全填充的T类对象进行搜索。因此,如果你想获得name=x的帐户,那么你会有这样的代码:

  1. AccountDAO accountDAO = DAOFactory.getAccountDAO();
  2. Account result = accountDAO.get(new Account(x, null, null));

但我不确定这是否是最好的解决方案,请帮助我。
问候。

50few1ms

50few1ms1#

这就是我如何处理我的:
我创建了一个接受SQL查询的抽象类NativeQueryBasedDAO。这样,我就可以拥有扩展NativeQueryBasedDAO的子类,这些子类表示每个RDBMS(如MySQL、DB2、PostGres等)。
在你的情况下,你会有这样的效果:

  1. public abstract class NativeQueryBasedDAO<T> implements DAO<T> {
  2. private Connection connection;
  3. private String schemaName;
  4. private String tableName;
  5. protected NativeQueryBasedDAO(Connection connection, String tableName) {
  6. this(connection, null, tableName);
  7. }
  8. protected NativeQueryBasedDAO(Connection connection, String schemaName, String tableName) {
  9. if (schemaName == null) {
  10. this.schemaName = "";
  11. } else {
  12. this.schemaName = schemaName;
  13. }
  14. this.connection = connection;
  15. this.tableName = tableName;
  16. }
  17. protected final String getTableName() {
  18. return tableName;
  19. }
  20. protected final String getSchemaName() {
  21. return schemaName;
  22. }
  23. protected final Connection getConnection() {
  24. return connection;
  25. }
  26. }

上面的字段可以帮助你在方法,如get(),你需要得到一个基于你的schemaName + tableName的对象。
然后,我将有一个Factory<T>,它有一个public T create(),它创建一个对象(在您的情况下,是帐户)。
因此,基于一些Parameters, you can pass/generate a工厂,将生成一个对象。 然后,您可以修改DAO以执行get(Criteria criteria),该get(Criteria criteria)构建Factory`,并使用参数填充以创建对象。
简而言之,它不是一个简单的解决方案(因为它必须是健壮的,并且可以根据您的需求进行扩展)。ORM,如Hibernate或JPA实现(例如TopLink)处理这个。
希望这能帮上忙。

展开查看全部

相关问题