1. 程式人生 > >MyBatis Mapper3介面大全

MyBatis Mapper3介面大全

Mapper3介面有兩種形式,一種是提供了一個方法的介面。還有一種是不提供方法,但是繼承了多個單方法的介面,一般是某類方法的集合。

例如SelectMapper<T>是一個單方法的介面,BaseSelectMapper<T>是一個繼承了4個基礎查詢方法的介面。

基礎介面 Select

介面:SelectMapper<T>
方法:List<T> select(T record);
說明:根據實體中的屬性值進行查詢,查詢條件使用等號

介面:SelectByPrimaryKeyMapper<T>
方法:T selectByPrimaryKey(Object key);
說明:根據主鍵欄位進行查詢,方法引數必須包含完整的主鍵屬性,查詢條件使用等號

介面:SelectAllMapper<T>
方法:List<T> selectAll();
說明:查詢全部結果,select(null)方法能達到同樣的效果

介面:SelectOneMapper<T>
方法:T selectOne(T record);
說明:根據實體中的屬性進行查詢,只能有一個返回值,有多個結果是丟擲異常,查詢條件使用等號

介面:SelectCountMapper<T>
方法:int selectCount(T record);
說明:根據實體中的屬性查詢總數,查詢條件使用等號

基礎介面Insert

介面:InsertMapper<T>
方法:int insert(T record);
說明:儲存一個實體,null的屬性也會儲存,不會使用資料庫預設值

介面:InsertSelectiveMapper<T>
方法:int insertSelective(T record);
說明:儲存一個實體,null的屬性不會儲存,會使用資料庫預設值

基礎介面Update

介面:UpdateByPrimaryKeyMapper<T>
方法:int updateByPrimaryKey(T record);
說明:根據主鍵更新實體全部欄位,null值會被更新

介面:UpdateByPrimaryKeySelectiveMapper<T>
方法:int updateByPrimaryKeySelective(T record);
說明:根據主鍵更新屬性不為null的值

基礎介面Delete

介面:DeleteMapper<T>
方法:int delete(T record);
說明:根據實體屬性作為條件進行刪除,查詢條件使用等號
介面:DeleteByPrimaryKeyMapper<T>
方法:int deleteByPrimaryKey(Object key);
說明:根據主鍵欄位進行刪除,方法引數必須包含完整的主鍵屬性

Base組合介面

介面:BaseSelectMapper<T>
方法:包含上面Select的4個方法
介面:BaseInsertMapper<T>
方法:包含上面Insert的2個方法

介面:BaseUpdateMapper<T>
方法:包含上面Update的2個方法

介面:BaseDeleteMapper<T>
方法:包含上面Delete的2個方法

CURD組合介面

介面:BaseMapper<T>
方法:繼承了base組合介面中的4個組合介面,包含完整的CRUD方法

Example方法

介面:SelectByExampleMapper<T>
方法:List<T> selectByExample(Object example);
說明:根據Example條件進行查詢
重點:這個查詢支援通過Example類指定查詢列,通過selectProperties方法指定查詢列

介面:SelectCountByExampleMapper<T>
方法:int selectCountByExample(Object example);
說明:根據Example條件進行查詢總數
介面:UpdateByExampleMapper<T>
方法:int updateByExample(@Param("record") T record, @Param("example") Object example);
說明:根據Example條件更新實體record包含的全部屬性,null值會被更新
介面:UpdateByExampleSelectiveMapper<T>
方法:int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);
說明:根據Example條件更新實體record包含的不是null的屬性值
介面:DeleteByExampleMapper<T>
方法:int deleteByExample(Object example);
說明:根據Example條件刪除資料

Example組合介面

介面:ExampleMapper<T>
方法:包含上面Example中的5個方法

Condition方法

Condition方法和Example方法作用完全一樣,只是為了避免Example帶來的歧義,提供的的Condition方法
介面:SelectByConditionMapper<T>
方法:List<T> selectByCondition(Object condition);
說明:根據Condition條件進行查詢
介面:SelectCountByConditionMapper<T>
方法:int selectCountByCondition(Object condition);
說明:根據Condition條件進行查詢總數
介面:UpdateByConditionMapper<T>
方法:int updateByCondition(@Param("record") T record, @Param("example") Object condition);
說明:根據Condition條件更新實體record包含的全部屬性,null值會被更新
介面:UpdateByConditionSelectiveMapper<T>
方法:int updateByConditionSelective(@Param("record") T record, @Param("example") Object condition);
說明:根據Condition條件更新實體record包含的不是null的屬性值
介面:DeleteByConditionMapper<T>
方法:int deleteByCondition(Object condition);
說明:根據Condition條件刪除資料

Condition組合介面

介面:ConditionMapper<T>
方法:包含上面Condition中的5個方法

RowBounds

預設為記憶體分頁,可以配合PageHelper實現物理分頁
介面:SelectRowBoundsMapper<T>
方法:List<T> selectByRowBounds(T record, RowBounds rowBounds);
說明:根據實體屬性和RowBounds進行分頁查詢
介面:SelectByExampleRowBoundsMapper<T>
方法:List<T> selectByExampleAndRowBounds(Object example, RowBounds rowBounds);
說明:根據example條件和RowBounds進行分頁查詢
介面:SelectByConditionRowBoundsMapper<T>
方法:List<T> selectByConditionAndRowBounds(Object condition, RowBounds rowBounds);
說明:根據example條件和RowBounds進行分頁查詢,該方法和selectByExampleAndRowBounds完全一樣,只是名字改成了Condition

RowBounds組合介面

介面:RowBoundsMapper<T>
方法:包含上面RowBounds中的前兩個方法,不包含selectByConditionAndRowBounds

Special特殊介面

這些介面針對部分資料庫設計,不是所有資料庫都支援
介面:InsertListMapper<T>
方法:int insertList(List<T> recordList);
說明:批量插入,支援批量插入的資料庫可以使用,例如MySQL,H2等,另外該介面限制實體包含id屬性並且必須為自增列
介面:InsertUseGeneratedKeysMapper<T>
方法:int insertUseGeneratedKeys(T record);
說明:插入資料,限制為實體包含id屬性並且必須為自增列,實體配置的主鍵策略無效

MySQL專用

介面:MySqlMapper<T>
繼承方法:int insertList(List<T> recordList);
繼承方法:int insertUseGeneratedKeys(T record);
說明:該介面不包含方法,繼承了special中的InsertListMapper<T>和InsertUseGeneratedKeysMapper<T>

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<T>
該介面相容Mapper2.x版本,繼承了BaseMapper<T>, ExampleMapper<T>, RowBoundsMapper<T>三個組合介面。