MybatisPlus IService接口功能介紹
一、介紹
在MybatisPlus框架中,IService接口扮演著重要的角色。
作為一個通用的服務接口,IService定義了一系列方法,包括查詢、插入、更新、刪除等。
這些方法的定義使得在服務層進行數(shù)據(jù)庫操作變得更為便捷和高效。
- IService 接口是一個泛型接口,定義了一組通用的基礎方法,包括常見的增刪改查操作。
- 例如,它提供了插入數(shù)據(jù)、根據(jù)主鍵更新數(shù)據(jù)、根據(jù)主鍵刪除數(shù)據(jù)、根據(jù)主鍵查詢數(shù)據(jù)等方法的簽名。
- 用戶可以根據(jù)自己的需求和業(yè)務邏輯在自定義的服務接口中繼承 IService 接口,并實現(xiàn)其中的方法。
用法:
public interface UserService extends IService<User> {}- ServiceImpl 類是 IService 接口的默認實現(xiàn)類,提供了基本的增刪改查操作的實現(xiàn)細節(jié)。
- 它使用了泛型參數(shù)來規(guī)范實體類和主鍵類型,并實現(xiàn)了 IService 接口中定義的方法。
- 用戶可以繼承 ServiceImpl 類,并在自己的實現(xiàn)類中添加或重寫更具體的業(yè)務邏輯。
用法:
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {}二、IService用法
1、添加數(shù)據(jù)
// 插入一條記錄(選擇字段,策略插入) boolean save(T entity); // 插入(批量) boolean saveBatch(Collection<T> entityList); // 插入(批量,限制數(shù)量) boolean saveBatch(Collection<T> entityList, int batchSize); // TableId 注解存在更新記錄,否則插入一條記錄 boolean saveOrUpdate(T entity); // 根據(jù) updateWrapper 嘗試更新,否則繼續(xù)執(zhí)行 saveOrUpdate(T) 方法 boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper); // 批量修改插入 boolean saveOrUpdateBatch(Collection<T> entityList); // 批量修改插入 boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
可以開啟 rewriteBatchedStatements=true 參數(shù),提高批處理的執(zhí)行效率。
2、刪除數(shù)據(jù)
// 根據(jù) entity 條件,刪除記錄 boolean remove(Wrapper<T> queryWrapper); // 根據(jù) ID 刪除 boolean removeById(Serializable id); // 根據(jù) columnMap 條件,刪除記錄 boolean removeByMap(Map<String, Object> columnMap); // 刪除(根據(jù)ID 批量刪除) boolean removeByIds(Collection<? extends Serializable> idList);
3、修改數(shù)據(jù)
// 根據(jù) UpdateWrapper 條件更新記錄,需要設置sqlset boolean update(Wrapper<T> updateWrapper); // 根據(jù) whereWrapper 條件,更新記錄 boolean update(T updateEntity, Wrapper<T> whereWrapper); // 根據(jù) ID 選擇修改 boolean updateById(T entity); // 根據(jù)ID 批量更新 boolean updateBatchById(Collection<T> entityList); // 根據(jù)ID 批量更新 boolean updateBatchById(Collection<T> entityList, int batchSize);
4、查詢數(shù)據(jù)
查詢一條數(shù)據(jù)
// 根據(jù) ID 查詢
T getById(Serializable id);
// 根據(jù) Wrapper,查詢一條記錄。結果集如果是多個會拋出異常,隨機取一條加上限制條件 wrapper.last("LIMIT 1")
T getOne(Wrapper<T> queryWrapper);
// 根據(jù) Wrapper,查詢一條記錄,這個是方法返回結果不止一條則會拋出異常,如果想默認取第一條結果,可以給這方法傳第二個參數(shù)為false。
T getOne(Wrapper<T> queryWrapper, boolean throwEx);
// 根據(jù) Wrapper,查詢一條記錄
Map<String, Object> getMap(Wrapper<T> queryWrapper);
// 根據(jù) Wrapper,查詢一條記錄
// mapper:轉換函數(shù),用于將查詢結果中的每個對象轉換為指定的對象類型。
<V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);查詢多條數(shù)據(jù)
// 查詢所有 List<T> list(); // 查詢列表 List<T> list(Wrapper<T> queryWrapper); // 查詢(根據(jù) ID 批量查詢) Collection<T> listByIds(Collection<? extends Serializable> idList); // 查詢(根據(jù) columnMap 條件) Collection<T> listByMap(Map<String, Object> columnMap); // 查詢所有列表 List<Map<String, Object>> listMaps(); // 查詢列表 List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper); // 查詢全部記錄 List<Object> listObjs(); // 查詢全部記錄 <V> List<V> listObjs(Function<? super Object, V> mapper); // 根據(jù) Wrapper 條件,查詢全部記錄 List<Object> listObjs(Wrapper<T> queryWrapper); // 根據(jù) Wrapper 條件,查詢全部記錄 // mapper:轉換函數(shù),用于將查詢結果中的每個對象轉換為指定的對象類型。 <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
查詢記錄數(shù) count()
// 查詢總記錄數(shù) int count(); // 根據(jù) Wrapper 條件,查詢總記錄數(shù) int count(Wrapper<T> queryWrapper);
分頁:Page
// 無條件分頁查詢 IPage<T> page(IPage<T> page); // 條件分頁查詢 IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper); // 無條件分頁查詢 IPage<Map<String, Object>> pageMaps(IPage<T> page); // 條件分頁查詢 IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);
IPage 是MyBatis-Plus 提供的一個分頁相關的接口,它有一個實現(xiàn)類為 Page,類中定義了分頁相關的多個參數(shù)。
- size (每頁顯示條數(shù)):此參數(shù)通常由前端傳給我們,然后再封裝到Page對象中進行分頁查詢;
- current (要展示哪一頁數(shù)據(jù)):此參數(shù)通常由前端傳給我們,然后再封裝到Page對象中進行分頁查詢;
- orders(排序規(guī)則集合):按照哪些字段進行排序?可以為多個,例如希望通過時間進行排序,如果時間相同就根據(jù)用戶ID降序排序,可以添加多個字段;
- total (總記錄數(shù)):指查詢完畢后返回的數(shù)據(jù)庫中總記錄數(shù),注意不包含已被邏輯刪除的數(shù)據(jù);
- records(查詢到的分頁結果集數(shù)據(jù)):分頁查詢得到的多條數(shù)據(jù)會存儲在 records 中,可以看出該對象是一個集合,可以傳遞一個泛型,泛型就是查詢到的數(shù)據(jù)對應的實體泛型;
public class Page<T> implements IPage<T> {
private static final long serialVersionUID = 8545996863226528798L;
protected List<T> records;
protected long total;
protected long size;
protected long current;
protected List<OrderItem> orders;
protected boolean optimizeCountSql;
protected boolean searchCount;
protected boolean optimizeJoinOfCountSql;
protected String countId;
protected Long maxLimit;
/* 以下省略 */
}Page對象使用演示:
@SpringBootTest
public class ProductMapperTest {
// 自動注入 productMapper 接口對應的實現(xiàn)類對象
@Autowired
private ProductMapper productMapper;
@Test
public void testPageQuery(){
// 創(chuàng)建分頁查詢相關參數(shù) page,泛型為 Product,表示查詢到的結果對應的實體類為Product
Page<Product> page = new Page<>();
// 設置分頁查詢顯示第二頁的數(shù)據(jù),實際開發(fā)過程中該參數(shù)有前端傳遞
page.setCurrent(2);
// 設置分頁查詢每頁顯示四條數(shù)據(jù),實際開發(fā)過程中該參數(shù)有前端傳遞
page.setSize(4);
// 創(chuàng)建排序字段集合,不想排序不加即可,實際開發(fā)過程中一般都會要求按照時間降序排序
List<OrderItem> orders = new ArrayList<>();
// 按照價格排序,排序方式為降序,ASC為True表示升序,false表示降序,第一個參數(shù)表示數(shù)據(jù)庫中的列名
orders.add(new OrderItem("price",false));
// 按照生產時間排序,排序方式為降序
orders.add(new OrderItem("production_date",false));
// 將排序對象集合加入分頁查詢對象Page中
page.setOrders(orders);
// 執(zhí)行分頁查詢,可以創(chuàng)建一個Page對象接受查詢結果,
// 也可以用查詢條件參數(shù)page,但其實最后結果都是同一個
page = productMapper.selectPage(page, null);
// 可以新創(chuàng)建一個Page對象,就像下面這樣
Page<Product> productPage = productMapper.selectPage(page,null);
// 輸出分頁查詢結果顯示當前的哪一頁
System.out.println(page.getCurrent());
// 輸出分頁查詢結果的總數(shù)據(jù)條數(shù)
System.out.println(page.getTotal());
// 輸出分頁查詢結果的數(shù)據(jù)集合
System.out.println(page.getRecords());
// 輸出分頁查詢結果的每頁顯示條數(shù)
System.out.println(page.getSize());
// 判斷剛才分頁查詢的兩個結果對象是否為同一個
System.out.println(page == productPage);
// 輸出第一個分頁查詢對象內存地址
System.out.println(page);
// 輸出第二個分頁查詢對象內存地址
System.out.println(productPage);
}
}鏈式
鏈式查詢演示:
// 鏈式查詢 普通
QueryChainWrapper<T> query();
// 鏈式查詢 lambda 式。注意:不支持 Kotlin
LambdaQueryChainWrapper<T> lambdaQuery();
// 示例:
// eq 指定條件
query().eq("column", value).one();
lambdaQuery().eq(Entity::getId, value).list();鏈式更新演示:
// 鏈式更改 普通
UpdateChainWrapper<T> update();
// 鏈式更改 lambda 式。注意:不支持 Kotlin
LambdaUpdateChainWrapper<T> lambdaUpdate();
// 示例:
// eq 指定條件
update().eq("column", value).remove();
lambdaUpdate().eq(Entity::getId, value).update(entity);總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
為了多次讀取ServletInputStream引發(fā)的一系列問題
這篇文章主要介紹了為了多次讀取ServletInputStream引發(fā)的一系列問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
Java中的字節(jié)流InputStream和OutputStream詳解
這篇文章主要介紹了Java中的字節(jié)流InputStream和OutputStream詳解,繼承自InputStream的流都是用于向程序中輸入數(shù)據(jù),且數(shù)據(jù)的單位為字節(jié)8bit,我們看到的具體的某一些管道,凡是以InputStream結尾的管道,都是以字節(jié)的形式向我們的程序輸入數(shù)據(jù),需要的朋友可以參考下2023-10-10

