我无法在java中使用SQLite获得DAO/DTO模式
我一直试图理解DAO模式,但现在我没有成功。可能是因为我无法将在互联网上找到的东西应用到我试图解决的问题上。我想封装数据库,把事情做好 到目前为止我都这么做了,但我觉得这很没用 我的DTO类:我无法在java中使用SQLite获得DAO/DTO模式,java,dao,data-access-object,Java,Dao,Data Access Object,我一直试图理解DAO模式,但现在我没有成功。可能是因为我无法将在互联网上找到的东西应用到我试图解决的问题上。我想封装数据库,把事情做好 到目前为止我都这么做了,但我觉得这很没用 我的DTO类: public class PersonDTO{ final public static String TABLE = "PEOPLE"; private int id; private String name; public int g
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);
}
}
}
我找不到:
这应该是DTO类和
道类。
DAO类必须具有从中获取信息的方法
数据库?
DTO类的用途是什么,为什么不使用Person类呢?。
这相当令人沮丧。如果有任何帮助,我将不胜感激。DAO代表数据访问对象。顾名思义,它的职责是访问数据。这意味着它知道如何使用数据连接从数据存储中检索对象
DTO代表数据传输对象。它的职责是将数据格式封装在编程语言结构中,使其易于在代码中使用
在我看来,DAO应该处理您的对象模型,而不是DTO。换句话说,接口应该返回Person而不是PersonDTO。在DAO实现内部,可以方便地使用中间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;
}
}
在这里,在您的示例中,拥有DTO可能是无用的,但一般来说并不是无用的 DTO对象应该是远程服务RMI/Web服务请求的输出 在进程之间传送数据以减少方法调用数量的对象。 参考: 这个对象应该携带数据以减少方法调用的数量 正如您使用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还可以携带聚合数据。它应该是远程方法的外部视图。普通对象在内部视图中是私有的,您只能与之交互。一般来说,DTO类是帮助类,它保存任何层所需的数据,例如:-DAO、服务等。DAO是一个层,它执行所有持久性操作,如保存、更新、删除等。DAO和DTO都是两个不同的概念!stmt是什么类的实例?那么DTO类不是必需的吗?它是PreparedStatement的一个实例。我更新了我的示例。