[英]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.