Java/sql实验DAO实现
我正在研究Java(web)应用程序中数据库的DAO实现。只是我遇到了一个小问题 我当前的代码: Account.java:Java/sql实验DAO实现,java,sql,dao,Java,Sql,Dao,我正在研究Java(web)应用程序中数据库的DAO实现。只是我遇到了一个小问题 我当前的代码: Account.java: package beans; public class Account { private int accountId; private String name; private String password; private String email; public Account() { } publi
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);
}
但我不确定这是否是最好的解决方案,请帮助我
问候。我就是这样处理我的问题的: 我创建了一个接受SQL查询的抽象类
NativeQueryBasedDAO
。通过这种方式,我可以拥有扩展表示每个RDBMS的NativeQueryBasedDAO
的子类(如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;
}
}
公共抽象类NativeQueryBasedDAO实现DAO{
专用连接;
私有字符串模式名;
私有字符串表名;
受保护的NativeQueryBasedDAO(连接,字符串表名){
此(连接,null,tableName);
}
受保护的NativeQueryBasedDAO(连接、字符串模式名、字符串表名){
if(schemaName==null){
this.schemaName=“”;
}否则{
this.schemaName=schemaName;
}
这个连接=连接;
this.tableName=tableName;
}
受保护的最终字符串getTableName(){
返回表名;
}
受保护的最终字符串getSchemaName(){
返回模式名;
}
受保护的最终连接getConnection(){
回路连接;
}
}
上面的字段可以帮助您使用诸如get()
之类的方法,您需要根据schemaName+tableName
获取对象
然后,我会有一个工厂
,它有一个public T create()
,它创建一个对象(在您的例子中是account)
因此,基于一些参数,您可以传递/生成一个将生成对象的工厂
然后,您可以修改DAO
以执行get(Criteria)
操作,该操作将构建一个工厂
,其中包含要填充的参数以创建对象
简言之,它不是一个简单的解决方案(因为它必须是健壮的,并根据您的需求增长)。ORM,如Hibernate或JPA实现(如TopLink)处理此问题
我希望这会有所帮助。为什么要重新实施控制盘?现有的ORM不能解决你的问题吗?@Thihara我发现这些很难掌握,我肯定已经在互联网上四处寻找过了,但它们看起来都很难。我想从我的应用程序soonish开始,如果我能让这部分工作,现在看来就足够了。你的解决方案看起来很清楚。您认为它有什么问题?@skiwi您编写自己的解决方案所花费的时间将比使用JPA/Hibernate所花费的时间多得多。这确实是您不想自己解决的问题之一。您的get()
和search()
方法的确切问题是什么?
AccountDAO accountDAO = DAOFactory.getAccountDAO();
Account result = accountDAO.get(new Account(x, null, null));
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;
}
}