[英]Java DAO for all class
I'm new to Java and I must make some DAO for my app. 我是Java新手,必须为我的应用程序做一些DAO。 However, I don't want to make a DAO for each class (with interface) et override methods.
但是,我不想为每个类(带有接口)和重写方法都做一个DAO。 Is it possible to make a DAO extended by all the others, with methods working with all kind of Class ?
是否有可能通过使用适用于所有Class的方法来将DAO扩展为其他所有对象? For example, a DAO that could handle class MyClass and class Foo with a single mehtod getList().
例如,一个DAO可以通过一个方法getList()处理MyClass类和Foo类。 Thank you !
谢谢 !
Not that gooed idea, in general, but... 一般而言,这不是个好主意,但是...
If it is about low-level JDBC (no framework like Hibernate, Spring, etc.), then: 如果是关于低级JDBC(没有像Hibernate,Spring等框架),那么:
You can make an AbstractDAO class, then your other DAO-classes (UserDAO, ProductDAO, etc.), then you can make a CommonService class that has all those DAO-classes and provides the functions you need. 您可以创建一个AbstractDAO类,然后创建其他DAO类(UserDAO,ProductDAO等),然后创建具有所有这些DAO类并提供所需功能的CommonService类。
Example: 例:
abstract class AbstractDAO {
private DataSource dataSource;
protected getDataSource() { // Inject it or hard-coded dataSource
return dataSource;
}
}
public class UserDAO extends AbstractDAO {
public User read(long id) {
// blablabla
return user;
}
public List<User> findAll() {
// blablabla
return users;
}
// and so on...
}
public class ProductDAO extends AbstractDAO {
public Product read(long id) {
// blablabla
return product;
}
public List<Product> findAll() {
// blablabla
return products;
}
// and so on...
}
Then other repositories, and then: 然后是其他存储库,然后:
public class CommonService {
private final UserDAO userDAO = new UserDAO();
private final ProductDAO productDAO = new ProductDAO();
// other repositories
public User readUser(long id) {
return userDAO.read(id);
}
public Product readProduct(long id) {
return productDAO.read(id);
}
public List<User> findAllUsers() {
return userDAO.findAll();
}
public List<Product> findAllProducts() {
return productDAO.findAll();
}
}
And if you mean you want to make a generic repository (DAO), again not that good idea, because Spring has already made it in a quite good way (it calls JpaRepository
, eg interface MyRepository extends JpaRepository<User, Long> { }
): Source: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.multiple-modules 而且,如果您是想创建通用存储库(DAO),那也不是一个好主意,因为Spring已经以相当不错的方式实现了它(它调用
JpaRepository
,例如, interface MyRepository extends JpaRepository<User, Long> { }
) :来源: https : //docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.multiple-modules
But if you want, you can make such a mechanism too, based on something like this (but it will be a cumbersome work to make it work like it does in Spring, for instance; because they are a team of experts who worked day and night to realize such a tremendous project): 但是,如果您愿意,您也可以基于这样的机制(例如,使其像Spring那样工作,将是一项繁琐的工作;因为他们是一支每天工作的专家团队,晚上实现如此巨大的项目):
public abstract class Repo<T, K> {
public abstract T read(K id);
public abstract List<T> findAll();
}
or 要么
public interface Repo<T, K> {
T read(K id);
List<T> findAll();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.