Java 什么是DAO工厂模式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6401543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is DAO factory pattern?
提问by coder25
I am aware of factory and abstract factory methods, but I want to create a DAO factory pattern in Java.
我知道工厂方法和抽象工厂方法,但我想用 Java 创建一个 DAO 工厂模式。
- I want to know its importance.
- Its usage
- 我想知道它的重要性。
- 它的用法
I have checked this linkbut it is difficult for me to understand.
我已经检查了这个链接,但我很难理解。
Can anyone explain it with the help of an example?
任何人都可以在示例的帮助下解释它吗?
Edit:Here is an example of DAO pattern as I understood it:
编辑:这是我理解的 DAO 模式示例:
public interface UserDAO {
public void insert(User user);
public void update(User user);
public void delete(int userId);
}
Implementation:
执行:
public class UserDAOImpl implements UserDAO {
@Override
public void delete(int userId) {
// delete user from user table
}
@Override
public User[] findAll() {
// get a list of all users from user table
return null;
}
@Override
public User findByKey(int userId) {
// get a user information if we supply unique userid
return null;
}
@Override
public void insert(User user) {
// insert user into user table
}
@Override
public void update(User user) {
// update user information in user table
}
}
Factory:
工厂:
public class UserDAOFactory {
public static UserDAO getUserDAO(String type) {
if (type.equalsIgnoreCase("jdbc")) {
return new UserDAOImpl();
} else {
return new UserDAOImpl();
}
}
}
Client side code:
客户端代码:
User user=new User();
user.setName("Jinoy P George");
user.setDesignation("Programmer");
user.setAge(35);
//get a reference to UserDAO object
UserDAO userDAO=UserDAOFactory.getUserDAO("jdbc");
//call insert method by passing user object
userDAO.insert(user);
Is this dao pattern correct?
这个 dao 模式正确吗?
Where should I open connection and close it?
我应该在哪里打开连接并关闭它?
采纳答案by Adrian E. Labastida Ca?izares
Probably what you don't understand is how the code works? It seems fine.
可能你不明白的是代码是如何工作的?看起来不错。
Just FYI:
仅供参考:
What is defined as UserDAOImpl can be better understood if you consider naming it as UserDAOMySQLImpl and another new one as UserDAOMSSQLImpl, and so on for each database access you may need.
In each of those you should handle the connections and add other things like private functions for that specific database server configuration it may need and not forcibly needed to be declared in the interface (UserDAO) but as minimum you must always implement all the methods defined in the interface, then in the Factory (UserDAOFactory) conditions you could have something like this:
如果您考虑将其命名为 UserDAOMySQLImpl 并将另一个新命名为 UserDAOMSSQLImpl,则可以更好地理解定义为 UserDAOImpl,对于您可能需要的每个数据库访问,依此类推。
在其中的每一个中,您应该处理连接并为该特定数据库服务器配置添加其他诸如私有函数之类的东西,它可能需要而不是强制需要在接口(UserDAO)中声明,但至少您必须始终实现中定义的所有方法接口,然后在工厂 (UserDAOFactory) 条件中,您可以有这样的东西:
`
`
public class UserDAOFactory{
public static UserDAO getUserDAO(String type){
if (type.equalsIgnoreCase("mysql")){
return new UserDAOMySQLImpl();
}else{
return new UserDAOMSSQLImpl();
}
}
}
A little clearer?
清晰一点?
Then, in the client side instead of a hardcoded line like:
然后,在客户端而不是像这样的硬编码行:
UserDAO userDAO=UserDAOFactory.getUserDAO("jdbc");
You could have a properties file to be able to switch between DAOs dynamically, having retrieved that string from the properties file you can simply do:
您可以拥有一个属性文件,以便能够在 DAO 之间动态切换,从属性文件中检索该字符串后,您可以简单地执行以下操作:
UserDAO userDAO=UserDAOFactory.getUserDAO(myStringFromPropertiesFile);
myStringFromPropertiesFile would contain "mysql" or "mssql" according to the definition in your properties file.
根据属性文件中的定义,myStringFromPropertiesFile 将包含“mysql”或“mssql”。
Hope this helps!
希望这可以帮助!
回答by duffymo
DAO stands for "Data Access Object". It's an interface-based class that handles all your CRUD operations with a relational database for a particular object. Here's an example that uses generics:
DAO 代表“数据访问对象”。它是一个基于接口的类,它使用特定对象的关系数据库处理所有 CRUD 操作。这是一个使用泛型的示例:
package persistence;
public interface GenericDao<K extends Serializable, T>
{
public T find(K id);
public List<T> find();
public K save(T value);
public void update(T value);
public void delete(T value);
}
Think of a factory as a "virtual constructor": its creation method returns an interface type, but you can ask it to create any number of different implementations as needed.
将工厂视为“虚拟构造函数”:它的创建方法返回一个接口类型,但您可以根据需要要求它创建任意数量的不同实现。