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即可。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM