我正在研究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));
但我不確定這是否是最好的解決方案,請幫助我。
問候。
爲什麼重新實施車輪?現有的ORM不會解決你的問題嗎? – Thihara
@Thihara我發現那些難以掌握的人,我已經在互聯網上看過,但他們看起來都很困難。我想早點開始我的應用程序,如果我能讓這部分工作起來,現在看起來已經足夠了。 – skiwi
您的解決方案看起來很清楚。你認爲這有什麼不對? – inigoD