通用mapper接口說明

轉載自:https://blog.csdn.net/keliyi2007/article/details/78730863
僅給自己查詢觀看

Mapper3接口有兩種形式,一種是提供了一個方法的接口。還有一種是不提供方法,但是繼承了多個單方法的接口,一般是某類方法的集合。 
例如SelectMapper是一個單方法的接口,BaseSelectMapper是一個繼承了4個基礎查詢方法的接口。

基礎接口 Select

接口:SelectMapper 
方法:List select(T record);

說明:根據實體中的屬性值進行查詢,查詢條件使用等號

接口:SelectByPrimaryKeyMapper 
方法:T selectByPrimaryKey(Object key);

說明:根據主鍵字段進行查詢,方法參數必須包含完整的主鍵屬性,查詢條件使用等號

接口:SelectAllMapper 
方法:List selectAll();

說明:查詢全部結果,select(null)方法能達到同樣的效果

接口:SelectOneMapper 
方法:T selectOne(T record);

說明:根據實體中的屬性進行查詢,只能有一個返回值,有多個結果是拋出異常,查詢條件使用等號

接口:SelectCountMapper 
方法:int selectCount(T record);

說明:根據實體中的屬性查詢總數,查詢條件使用等號

基礎接口Insert

接口:InsertMapper 
方法:int insert(T record);

說明:保存一個實體,null的屬性也會保存,不會使用數據庫默認值

接口:InsertSelectiveMapper 
方法:int insertSelective(T record);

說明:保存一個實體,null的屬性不會保存,會使用數據庫默認值

基礎接口Update

接口:UpdateByPrimaryKeyMapper 
方法:int updateByPrimaryKey(T record);

說明:根據主鍵更新實體全部字段,null值會被更新

接口:UpdateByPrimaryKeySelectiveMapper 
方法:int updateByPrimaryKeySelective(T record);

說明:根據主鍵更新屬性不爲null的值

基礎接口Delete

接口:DeleteMapper 
方法:int delete(T record);

說明:根據實體屬性作爲條件進行刪除,查詢條件使用等號

接口:DeleteByPrimaryKeyMapper 
方法:int deleteByPrimaryKey(Object key);

說明:根據主鍵字段進行刪除,方法參數必須包含完整的主鍵屬性

Base組合接口

接口:BaseSelectMapper 
方法:包含上面Select的4個方法

接口:BaseInsertMapper 
方法:包含上面Insert的2個方法

接口:BaseUpdateMapper 
方法:包含上面Update的2個方法

接口:BaseDeleteMapper 
方法:包含上面Delete的2個方法

CURD組合接口

接口:BaseMapper 
方法:繼承了base組合接口中的4個組合接口,包含完整的CRUD方法

Example方法

接口:SelectByExampleMapper 
方法:List selectByExample(Object example);

說明:根據Example條件進行查詢 
重點:這個查詢支持通過Example類指定查詢列,通過selectProperties方法指定查詢列

接口:SelectCountByExampleMapper 
方法:int selectCountByExample(Object example);

說明:根據Example條件進行查詢總數

接口:UpdateByExampleMapper 
方法:int updateByExample(@Param(“record”) T record, @Param(“example”) Object example);

說明:根據Example條件更新實體record包含的全部屬性,null值會被更新

接口:UpdateByExampleSelectiveMapper 
方法:int updateByExampleSelective(@Param(“record”) T record, @Param(“example”) Object example);

說明:根據Example條件更新實體record包含的不是null的屬性值

接口:DeleteByExampleMapper 
方法:int deleteByExample(Object example);

說明:根據Example條件刪除數據

Example組合接口

接口:ExampleMapper 
方法:包含上面Example中的5個方法

Condition方法

Condition方法和Example方法作用完全一樣,只是爲了避免Example帶來的歧義,提供的的Condition方法

接口:SelectByConditionMapper 
方法:List selectByCondition(Object condition);

說明:根據Condition條件進行查詢

接口:SelectCountByConditionMapper 
方法:int selectCountByCondition(Object condition);

說明:根據Condition條件進行查詢總數

接口:UpdateByConditionMapper 
方法:int updateByCondition(@Param(“record”) T record, @Param(“example”) Object condition);

說明:根據Condition條件更新實體record包含的全部屬性,null值會被更新

接口:UpdateByConditionSelectiveMapper 
方法:int updateByConditionSelective(@Param(“record”) T record, @Param(“example”) Object condition);

說明:根據Condition條件更新實體record包含的不是null的屬性值

接口:DeleteByConditionMapper 
方法:int deleteByCondition(Object condition);

說明:根據Condition條件刪除數據

Condition組合接口

接口:ConditionMapper 
方法:包含上面Condition中的5個方法

RowBounds

默認爲內存分頁,可以配合PageHelper實現物理分頁

接口:SelectRowBoundsMapper 
方法:List selectByRowBounds(T record, RowBounds rowBounds);

說明:根據實體屬性和RowBounds進行分頁查詢

接口:SelectByExampleRowBoundsMapper 
方法:List selectByExampleAndRowBounds(Object example, RowBounds rowBounds);

說明:根據example條件和RowBounds進行分頁查詢

接口:SelectByConditionRowBoundsMapper 
方法:List selectByConditionAndRowBounds(Object condition, RowBounds rowBounds);

說明:根據example條件和RowBounds進行分頁查詢,該方法和selectByExampleAndRowBounds完全一樣,只是名字改成了Condition

RowBounds組合接口

接口:RowBoundsMapper 
方法:包含上面RowBounds中的前兩個方法,不包含selectByConditionAndRowBounds

Special特殊接口

這些接口針對部分數據庫設計,不是所有數據庫都支持

接口:InsertListMapper 
方法:int insertList(List recordList);

說明:批量插入,支持批量插入的數據庫可以使用,例如MySQL,H2等,另外該接口限制實體包含id屬性並且必須爲自增列

接口:InsertUseGeneratedKeysMapper 
方法:int insertUseGeneratedKeys(T record);

說明:插入數據,限制爲實體包含id屬性並且必須爲自增列,實體配置的主鍵策略無效

MySQL專用

接口:MySqlMapper 
繼承方法:int insertList(List recordList); 
繼承方法:int insertUseGeneratedKeys(T record);

說明:該接口不包含方法,繼承了special中的InsertListMapper和InsertUseGeneratedKeysMapper

SQLServer專用

由於sqlserver中插入自增主鍵時,不能使用null插入,不能在insert語句中出現id。 
注意SqlServer的兩個特有插入方法都使用了 
@Options(useGeneratedKeys = true, keyProperty = “id”) 
這就要求表的主鍵爲id,且爲自增,如果主鍵不叫id可以看高級教程中的解決方法。 
另外這倆方法和base中的插入方法重名,不能同時存在! 
如果某種數據庫和SqlServer這裏類似,也可以使用這些接口(需要先測試)。 
經測試,PostgreSQL可以採用。

接口:InsertMapper 
方法:int insert(T record);

說明:插入數據庫,null值也會插入,不會使用列的默認值

接口:InsertSelectiveMapper 
方法:int insertSelective(T record);

說明:插入數據庫,null的屬性不會保存,會使用數據庫默認值

接口:SqlServerMapper

說明:這是上面兩個接口的組合接口。

Mapper接口

接口:Mapper 
該接口兼容Mapper2.x版本,繼承了BaseMapper, ExampleMapper, RowBoundsMapper三個組合接口。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章