zoukankan      html  css  js  c++  java
  • Java中接口继承泛型接口

      在使用Mybatis做web开发时,每一个模块的数据持久层的接口都会定义:增删改查四个方法。我想为什么不新建一个Base接口来做所有数据持久层的父接口呢?

      于是,我试验了一下,建立了一个泛型接口,里面定义了四个操作数据库的基本方法,对应增删改查:

    public interface BaseMapper<T> {
      public T selectByPrimaryKey(Integer id);
      public void insert(T t);
      public void updateByPrimaryKey(Integer id);
      public void deleteByPrimaryKey(Integer id);
    }

      然后新建User的数据库持久层接口UserMapper继承BaseMapper:

    public interface UserMapper extends BaseMapper<User>{
      // public void insertUser(User usr);
      // public User selectByPrimary(Integer userId);
      // public void update(User usr);
      public int selectCountByEmail(String email);
      public User selectByEmailAndPass(User usr);

    }

       测试可行。这样就不用每次创建持久层接口的时候都要写一遍增删改查的方法,只需该接口继承自BaseMapper即可。

  • 相关阅读:
    泛型简介
    单元测试(junit使用)
    枚举简介
    面试题:二叉树的镜像
    面试题:和为S的连续正数列
    面试题:丑数
    面试题:合并两个排序的链表
    面试题:数值的整数次方
    面试题:矩形覆盖
    面试题:数组中的逆序对
  • 原文地址:https://www.cnblogs.com/jpfss/p/8276597.html
Copyright © 2011-2022 走看看