MyBatis Plus (MP for short) is a MyBatis enhancement tool designed to simplify development and improve efficiency. It provides many convenient features, one of which isIService
interface.IService
The interface is a service layer interface provided by MyBatis Plus, encapsulating commonly used CRUD operations, allowing developers to perform database operations more conveniently. This article will explain in detailIService
Interfaces include its basic concepts, common methods and practical application scenarios.
1. Basic concepts
IService
The interface is a service layer interface provided by MyBatis Plus, which defines a series of commonly used CRUD operation methods. By implementingIService
Interfaces can quickly build service layers and simplify development processes.
1.1 Interface definition
IService
The interface is defined as follows:
public interface IService<T> { // Insert a record (select field, policy insertion) boolean save(T entity); // Insert (batch) boolean saveBatch(Collection<T> entityList); // Insert (batch) boolean saveBatch(Collection<T> entityList, int batchSize); // Delete by ID boolean removeById(Serializable id); // Delete according to entity (ID) boolean removeById(T entity); // Delete the record according to the columnMap condition boolean removeByMap(Map<String, Object> columnMap); // Delete the record according to entity conditions boolean remove(Wrapper<T> queryWrapper); // Delete (batch deletion according to ID) boolean removeByIds(Collection<? extends Serializable> idList); // Select Modify according to ID boolean updateById(T entity); // Update records according to whereEntity conditions boolean update(T entity, Wrapper<T> updateWrapper); // Query by ID T getById(Serializable id); // Query (batch query based on ID) List<T> listByIds(Collection<? extends Serializable> idList); // Query (according to columnMap conditions) List<T> listByMap(Map<String, Object> columnMap); // Query a record according to entity conditions T getOne(Wrapper<T> queryWrapper); // Query all records according to Wrapper conditions List<T> list(Wrapper<T> queryWrapper); // Query all List<T> list(); // Query the total number of records int count(); // Query the total number of records int count(Wrapper<T> queryWrapper); // Query all lists List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper); // Query all lists List<Map<String, Object>> listMaps(); // Query all records List<Object> listObjs(); // Query all records <V> List<V> listObjs(Function<? super Object, V> mapper); // Query all records according to Wrapper conditions List<Object> listObjs(Wrapper<T> queryWrapper); // Query all records according to Wrapper conditions <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper); // Unconditional pagination query IPage<T> page(IPage<T> page); // Conditional pagination query IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper); // Unconditional pagination query IPage<Map<String, Object>> pageMaps(IPage<T> page); // Conditional pagination query IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper); }
1.2 Implementation Class
For useIService
Interfaces usually require creating an implementation class, inheritingServiceImpl
class and implementIService
interface.ServiceImpl
Class is a default implementation class provided by MyBatis Plus, encapsulating commonly used CRUD operations.
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IService<User> { // Custom method}
In the above code,UserServiceImpl
InheritedServiceImpl
class and implementedIService<User>
interface.ServiceImpl
The constructor of the class needs to be passed into the Mapper interface and entity class.
2. Common methods
2.1 Insert operation
- save: Insert a record (select field, policy insertion)
- saveBatch: Insert (batch)
User user = new User(); ("John"); (25); (user); List<User> userList = new ArrayList<>(); (new User("John", 25, "john@")); (new User("Jane", 30, "jane@")); (userList);
2.2 Delete operation
- removeById: Delete according to ID
- removeByMap: Delete according to columnMap conditions
- remove: Delete according to entity conditions
- removeByIds: Batch deletion according to ID
(1); Map<String, Object> columnMap = new HashMap<>(); ("age", 25); (columnMap); QueryWrapper<User> queryWrapper = new QueryWrapper<>(); ("age", 25); (queryWrapper); List<Integer> idList = (1, 2, 3); (idList);
2.3 Update operation
- updateById: Updated by ID
- update: Updated according to whereEntity conditions
User user = new User(); (1); ("John Doe"); (26); (user); User user = new User(); (1); QueryWrapper<User> updateWrapper = new QueryWrapper<>(); ("age", 25); (user, updateWrapper);
2.4 Query operation
- getById: Query based on ID
- listByIds: Batch query based on ID
- listByMap: Query according to columnMap conditions
- getOne: Query a record according to entity conditions
- list: Query all records according to Wrapper conditions
- list: Query all
- count: Query the total number of records
- page: Unconditional pagination query
- page: Conditional pagination query
User user = (1); List<Integer> idList = (1, 2, 3); List<User> userList = (idList); Map<String, Object> columnMap = new HashMap<>(); ("age", 25); List<User> userList = (columnMap); QueryWrapper<User> queryWrapper = new QueryWrapper<>(); ("age", 25); User user = (queryWrapper); QueryWrapper<User> queryWrapper = new QueryWrapper<>(); ("age", 25); List<User> userList = (queryWrapper); List<User> userList = (); int count = (); Page<User> page = new Page<>(1, 10); IPage<User> userPage = (page); Page<User> page = new Page<>(1, 10); QueryWrapper<User> queryWrapper = new QueryWrapper<>(); ("age", 25); IPage<User> userPage = (page, queryWrapper);
3. Practical application scenarios
3.1 User Management
Suppose we have a user management system that needs to add, delete, modify and check the users. passIService
Interface, these functions can be easily implemented.
@Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IService<User> { // Custom method public User getUserByName(String name) { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); ("name", name); return getOne(queryWrapper); } }
In the above code, we define a custom methodgetUserByName
, query user information based on user name.
3.2 Order Management
Suppose we have an order management system that requires adding, deleting, modifying and checking orders. passIService
Interface, these functions can be easily implemented.
@Service public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IService<Order> { // Custom method public List<Order> getOrdersByUserId(int userId) { QueryWrapper<Order> queryWrapper = new QueryWrapper<>(); ("user_id", userId); return list(queryWrapper); } }
In the above code, we define a custom methodgetOrdersByUserId
, query order information based on user ID.
4. Summary
MyBatis PlusIService
The interface provides a series of commonly used CRUD operation methods, allowing developers to perform database operations more conveniently. By implementingIService
Interface and inheritServiceImpl
class, which can quickly build a service layer and simplify the development process. In practical applications, combining custom methods can further improve development efficiency and code maintainability.
This is the article about the implementation of the IService interface of MyBatisPlus. For more information about the content of the MyBatisPlus IService interface, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!