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