From 150416a7c348276820216ed7ea5af696125a77ec Mon Sep 17 00:00:00 2001 From: "Watcher.Wang" Date: Sun, 8 Oct 2023 17:26:03 +0800 Subject: [PATCH] =?UTF-8?q?Mapper=E5=A2=9E=E5=8A=A0Entity=E4=BD=9C?= =?UTF-8?q?=E4=B8=BA=E5=85=A5=E5=8F=82=E8=BF=9B=E8=A1=8C=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E4=B8=8E=E5=88=A0=E9=99=A4=E7=9A=84=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E6=A0=B9=E6=8D=AEEntity=E7=9A=84=E4=B8=BB=E9=94=AE=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E6=9F=A5=E8=AF=A2=E4=B8=8E=E5=88=A0=E9=99=A4=E3=80=82?= =?UTF-8?q?=20IService=E5=A2=9E=E5=8A=A0=E5=BC=95=E7=94=A8=E4=B8=8A?= =?UTF-8?q?=E8=BF=B0=E6=96=B9=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mybatisflex/core/BaseMapper.java | 26 ++++++++++++++++ .../mybatisflex/core/service/IService.java | 30 +++++++++++++++++++ 2 files changed, 56 insertions(+) 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)); + } /** *

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