MyBatisPlus的IService接口實現(xiàn)
MyBatis Plus(簡稱MP)是一個MyBatis的增強工具,旨在簡化開發(fā)、提高效率。它提供了許多便捷的功能,其中之一就是IService接口。IService接口是MyBatis Plus提供的一個服務(wù)層接口,封裝了常用的CRUD操作,使得開發(fā)者可以更加便捷地進行數(shù)據(jù)庫操作。本文將詳細講解IService接口,包括其基本概念、常用方法以及實際應(yīng)用場景。
1. 基本概念
IService接口是MyBatis Plus提供的一個服務(wù)層接口,定義了一系列常用的CRUD操作方法。通過實現(xiàn)IService接口,可以快速構(gòu)建服務(wù)層,簡化開發(fā)流程。
1.1 接口定義
IService接口定義如下:
public interface IService<T> {
// 插入一條記錄(選擇字段,策略插入)
boolean save(T entity);
// 插入(批量)
boolean saveBatch(Collection<T> entityList);
// 插入(批量)
boolean saveBatch(Collection<T> entityList, int batchSize);
// 根據(jù) ID 刪除
boolean removeById(Serializable id);
// 根據(jù)實體(ID)刪除
boolean removeById(T entity);
// 根據(jù) columnMap 條件,刪除記錄
boolean removeByMap(Map<String, Object> columnMap);
// 根據(jù) entity 條件,刪除記錄
boolean remove(Wrapper<T> queryWrapper);
// 刪除(根據(jù)ID 批量刪除)
boolean removeByIds(Collection<? extends Serializable> idList);
// 根據(jù) ID 選擇修改
boolean updateById(T entity);
// 根據(jù) whereEntity 條件,更新記錄
boolean update(T entity, Wrapper<T> updateWrapper);
// 根據(jù) ID 查詢
T getById(Serializable id);
// 查詢(根據(jù)ID 批量查詢)
List<T> listByIds(Collection<? extends Serializable> idList);
// 查詢(根據(jù) columnMap 條件)
List<T> listByMap(Map<String, Object> columnMap);
// 根據(jù) entity 條件,查詢一條記錄
T getOne(Wrapper<T> queryWrapper);
// 根據(jù) Wrapper 條件,查詢?nèi)坑涗?
List<T> list(Wrapper<T> queryWrapper);
// 查詢所有
List<T> list();
// 查詢總記錄數(shù)
int count();
// 查詢總記錄數(shù)
int count(Wrapper<T> queryWrapper);
// 查詢所有列表
List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);
// 查詢所有列表
List<Map<String, Object>> listMaps();
// 查詢?nèi)坑涗?
List<Object> listObjs();
// 查詢?nèi)坑涗?
<V> List<V> listObjs(Function<? super Object, V> mapper);
// 根據(jù) Wrapper 條件,查詢?nèi)坑涗?
List<Object> listObjs(Wrapper<T> queryWrapper);
// 根據(jù) Wrapper 條件,查詢?nèi)坑涗?
<V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
// 無條件分頁查詢
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);
}
1.2 實現(xiàn)類
為了使用IService接口,通常需要創(chuàng)建一個實現(xiàn)類,繼承ServiceImpl類,并實現(xiàn)IService接口。ServiceImpl類是MyBatis Plus提供的一個默認實現(xiàn)類,封裝了常用的CRUD操作。
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IService<User> {
// 自定義方法
}
在上述代碼中,UserServiceImpl繼承了ServiceImpl類,并實現(xiàn)了IService<User>接口。ServiceImpl類的構(gòu)造函數(shù)需要傳入Mapper接口和實體類。
2. 常用方法
2.1 插入操作
- save:插入一條記錄(選擇字段,策略插入)
- saveBatch:插入(批量)
User user = new User();
user.setName("John");
user.setAge(25);
userService.save(user);
List<User> userList = new ArrayList<>();
userList.add(new User("John", 25, "john@example.com"));
userList.add(new User("Jane", 30, "jane@example.com"));
userService.saveBatch(userList);
2.2 刪除操作
- removeById:根據(jù)ID刪除
- removeByMap:根據(jù)columnMap條件刪除
- remove:根據(jù)entity條件刪除
- removeByIds:根據(jù)ID批量刪除
userService.removeById(1);
Map<String, Object> columnMap = new HashMap<>();
columnMap.put("age", 25);
userService.removeByMap(columnMap);
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("age", 25);
userService.remove(queryWrapper);
List<Integer> idList = Arrays.asList(1, 2, 3);
userService.removeByIds(idList);
2.3 更新操作
- updateById:根據(jù)ID更新
- update:根據(jù)whereEntity條件更新
User user = new User();
user.setId(1);
user.setName("John Doe");
user.setAge(26);
userService.updateById(user);
User user = new User();
user.setStatus(1);
QueryWrapper<User> updateWrapper = new QueryWrapper<>();
updateWrapper.eq("age", 25);
userService.update(user, updateWrapper);
2.4 查詢操作
- getById:根據(jù)ID查詢
- listByIds:根據(jù)ID批量查詢
- listByMap:根據(jù)columnMap條件查詢
- getOne:根據(jù)entity條件查詢一條記錄
- list:根據(jù)Wrapper條件查詢?nèi)坑涗?/li>
- list:查詢所有
- count:查詢總記錄數(shù)
- page:無條件分頁查詢
- page:條件分頁查詢
User user = userService.getById(1);
List<Integer> idList = Arrays.asList(1, 2, 3);
List<User> userList = userService.listByIds(idList);
Map<String, Object> columnMap = new HashMap<>();
columnMap.put("age", 25);
List<User> userList = userService.listByMap(columnMap);
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("age", 25);
User user = userService.getOne(queryWrapper);
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("age", 25);
List<User> userList = userService.list(queryWrapper);
List<User> userList = userService.list();
int count = userService.count();
Page<User> page = new Page<>(1, 10);
IPage<User> userPage = userService.page(page);
Page<User> page = new Page<>(1, 10);
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("age", 25);
IPage<User> userPage = userService.page(page, queryWrapper);
3. 實際應(yīng)用場景
3.1 用戶管理
假設(shè)我們有一個用戶管理系統(tǒng),需要對用戶進行增刪改查操作。通過IService接口,可以輕松實現(xiàn)這些功能。
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IService<User> {
// 自定義方法
public User getUserByName(String name) {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", name);
return getOne(queryWrapper);
}
}
在上述代碼中,我們定義了一個自定義方法getUserByName,根據(jù)用戶名查詢用戶信息。
3.2 訂單管理
假設(shè)我們有一個訂單管理系統(tǒng),需要對訂單進行增刪改查操作。通過IService接口,可以輕松實現(xiàn)這些功能。
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IService<Order> {
// 自定義方法
public List<Order> getOrdersByUserId(int userId) {
QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", userId);
return list(queryWrapper);
}
}
在上述代碼中,我們定義了一個自定義方法getOrdersByUserId,根據(jù)用戶ID查詢訂單信息。
4. 總結(jié)
MyBatis Plus的IService接口提供了一系列常用的CRUD操作方法,使得開發(fā)者可以更加便捷地進行數(shù)據(jù)庫操作。通過實現(xiàn)IService接口,并繼承ServiceImpl類,可以快速構(gòu)建服務(wù)層,簡化開發(fā)流程。在實際應(yīng)用中,結(jié)合自定義方法,可以進一步提高開發(fā)效率和代碼可維護性。
到此這篇關(guān)于MyBatisPlus的IService接口實現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatisPlus IService接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java hibernate使用注解來定義聯(lián)合主鍵
這篇文章主要介紹了java hibernate使用注解來定義聯(lián)合主鍵的相關(guān)資料,需要的朋友可以參考下2017-01-01
使用hibernate和struts2實現(xiàn)分頁功能的示例
本篇文章主要介紹了使用hibernate和struts2實現(xiàn)分頁功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
使用Java實現(xiàn)百萬Excel數(shù)據(jù)導(dǎo)出
這篇文章主要為大家詳細介紹了如何使用Java實現(xiàn)百萬Excel數(shù)據(jù)導(dǎo)出,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考一下2024-03-03
springboot如何獲取applicationContext?servletContext
這篇文章主要介紹了springboot如何獲取applicationContext?servletContext問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
JAVA?從完整的文件路徑中分別截取文件名和文件路徑的實現(xiàn)
在Java編程中,經(jīng)常會遇到需要截取文件名的場景,本文主要介紹了JAVA?從完整的文件路徑中分別截取文件名和文件路徑的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-04-04
Spring Gateway自定義請求參數(shù)封裝的實現(xiàn)示例
這篇文章主要介紹了Spring Gateway自定義請求參數(shù)封裝的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

