我有 10-12 个表,其中有我的主数据,我必须使用 hibernate 的 findAll()
工具加载它们。
因此,对于所有这些 10-12 表,我需要编写与此相同的方法
public List<XYZType> getAllXYZType() {
return XYZRepository.findAll();
}
所以这种代码对于不同的实体会重复10-12次。 有什么办法可以让它通用吗?
最佳答案
public <T> List<T> getAll(final Class<T> type) {
Session session = sessionFactory.getCurrentSession();
Criteria crit = session.createCriteria(type);
return crit.list();
}
为了使用它,只需将所需的类传递给函数,它将返回列表。
关于java - 如何在Java中从主列表中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32479998/