【问题标题】:I can't get the DAO/DTO pattern with SQLite in java我无法在 java 中使用 SQLite 获得 DAO/DTO 模式
【发布时间】:2016-10-06 20:16:33
【问题描述】:

我一直在尝试理解 DAO 模式,但现在我没有成功。可能是因为我无法将我在互联网上找到的内容应用于我尝试解决的问题。我要封装数据库,把事情做好。

到目前为止,我一直这样做,但我觉得它很没用。

我的 DTO 课程:

   public class PersonDTO{
        final public static String TABLE = "PEOPLE";
        private int id;
        private String name;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }

我的“DAO”

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public class PersonDAO {
    private Connection connection;
    private DTO dto;
    private Statement stmt = null;
    private String tableName;
    private Integer id;

    public PersonDAO() {

    }

    public PersonDTO getPerson(int id) {
        connection = ConnectionFactory.getInstance();
        PersonDTO person = new PersonDTO();
        try {
            stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM " + PersonDTO.TABLE +" WHERE ID = '"+id+"'");
            person.setId(rs.getInt("id"));
            person.setName(rs.getString("age"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        closeConnection();
        return person;
    }

    public void save() {
        throw new UnsupportedOperationException(); //not implemented yet
    }

    public void update() {
        throw new UnsupportedOperationException(); //not implemented yet
    }

    public void delete() {
        throw new UnsupportedOperationException(); //not implemented yet
    }

    public List<DTO> getDTO(String filter) {
        return null;
    }

    protected void closeConnection() {
        try {
            connection.close();
            connection = null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

我无法获得:

  1. 应该是DTO类和 DAO 类。
  2. DAO 类必须具有从中获取信息的方法 数据库?
  3. DTO 类有什么用,为什么不使用“Person”类呢?。

这很令人沮丧。如有任何帮助,我将不胜感激。

【问题讨论】:

  • 一般DTO 类是helper 类,其中包含任何layer 所需的数据例如:-DAOSERVICE 等,DAO 是一个层,它是执行所有persistence operations 之类的,save,update,delete etc. DAODTO 是两个不同的概念

标签: java dao data-access-object


【解决方案1】:

DAO 代表Data Access Object。顾名思义,它的职责是访问数据。这意味着它知道如何使用数据连接从数据存储中检索对象。

DTO 代表Data Transfer Object。它的职责是将数据格式封装在一种编程语言结构中,使其易于在代码中使用。

在我看来,DAO 应该处理您的对象模型,而不是 DTO 的。换句话说,接口应该返回Person 而不是PersonDTO。在您的 DAO 实现内部,可能使用中介 DTO 对象来帮助您获取和存储对象会很方便。如果您使用的是 Hibernate 和/或 JPA,您可以创建一个带有 JPA 注释的 DTO

下面是我的实现方式:

// Define an interface for your DAO so you can mock in unit tests, or swap out an implementation if you decide not to use SQLite
public interface PersonDao {
    Person getPerson(int id) throws PersonDaoException;
}

// Write your implementation for SQLite. I fixed some design/implementation issues
class PersonDaoSqlite implements PersonDao {
    private final DataSource ds;

    public PersonDaoSqlite(DataSource ds) {
        this.ds = ds;
    }

    public Person getPerson(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet result = null;

        // As of Java 7 and onwards, one can use try-with-resources here.
        try {
            connection = ds.getConnection();
            statement = connection.prepareStatement("SELECT * FROM PEOPLE WHERE ID = ?");
            statement .setInt(1, id);
            result = statement .executeQuery();

            Person person = new Person();
            person.setId(rs.getInt("id"));
            person.setName(rs.getString("age"));
        } catch (SQLException e) {
            throw new PersonDaoException(e);
        } finally {
            if (result != null) {
                result.close();
            }

            if (statement != null) {
                statement.close();
            }

            if (connection != null) {
                connection.close();
            }
        }
    }
}

public class PersonDaoException extends Exception {
     public PersonDaoException(Throwable cause) {
          super(cause);
     }
}

public class Person {
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

【讨论】:

  • 谢谢! stmt是什么类的实例?那么就不需要 DTO 类了吗?
  • 这是PreparedStatement 的一个实例。我更新了我的示例。
【解决方案2】:

在您的示例中,DTO 可能没用,但总的来说并非没用。

DTO 对象应该是remote service (RMI/Web service request) 的输出。

在进程之间携带数据以减少方法调用次数的对象。

参考:http://martinfowler.com/eaaCatalog/dataTransferObject.html

这个对象应该携带数据以减少方法调用的次数。

正如您使用PersonDTO 携带Person 表数据一样。但是,只为一个对象创建PersonDTO 是没有用的,而只是为用户Person 创建。

如果您有人员列表,或者可能是一些其他数据,其中包含有关请求状态的更多信息,如下所示

public class PersonDTO {
    public List<Person> personList;
    public Fault fault;
    
    public class Fault {
        String faultCode;
        String faultMessage
    }
    
    public Date requestDate;
    public UUID requestId;
    public String requestSignature;
    
    ...
}

在这种情况下,使用 DTO 对象是有意义的,因为响应不仅仅是一个人。

DTO也可以携带聚合数据。它应该是您的远程方法的外部视图。普通对象是私有的内部视图,仅供您交互。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-30
    • 2012-09-15
    • 2015-12-19
    • 2011-02-05
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    相关资源
    最近更新 更多