2

我一直在尝试理解 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;
        }
    }

我的“道”

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”类呢?

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

4

2 回答 2

2

在您的示例中, aDTO可能没用,但一般来说并非没用。

DTOobject 应该是 a 的输出remote service (RMI/Web service request)

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

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

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

就像你用来PersonDTO携带Person表格数据一样。但是PersonDTO,只为一个对象创建是没有用的,而不是只为 user 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还可以携带汇总数据。它应该是您的远程方法的外部视图。普通对象是私有的内部视图,仅供您交互。

于 2016-06-07T05:26:57.427 回答
1

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

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

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

以下是我将如何实施:

// 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;
    }
}
于 2016-06-07T05:03:33.863 回答