繁体   English   中英

在通用Spring CRUD DAO中获取正确的Hibernate模板

[英]Getting the correct Hibernate template in a generic Spring CRUD DAO

我对Spring还是很陌生,并且发现制作所有这些CRUD DAO令人不快,因此我制作了一个“公共类GenericCRUDDAO扩展了HibernateDaoSupport实现CRUDDAO”。 然后,在服务对象中,我只说类似

private GenericCRUDDAO<User, Integer> userDAO = new GenericCRUDDAO<User, Integer>();

而且,我无需再编写简单的DAO并将其连接起来。 好极了! 除了一件事情,我确定所有经验丰富的Spring开发人员都会立即看到:我无法在GenericCRUDDAO中获取Hibernate模板,因此

HibernateTemplate ht = getHibernateTemplate();

给我的ht为空。 不太好。 我考虑过将其连接起来,这意味着要制作一个通用的CRUDDAO bean,然后设置一个静态的AnnotationSessionFactoryBean,但这仍然不能给我HibernateTemplate。 关于如何解决该问题的任何建议,以便可以使用我的Hibernate模板?

我应该考虑制作通用CRUD DAO的其他问题吗?

干杯

对于许多人来说, HibernateTemplateHibernateDaoSupport市场上,而是首选注入SessionFactory 请注意,不是所有人,但这是一种趋势,我不久前就采用了这种趋势,即从我自己的通用DAO中删除了HibernateTemplate

这个博客有一个很好的总结。

作者的例子应该能够帮助您到达想要的地方。

GenericDao

好吧,对我来说, 如果您的GenericDAO是'generic',那么您可能只需要一个实例 ,并使用该单个实例完成所有操作。

我确信它不会打扰您,例如,反复发脾气(我也同意)。

例如, 您可以将Entity类传递给通用方法

  • public void save(Class,E ...):可以保存一个或多个E类型的实例,E是您的实体之一。
  • public E load(Class,Long id):加载一个实体。
  • ...

     /** Assuming the entities have a superclass SuperEntity with getIdent(). */ public class GenericDaoImpl implements GenericDao { /** Save a bunch of entities */ public void save(SuperEntity... entities) { for(SuperEntity entity : entities) { getSession().save(entity); } } /** Load any entity. */ public <E extends SuperEntity> E load(Class<E> entityClass, Long ident) { return (E)getSession().load(entityClass, ident); } // other generic methods } 

变种

在我们的应用程序中,我们实际上为此有一个变体。 因为每个Dao都有许多特定的请求,所以无论如何我们都需要特定的Dao类(创建并连接它),因此为了避免无法定义的Dao的特殊情况,我们立即制作特定的Dao类。

编码

但是我们绝不会重复代码。 我们所有的Daos扩展了GenericDao,在构造函数中提供了所需的Class参数。 示例代码(不完整,很容易获得基本思想):

    public abstract class GenericDaoImpl<E extends SuperEntity> 
        implements GenericDao<E> {

       /** Available for generic methods, so it is not a parameter 
        * for the generic methods. */
       private final Class<E> entityClass;

       protected GenericDaoImpl(Class<E> entityClass) {
         this.entityClass = entityClass;
       }

       // generic implementation ; can be made efficient, as it may 
       // send the orders as a batch
       public void save(E... entities) {
         for(SuperEntity entity : entities) {
           getSession().save(entityClass, entity.getIdent());
         }
         // possibly add flushing, clearing them from the Session ...
       }

       // other generic methods
    }

    public class PersonDaoImpl extends GenericDaoImpl<Person> 
        implements PersonDao {

      /** Constructor, instanciating the superclass with the class parameter. */
      public PersonDaoImpl() {
        super(Person.class);
      }

      /** Specific method. */
      public List<Person> findByAge(int minAge, int maxAge) {
        //....
      }
    }

布线

接线所有的豆子不是致命的。 如今,有许多自动装配策略,您不必担心。 春季见
http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-annotation-config

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM