JPAの抽象クラスと汎用インタフェース

3972 ワード

エンティティークラス抽象親
@MappedSuperclass
@Data
public abstract class CommonEntity {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  @Temporal(TemporalType.TIMESTAMP)
  @CreatedDate
  private Date createTime;

  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  @Temporal(TemporalType.TIMESTAMP)
  @LastModifiedDate
  private Date modifiedTime;
}

抽象インタフェースレイヤ
@NoRepositoryBean
public interface BaseRepositoryextends Serializable> extends Repository {

    /**
     * Deletes a managed entity.
     * @param id    The id of the deleted entity.
     * @return      an {@code Optional} that contains the deleted entity. If there
     *              is no entity that has the given id, this method returns an empty {@code Optional}.
     */
    Optional deleteById(ID id);
}

ソリッドインタフェース
interface TodoRepository extends BaseRepository {

    List findAll();

    /**
     * Finds todo entries by using the search term given as a method parameter.
     * @param searchTerm    The given search term.
     * @return  A list of todo entries whose title or description contains with the given search term.
     */
    @Query("SELECT t FROM Todo t WHERE " +
            "LOWER(t.title) LIKE LOWER(CONCAT('%',:searchTerm, '%')) OR " +
            "LOWER(t.description) LIKE LOWER(CONCAT('%',:searchTerm, '%')) " +
            "ORDER BY t.title ASC")
    List findBySearchTerm(@Param("searchTerm") String searchTerm);

    Optional findOne(Long id);

    void flush();

    Todo save(Todo persisted);
}

 
全体的に広がり方が多いです
転載先:https://www.cnblogs.com/zhouwenyang/p/11136536.html