在三層結構中,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> {}
