我正在研究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()
和{{1}的问题好吗?
由于它是一个通用接口,我还不能使用列名称,我希望在接口中保留search()
和get()
方法,以确保它们将被实现,除非从界面中删除它们是非常必要的。
我建议的建议是按照范围或未完全填写的T类对象进行搜索。因此,如果您想获得名称= x的帐户,那么您将拥有以下代码:
search()
但我不确定这是否是最佳解决方案,请帮助我。
问候。
答案 0 :(得分: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
Factory`。
然后,您可以修改DAO
以构建get(Criteria criteria)
来构建Factory
,并使用参数填充以创建对象。
简而言之,它不是一个简单的解决方案(因为它必须是健壮的,并根据您的要求增长)。诸如Hibernate或JPA实现之类的ORM(例如TopLink)处理这个问题。
我希望这会有所帮助。