BaseMapper和继承


在三层结构中,controller层,service层,dao层,其中dao层负责和数据库交互,dao层对应着mapper.xml,而通过代码生成的dao层,仔细观察会发现,方法都是差不多的,具有共性,那就把这些相同的方法提取出来形成BaseMapper,之后的dao层只需要继承它即可,这样就会减少大量的代码冗余了。

BaseMapper接口如下:

public interface BaseMapper<T, S> {
    int countByExample(S example);

    int deleteByExample(S example);

    int deleteByPrimaryKey(Integer pid);

    int insert(T record);

    int insertSelective(T record);

    List<T> selectByExample(S example);

    T selectByPrimaryKey(Integer pid);

    int updateByExampleSelective(@Param("record") T record, @Param("example") S example);

    int updateByExample(@Param("record") T record, @Param("example") S example);

    int updateByPrimaryKeySelective(T record);

    int updateByPrimaryKey(T record);

    int save(List<T> req);

    int delete(List<T> req);

    int update(T req);
}

在dao层中继承该BaseMapper,如下:

public interface PcNLatBluepayNotifyMapper<T, S> extends BaseMapper<T, S> {}


免责声明!

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



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