Skip to content
广告❤️成为赞助商

CRUD Interfaces

Service CRUD Interface

Note:

  • A general Service CRUD wrapper IService interface, further encapsulates CRUD using get to query a single row, remove to delete, list to query a collection, and page for pagination.
  • The generic type T represents any entity object.
  • If there is a possibility of adding custom general Service methods, create your own IBaseService that extends the base class provided by Mongo-Plus.

Note:

  • When inserting or updating binary files, it is recommended to use org.bson.types.Binary, a native MongoDB driver class with excellent support. MongoPlus provides a utility class com.mongoplus.toolkit.BinaryConvertHandler with commonly used operations, such as converting a File to Binary.

Command

java
// Query command, input must be JSON, otherwise it cannot be parsed
List<T> queryCommand(String command)
  • Example:
java
List<User> userList = service.queryCommand("{'user_name':{'$eq':'Zhang San'}}");
userList.forEach(System.out::println);

Save

java
// Insert a single record (selective fields, strategy insert)
boolean save(T entity);
// Batch insert
boolean saveBatch(Collection<T> entityList);

Parameters

TypeParameterDescription
TentityEntity object
Collection<T>entityListCollection of entity objects

SaveOrUpdate

java
// Update record if ID annotation exists, otherwise insert
boolean saveOrUpdate(T entity);
// isQueryDatabase set to true to check the database based on ID annotation
boolean saveOrUpdate(T entity, boolean isQueryDatabase);
// Batch save or update
boolean saveOrUpdateBatch(Collection<T> entityList);
boolean saveOrUpdateBatch(Collection<T> entityList, boolean isQueryDatabase);

Parameters

TypeParameterDescription
TentityEntity object
Collection<T>entityListCollection of entity objects
BooleanisQueryDatabaseWhether to query the database

Remove

java
// Delete records based on UpdateChainWrapper conditions
boolean remove(UpdateChainWrapper<T, ?> updateChainWrapper);
// Delete by ID
boolean removeById(Serializable id);
// Delete by column map condition
boolean removeByColumn(String column, Object value);
// Batch delete by IDs
boolean removeByIds(Collection<? extends Serializable> idList);

Parameters

TypeParameterDescription
UpdateChainWrapper<T>updateChainWrapperEntity wrapper for update operations
SerializableidPrimary key ID
column<String, Object>columnTable field
Collection<? extends Serializable>idListList of primary key IDs

Update

java
// Update records based on UpdateWrapper conditions, set sqlset required
boolean update(UpdateChainWrapper<T, ?> updateChainWrapper);
// Update by ID
boolean updateById(T entity);
// Batch update by ID
boolean updateBatchById(Collection<T> entityList);

Parameters

TypeParameterDescription
UpdateChainWrapper<T>updateChainWrapperEntity wrapper for update operations
TentityEntity object
Collection<T>entityListCollection of entity objects

Get

java
// Get by ID
T getById(Serializable id);
// Get by collection of IDs
List<T> getById(Collection<Serializable> ids);
// Get one record based on QueryChainWrapper
T one(QueryChainWrapper<T, ?> queryChainWrapper);

Parameters

TypeParameterDescription
SerializableidPrimary key ID
QueryChainWrapper<T>queryChainWrapperEntity wrapper for query operations

List

java
// Query all
List<T> list();
// Query list with conditions
List<T> list(QueryChainWrapper<T, ?> queryChainWrapper);

Parameters

TypeParameterDescription
QueryChainWrapper<T>queryWrapperEntity wrapper for query operations

Page

java
// Pagination without conditions
PageResult<T> page(PageParam pageParam);
// Pagination with conditions
PageResult<T> page(QueryChainWrapper<T, ?> queryChainWrapper, PageParam pageParam);

Parameters

TypeParameterDescription
IPage<T>pagePagination object
QueryChainWrapper<T>queryWrapperEntity wrapper for query operations

Count

java
// Count total records
int count();
// Count with Wrapper conditions
int count(QueryChainWrapper<T, ?> queryChainWrapper);

Parameters

TypeParameterDescription
QueryChainWrapper<T>queryWrapperEntity wrapper for query operations

Chain

query

java
// Lambda chain query. Note: Kotlin not supported
LambdaQueryChainWrapper<T> lambdaQuery();

// Example:
lambdaQuery().eq(Entity::getId, value).list();

update

java
// Lambda chain update. Note: Kotlin not supported
LambdaUpdateChainWrapper<T> lambdaUpdate();

// Example:
lambdaUpdate().eq(Entity::getId, value).update(entity);