!356 Mapper支持Entity作为入参进行查询与删除

Merge pull request !356 from 王超/main
This commit is contained in:
Michael Yang 2023-10-08 10:38:53 +00:00 committed by Gitee
commit c5fb1fbea8
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 56 additions and 0 deletions

View File

@ -194,6 +194,19 @@ public interface BaseMapper<T> {
// === delete ===
/**
* 根据实体主键来删除数据
*
* @param entity 实体对象必须包含有主键
* @return 受影响的行数
*/
default int delete( T entity){
FlexAssert.notNull(entity, "entity can not be null");
TableInfo tableInfo = TableInfoFactory.ofEntityClass(entity.getClass());
Object[] pkArgs = tableInfo.buildPkSqlArgs(entity);
return deleteById(pkArgs);
}
/**
* 根据主键删除数据如果是多个主键的情况下需要传入数组例如{@code new Integer[]{100,101}}
*
@ -368,6 +381,19 @@ public interface BaseMapper<T> {
// === select ===
/**
* 根据实体主键查询数据
*
* @param entity 实体对象必须包含有主键
* @return 实体类数据
*/
default T selectOneByEntity(T entity){
FlexAssert.notNull(entity, "entity can not be null");
TableInfo tableInfo = TableInfoFactory.ofEntityClass(entity.getClass());
Object[] pkArgs = tableInfo.buildPkSqlArgs(entity);
return selectOneById(pkArgs);
}
/**
* 根据主键查询数据
*

View File

@ -151,6 +151,16 @@ public interface IService<T> {
return remove(query().where(condition));
}
/**
* <p>根据实体主键删除数据
*
* @param entity 实体类对象
* @return {@code true} 删除成功{@code false} 删除失败
*/
default boolean removeById(T entity) {
return SqlUtil.toBool(getMapper().delete(entity));
}
/**
* <p>根据数据主键删除数据
*
@ -284,6 +294,26 @@ public interface IService<T> {
return getMapper().selectOneById(id);
}
/**
* <p>根据实体主键查询数据
*
* @param entity 实体对象必须包含有主键
* @return 查询结果数据
*/
default T getOneByEntity(T entity) {
return getMapper().selectOneByEntity(entity);
}
/**
* <p>根据实体主键查询数据
*
* @param entity 实体对象必须包含有主键
* @return 查询结果数据
* @apiNote 该方法会将查询结果封装为 {@link Optional} 类进行返回方便链式操作
*/
default Optional<T> getByEntityOpt(T entity) {
return Optional.ofNullable(getOneByEntity(entity));
}
/**
* <p>根据数据主键查询一条数据
*