泛型DAO接口

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了泛型DAO接口相关的知识,希望对你有一定的参考价值。

  1. /**
  2.  * Defines a generic DAO interface.
  3.  *
  4.  * @author javier
  5.  *
  6.  * @param <T>
  7.  * The type this DAO handles.
  8.  * @param <ID>
  9.  * The type of the id of the handled entity.
  10.  */
  11. public interface DAOInterface<T, ID extends Serializable>
  12. {
  13.  
  14. /**
  15.   * Makes the given entity persistent.
  16.   *
  17.   * @param entity The entity to persist.
  18.   * @throws When the quota of instances for that entity was exceeded.
  19.   */
  20. void makePersistent(T entity) throws QuotaExceededException;
  21.  
  22. /**
  23.   * Makes the given entity transient.
  24.   *
  25.   * @param entity The entity to make transient.
  26.   */
  27. void makeTransient(T entity);
  28.  
  29. /**
  30.   * @return the number of matching entities.
  31.   */
  32. int count(Filter filter);
  33.  
  34. /**
  35.   * Returns a single entity which has the given ID or throws an
  36.   * <code>EntityNotFoundException</code> if no matching entity is found.
  37.   *
  38.   * @param id
  39.   * The ID of the entity to return.
  40.   * @return The entity.
  41.   */
  42. T findById(ID id) throws EntityNotFoundException;
  43.  
  44. /**
  45.   * Returns all matching entities of type <code>T</code>.
  46.   *
  47.   * @param filter
  48.   * The <code>Filter</code> to use.
  49.   *
  50.   * @return An ordered <code>List</code> with the entities.
  51.   */
  52. List<T> find(Filter filter);
  53.  
  54. /**
  55.   * Returns all entities of type <code>T</code>.
  56.   *
  57.   * @return An ordered <code>List</code> with the entities.
  58.   */
  59. List<T> findAll();
  60.  
  61. /**
  62.   * Returns a page of entities.
  63.   *
  64.   * @param filter
  65.   * The <code>Filter</code> to use.
  66.   * @param startRow
  67.   * The offset.
  68.   * @param pageSize
  69.   * The number of entities to return.
  70.   *
  71.   * @return A <code>Pair</code> of values. The first value is the
  72.   * <code>List</code> of entities and the second one if the total
  73.   * number of existing entities.
  74.   */
  75. Pair<List<T>, Integer> findPaged(Filter filter, int startRow, int pageSize);
  76.  
  77. /**
  78.   * Flushing is the process of synchronising the underlying persistent
  79.   * store with persistable state held in memory.
  80.   */
  81. void flush();
  82. }

以上是关于泛型DAO接口的主要内容,如果未能解决你的问题,请参考以下文章

使用工厂模式与泛型对三层架构的每一层进行解耦

Spring + Hibernate DAO 作为泛型类

JPA的泛型DAO设计及使用

如何使用泛型实现 Hibernate DAO

Spring 数据存储库和 DAO Java 泛型

Hibernate 泛型Dao实现