문제

나는 여전히 봄을 처음 접했고, 나는이 모든 crud daos를 만드는 것을 자극하는 것을 발견했습니다. 그래서 나는 "공공 클래스 GenericCruddao는 Cruddao를 확장했습니다. 내 서비스 개체에서 나는 단순히

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

그리고 더 이상 간단한 daos를 쓰고 그들을 연결할 필요가 없습니다. 예! 한 가지를 제외하고 나는 당신이 경험 한 모든 봄 개발자들이 즉시 볼 것이라고 확신합니다. 나는 GenericCruddao 내에서 최대 절전 모드 템플릿을 얻을 수 없습니다.

HibernateTemplate ht = getHibernateTemplate();

나에게 null 인 HT를 준다. 그렇게 좋지 않습니다. 나는 그것을 배선하는 것을 생각하여 일반적인 annotationsessionsectionfactorybean을 설정 한 다음 여전히 hibernateTemplate을 제공하지는 않을 것입니다. 최대 절전 모드 템플릿을 가질 수 있도록 그 주변의 작업에 대한 제안이 있습니까?

내가 생각해야 할 일반적인 크루드 dao를 만드는 데 더 많은 문제가 있습니까?

건배

도움이 되었습니까?

해결책

다수를 위해서, HibernateTemplate 그리고 HibernateDaoSupport 외부에 있고 대신 a SessionFactory 선호됩니다. 모든 사람이 아니라, 당신을 염두에 두지 만, 너무 오래 전에 입양 한 트렌드입니다. HibernateTemplate 내 자신의 제네릭 다오에서.

이 블로그 꽤 좋은 요약이 있습니다.

저자의 사례는 원하는 곳으로가는 데 도움이 될 수 있어야합니다.

다른 팁

제네릭 다오

글쎄, 나에게 GenericDao가 '일반'인 경우 인스턴스가 하나만 필요할 수 있습니다., 그리고 그 단일 인스턴스로 모든 작업을 수행하십시오.

나는 그것이 당신이 예를 들어, 당신이 화를내는 반복에 있지 않다고 확신합니다 (그리고 나는 당신과 동의합니다).

예를 들어, 엔티티 클래스를 일반 방법으로 전달할 수 있습니다..

  • 공개 void save (class, e ...) : E 유형의 하나 이상의 인스턴스를 저장할 수 있습니다. E는 엔티티 중 하나입니다.
  • Public e Load (클래스, 긴 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는 생성자에 필요한 클래스 매개 변수를 제공하여 GenericDao를 확장합니다. 샘플 코드 (완전하지 않고 기본 아이디어를 얻기가 간단함) :

    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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top