如何在dao类中创建通用接口

时间:2019-10-11 10:22:45

标签: java generics model-view-controller interface dao

我有3个带有equals方法的equals接口,但它返回的值取决于实体。 在这三个类(老师,小组,学生)中,我有相同的接口方法。

我提供了一个GroupDao界面示例。

public interface GroupDao {
    void add(Group group);
    List<Group> getGroupsList();
    void update(Group group);
    Group findById(Long groupId);
    void delete(Long groupId);
}

我想将其合并为一个界面,例如

public interface EntitiesDao {
    void add({generic} entity);
    List<{generic}> getList();
    void update({generic} entity);
    {generic} findById(Long entityId);
    void delete(Long entityId);
}

我该怎么做?预先感谢

1 个答案:

答案 0 :(得分:3)

为此,您应该使用泛型。

public interface EntitiesDao<T> 
{
    void add(T entity);
    List<T> getList();
    void update(T entity);
    T findById(Long entityId);
    void delete(Long entityId);
}
相关问题