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, andpage
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 classcom.mongoplus.toolkit.BinaryConvertHandler
with commonly used operations, such as converting aFile
toBinary
.
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
Type | Parameter | Description |
---|---|---|
T | entity | Entity object |
Collection<T> | entityList | Collection 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
Type | Parameter | Description |
---|---|---|
T | entity | Entity object |
Collection<T> | entityList | Collection of entity objects |
Boolean | isQueryDatabase | Whether 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
Type | Parameter | Description |
---|---|---|
UpdateChainWrapper<T> | updateChainWrapper | Entity wrapper for update operations |
Serializable | id | Primary key ID |
column<String, Object> | column | Table field |
Collection<? extends Serializable> | idList | List 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
Type | Parameter | Description |
---|---|---|
UpdateChainWrapper<T> | updateChainWrapper | Entity wrapper for update operations |
T | entity | Entity object |
Collection<T> | entityList | Collection 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
Type | Parameter | Description |
---|---|---|
Serializable | id | Primary key ID |
QueryChainWrapper<T> | queryChainWrapper | Entity wrapper for query operations |
List
java
// Query all
List<T> list();
// Query list with conditions
List<T> list(QueryChainWrapper<T, ?> queryChainWrapper);
Parameters
Type | Parameter | Description |
---|---|---|
QueryChainWrapper<T> | queryWrapper | Entity 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
Type | Parameter | Description |
---|---|---|
IPage<T> | page | Pagination object |
QueryChainWrapper<T> | queryWrapper | Entity wrapper for query operations |
Count
java
// Count total records
int count();
// Count with Wrapper conditions
int count(QueryChainWrapper<T, ?> queryChainWrapper);
Parameters
Type | Parameter | Description |
---|---|---|
QueryChainWrapper<T> | queryWrapper | Entity 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);