diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java index f8dc8d67..b5c148db 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java @@ -194,6 +194,19 @@ public interface BaseMapper { // === 删(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 { // === 查(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); + } + /** * 根据主键查询数据。 * diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/service/IService.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/service/IService.java index 7300ee30..4ae5d20e 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/service/IService.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/service/IService.java @@ -151,6 +151,16 @@ public interface IService { return remove(query().where(condition)); } + /** + *

根据实体主键删除数据。 + * + * @param entity 实体类对象 + * @return {@code true} 删除成功,{@code false} 删除失败。 + */ + default boolean removeById(T entity) { + return SqlUtil.toBool(getMapper().delete(entity)); + } + /** *

根据数据主键删除数据。 * @@ -284,6 +294,26 @@ public interface IService { return getMapper().selectOneById(id); } + /** + *

根据实体主键查询数据。 + * + * @param entity 实体对象,必须包含有主键 + * @return 查询结果数据 + */ + default T getOneByEntity(T entity) { + return getMapper().selectOneByEntity(entity); + } + + /** + *

根据实体主键查询数据。 + * + * @param entity 实体对象,必须包含有主键 + * @return 查询结果数据 + * @apiNote 该方法会将查询结果封装为 {@link Optional} 类进行返回,方便链式操作。 + */ + default Optional getByEntityOpt(T entity) { + return Optional.ofNullable(getOneByEntity(entity)); + } /** *

根据数据主键查询一条数据。 *