使用 Spring Hibernate 使用注释编写更少的 DAO
我的 Spring+Hibernate 配置文件很小而且非常紧凑。我使用自动扫描来查找我的模型实体/DAOS。
我不想为层次结构中的每个实体编写 DAO + DAOImpl。
有些人可能有资格拥有自己的,例如,如果他们与其他实体有复杂的关系并且需要的不仅仅是基本的 CRUD 功能。但对于其余的......
有什么办法可以绕过事实上的标准吗?
比如说,类似于通用 DAO,例如:
http://www.ibm.com/ developerworks/java/library/j-genericdao/index.html
然后我可以做类似的事情,
GenericDao dao = appContext.getBean("genericDao");
dao.save(car);
dao.save(lease);
这可以使用注释吗?我不想在 xml 中配置任何内容。如果我不能执行上述操作,是否仍然可以拥有一个 GenericDaoImpl.java ,其中包含以下内容:
@Repository("carDao")
@Repository("leaseDao")
class GenericDaoImpl extends CustomHibernateDaoSupport implements GenericDao {
...
}
然后
GenericDao dao = appContext.getBean("carDao");
dao.save(car);
dao = appContext.getBean("leaseDao"); //carDao is garbage coll.
dao.save(lease);
这是否实用?
My Spring+Hibernate configuration files are small and super tight. I use auto scanning to find my model entities/daos.
I don't want to have to write a DAO + DAOImpl for EVERY Entity in my hierarchy.
Some may qualify to have their own, like if they have complex relationships with other entities and require more than basic CRUD functionality. But for the rest...
Is there any way to circumvent the defacto standard?
Say, something like a generic DAO, ex:
http://www.ibm.com/developerworks/java/library/j-genericdao/index.html
Then I can do something like
GenericDao dao = appContext.getBean("genericDao");
dao.save(car);
dao.save(lease);
Is this possible with annotations? I don't want to have to configure anything in xml. If I cannot do above, is it still possible to have one GenericDaoImpl.java with something like:
@Repository("carDao")
@Repository("leaseDao")
class GenericDaoImpl extends CustomHibernateDaoSupport implements GenericDao {
...
}
and then
GenericDao dao = appContext.getBean("carDao");
dao.save(car);
dao = appContext.getBean("leaseDao"); //carDao is garbage coll.
dao.save(lease);
Is this practical at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用泛型,你可以尝试这样的事情:
内容可能不同,但总体思想是适用的。
然后,您应该能够使用以下命令在控制器和服务类中自动装配 DAO
Using generics, you might try something like this:
The content may be different, but the general idea is applicable.
You should be able to then autowire the DAO in your controller and service classes by using
您可以结合 Spring /Hibernate with JPA,它为大量基本持久性任务提供了 EntityManager:
它还将处理事务和简单查询,而无需编写 DAO。对于更复杂的操作,您仍然可以编写 DAO 并回退到 Hibernate 的 SessionFactory(尽管这里也可以选择 JPA)。
一些教程建议您仍然应该编写 DAO 来抽象 JPA 管道。然而,我个人认为这是不必要的(JPA 的集成足迹非常小),事实上这也是这样的方式 Spring Roo 处理幕后的数据层。
You can combine Spring/Hibernate with JPA, which provides the EntityManager for a large amount of basic persistence tasks:
It will also handle transactions and simple queries without needing to write a DAO. For the more complex operations, you can still write a DAO and fall back to Hibernate's SessionFactory (although JPA is an option here too).
Some tutorials suggest you should still write the DAO for abstraction of the JPA plumbing. However, I have personally found this unnecessary (JPA has a very small integration footprint), and in fact this is also the way Spring Roo deals with the data layer behind the scenes.
您是否尝试过使用Spring Data。我的意思是说Spring JPA,您可以在其中使用存储库。
您可以消除为每个实体编写所有内容的情况。
Have you tried to use Spring Data. I mean to say Spring JPA where you can use repositories.
You can eliminate writing all stuffs for each entity.