【问题标题】:How to make a generic interfaces in dao classes如何在 dao 类中制作通用接口
【发布时间】:2019-10-11 10:22:45
【问题描述】:

我有 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);
}

我该怎么做?提前致谢

【问题讨论】:

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


【解决方案1】:

为此,您应该使用泛型。

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

【讨论】:

    猜你喜欢
    • 2019-09-03
    • 2011-03-30
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    相关资源
    最近更新 更多