[英]Java/sql experimental DAO implementation
我正在研究Java(Web)應用程序中數據庫的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.");
}
}
問題是我很確定自己不想在DAO
接口中使用SQL字符串,但是我有一個問題,即如何正確實現get()
和search()
?
因為它是一個通用接口,所以我現在還不能使用列名,並且我想將get()
和search()
方法保留在該接口內,以確保將它們實現,除非確實需要刪除它們從界面。
我建議的建議是按一個范圍或未完全填充的類T的對象進行搜索。因此,如果您想獲取名稱為x的帳戶,則將具有以下代碼:
AccountDAO accountDAO = DAOFactory.getAccountDAO();
Account result = accountDAO.get(new Account(x, null, null));
但是我不確定這是否是最好的解決方案,請幫助我。
問候。
這就是我的解決方法:
我創建了一個抽象類NativeQueryBasedDAO
,它接受SQL查詢。 這樣,我就可以擁有擴展NativeQueryBasedDAO
子類,該子類表示NativeQueryBasedDAO
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
獲取對象。
然后,我將擁有一個具有public T create()
的Factory<T>
,該public T create()
創建一個對象(在您的情況下為account)。
因此,基於某些Parameters, you can pass/generate a
將生成對象Parameters, you can pass/generate a
Factory`。
然后,您可以修改您的DAO
以執行get(Criteria criteria)
,該操作使用要填充的參數來構建Factory
來創建對象。
簡而言之,這不是一個簡單的解決方案(因為它必須健壯並根據您的要求進行擴展)。 諸如Hibernate或JPA實現(例如TopLink)之類的ORM可以處理此問題。
我希望這有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.