简体   繁体   English

Java / sql实验性DAO实现

[英]Java/sql experimental DAO implementation

I am working on a DAO implementation of a database in a Java (web) application. 我正在研究Java(Web)应用程序中数据库的DAO实现。 Only I have run into a slight issue. 只有我遇到了一个小问题。

My current code: 我当前的代码:

Account.java: 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: 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: 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.");
    }
}

The thing is that I am pretty sure that I do not want to use SQL strings already in the DAO interface, but then I have the issue of how will I implement get() and search() properly? 问题是我很确定自己不想在DAO接口中使用SQL字符串,但是我有一个问题,即如何正确实现get()search()

As it is a generic interface, I cannot work with the columns names yet and I would like to keep the get() and search() methods inside the interface, to ensure that they will be implemented, unless it is really neccessary to remove them from the interface. 因为它是一个通用接口,所以我现在还不能使用列名,并且我想将get()search()方法保留在该接口内,以确保将它们实现,除非确实需要删除它们从界面。

My proposed suggestion is to search by a range or by an not-completely filled in object of class T. So if you would want to get the account with name=x, then you would have code like: 我建议的建议是按一个范围或未完全填充的类T的对象进行搜索。因此,如果您想获取名称为x的帐户,则将具有以下代码:

AccountDAO accountDAO = DAOFactory.getAccountDAO();
Account result = accountDAO.get(new Account(x, null, null));

But I am unsure if this would be the best solution, please help me. 但是我不确定这是否是最好的解决方案,请帮助我。

Regards. 问候。

This is how I tackled mine: 这就是我的解决方法:

I created an abstract class NativeQueryBasedDAO that accepts SQL queries. 我创建了一个抽象类NativeQueryBasedDAO ,它接受SQL查询。 This way, I can have subclasses that extends NativeQueryBasedDAO that represents eeach RDBMS (like MySQL, DB2, PostGres, etc.). 这样,我就可以拥有扩展NativeQueryBasedDAO子类,该子类表示NativeQueryBasedDAO RDBMS(例如MySQL,DB2,PostGres等)。

In your case, you will have something of this effect: 就您而言,您将获得以下效果:

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;
    }
}

The above fields can help you in methods such as get() where you need to get an Object based on your schemaName + tableName . 上面的字段可以在诸如get()方法中为您提供帮助,在这些方法中,您需要根据schemaName + tableName获取对象。

Then, I would have an Factory<T> that has a public T create() that creates an object (in your case, account). 然后,我将拥有一个具有public T create()Factory<T> ,该public T create()创建一个对象(在您的情况下为account)。

So, based on some Parameters, you can pass/generate a Factory` that will generate an object. 因此,基于某些Parameters, you can pass/generate a将生成对象Parameters, you can pass/generate a Factory`。

You can then modify your DAO to do a get(Criteria criteria) that builds a Factory with parameters to populate to create your object. 然后,您可以修改您的DAO以执行get(Criteria criteria) ,该操作使用要填充的参数来构建Factory来创建对象。

In a nutshell, it is not a simple solution (as this must be robust and grow based on your requirement). 简而言之,这不是一个简单的解决方案(因为它必须健壮并根据您的要求进行扩展)。 ORM such as Hibernate or JPA implementations (eg TopLink) handles this. 诸如Hibernate或JPA实现(例如TopLink)之类的ORM可以处理此问题。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM