2013-06-28 43 views
0

我正在研究Java(web)應用程序中數據庫的DAO實現。只有我遇到了一個小問題。Java/sql實驗DAO實現

我當前的代碼:

Account.java:

package beans; 

public class Account { 
    private int accountId; 
    private String name; 
    private String password; 
    private String email; 

    public Account() { 

    } 

    public Account(final String name, final String password, final String email) { 
     this.name = name; 
     this.password = password; 
     this.email = email; 
    } 

    public int getAccountId() { 
     return accountId; 
    } 

    public void setAccountId(int accountId) { 
     this.accountId = accountId; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 
} 

DAO.java:

package dao; 

import java.util.Collection; 

public interface DAO<T> { 
    public int insert(T t); 

    public boolean update(T t); 

    public T get(); 

    public Collection<T> search(); 

    public boolean delete(T t); 
} 

AccountDAO.java:

package dao; 

import beans.Account; 
import java.util.Collection; 

public class AccountDAO implements DAO<Account> { 
    @Override 
    public int insert(Account t) { 
     throw new UnsupportedOperationException("Not supported yet."); 
    } 

    @Override 
    public boolean update(Account t) { 
     throw new UnsupportedOperationException("Not supported yet."); 
    } 

    @Override 
    public Account get() { 
     throw new UnsupportedOperationException("Not supported yet."); 
    } 

    @Override 
    public Collection<Account> search() { 
     throw new UnsupportedOperationException("Not supported yet."); 
    } 

    @Override 
    public boolean delete(Account t) { 
     throw new UnsupportedOperationException("Not supported yet."); 
    } 
} 

的事情是,我我很確定我不會t想要使用已經在DAO接口中的SQL字符串,但是接下來我會遇到如何正確執行get()search()的問題?

由於它是一個通用的接口,我不能與列名的工作還沒有,我想保持界面內get()search()方法,以確保它們將被執行,除非是真的neccessary刪除它們從界面。

我提出的建議是由一系列或通過類似未填寫完整的類T的目標因此,如果你想獲得與名稱= X的賬戶,然後你將不得不代碼搜索:

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

但我不確定這是否是最好的解決方案,請幫助我。

問候。

+0

爲什麼重新實施車輪?現有的ORM不會解決你的問題嗎? – Thihara

+0

@Thihara我發現那些難以掌握的人,我已經在互聯網上看過,但他們看起來都很困難。我想早點開始我的應用程序,如果我能讓這部分工作起來,現在看起來已經足夠了。 – skiwi

+0

您的解決方案看起來很清楚。你認爲這有什麼不對? – inigoD

回答

0

這是我如何解決我的:

我創建了一個接受的SQL查詢的抽象類NativeQueryBasedDAO。這樣,我可以有擴展NativeQueryBasedDAO的子類來代表eeach RDBMS(如MySQL,DB2,PostGres等)。

在你的情況,你就會有這樣的效果的東西:

public abstract class NativeQueryBasedDAO<T> implements DAO<T> { 

    private Connection connection; 
    private String schemaName; 
    private String tableName; 

    protected NativeQueryBasedDAO(Connection connection, String tableName) { 
     this(connection, null, tableName); 
    } 

    protected NativeQueryBasedDAO(Connection connection, String schemaName, String tableName) { 
     if (schemaName == null) { 
      this.schemaName = ""; 
     } else { 
      this.schemaName = schemaName; 
     } 
     this.connection = connection; 
     this.tableName = tableName; 
    } 

    protected final String getTableName() { 
     return tableName; 
    } 

    protected final String getSchemaName() { 
     return schemaName; 
    } 

    protected final Connection getConnection() { 
     return connection; 
    } 
} 

以上字段可以幫助您在方法,如get(),你需要得到一個對象根據您schemaName + tableName

然後,我會有一個Factory<T>有一個public T create()創建一個對象(在你的情況下,帳戶)。

因此,基於一些Parameters, you can pass/generate a工廠`,將生成一個對象。

然後,您可以修改您的DAO做一個get(Criteria criteria),它將生成一個帶有參數的Factory以填充以創建您的對象。簡而言之,這不是一個簡單的解決方案(因爲它必須是健壯的,並根據您的需求增長)。諸如Hibernate或JPA實現的ORM(例如TopLink)處理這個問題。

我希望這會有所幫助。