From fd59471fdd49c30d02f9149a72ea62ee3debb7ab Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Mon, 1 May 2023 14:43:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E9=A1=B6=E7=BA=A7?= =?UTF-8?q?=E5=A2=9E=E5=BC=BA=20Service=20=E7=B1=BB=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisflex/spring/service/IService.java | 384 ++++++++++++++++++ 1 file changed, 384 insertions(+) create mode 100644 mybatis-flex-spring/src/main/java/com/mybatisflex/spring/service/IService.java diff --git a/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/service/IService.java b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/service/IService.java new file mode 100644 index 00000000..154dd186 --- /dev/null +++ b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/service/IService.java @@ -0,0 +1,384 @@ +/** + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mybatisflex.spring.service; + +import com.mybatisflex.core.BaseMapper; +import com.mybatisflex.core.paginate.Page; +import com.mybatisflex.core.query.QueryCondition; +import com.mybatisflex.core.query.QueryWrapper; +import com.mybatisflex.core.util.CollectionUtil; +import org.springframework.transaction.annotation.Transactional; + +import java.io.Serializable; +import java.util.*; + +import static com.mybatisflex.core.util.SqlUtil.retBool; + +/** + * 由 Mybatis-Flex 提供的顶级增强 Service 接口。 + * + * @author 王帅 + * @since 2023-05-01 + * @param 实体类(Entity)类型 + */ +@SuppressWarnings("unused") +public interface IService { + + /** + * 获取对应实体类(Entity)的基础映射类(BaseMapper)。 + * + * @return 基础映射类(BaseMapper) + */ + BaseMapper getBaseMapper(); + + // ===== 保存(增)操作 ===== + + /** + * 保存实体类对象数据。 + * + * @param entity 实体类对象 + * @return {@code true} 保存成功,{@code false} 保存失败。 + * @apiNote 默认调用的是 {@link BaseMapper#insertSelective(Object)} 方法,忽略 + * {@code null} 字段的数据,使数据库配置的默认值生效。 + */ + default boolean save(T entity) { + return retBool(getBaseMapper().insertSelective(entity)); + } + + /** + * 保存或者更新实体类对象数据。 + * + * @param entity 实体类对象 + * @return {@code true} 保存或更新成功,{@code false} 保存或更新失败。 + * @apiNote 如果实体类对象主键有值,则更新数据,若没有值,则保存数据。 + */ + default boolean saveOrUpdate(T entity) { + return retBool(getBaseMapper().insertOrUpdate(entity)); + } + + /** + * 批量保存实体类对象数据。 + * + * @param entities 实体类对象 + * @return {@code true} 保存成功,{@code false} 保存失败。 + */ + @Transactional(rollbackFor = Exception.class) + default boolean saveBatch(Collection entities) { + return retBool(getBaseMapper().insertBatch(new ArrayList<>(entities))); + } + + /** + * 批量保存实体类对象数据。 + * + * @param entities 实体类对象 + * @param size 每次保存切分的数量 + * @return {@code true} 保存成功,{@code false} 保存失败。 + */ + @Transactional(rollbackFor = Exception.class) + default boolean saveBatch(Collection entities, int size) { + return retBool(getBaseMapper().insertBatch(new ArrayList<>(entities), size)); + } + + // ===== 删除(删)操作 ===== + + /** + * 根据查询条件删除数据。 + * + * @param query 查询条件 + * @return {@code true} 删除成功,{@code false} 删除失败。 + */ + default boolean remove(QueryWrapper query) { + return retBool(getBaseMapper().deleteByQuery(query)); + } + + /** + * 根据查询条件删除数据。 + * + * @param query 查询条件 + * @return {@code true} 删除成功,{@code false} 删除失败。 + */ + default boolean remove(QueryCondition query) { + return retBool(getBaseMapper().deleteByCondition(query)); + } + + /** + * 根据数据主键删除数据。 + * + * @param id 数据主键 + * @return {@code true} 删除成功,{@code false} 删除失败。 + */ + default boolean removeById(Serializable id) { + return retBool(getBaseMapper().deleteById(id)); + } + + /** + * 根据数据主键批量删除数据。 + * + * @param ids 数据主键 + * @return {@code true} 删除成功,{@code false} 删除失败。 + */ + @Transactional(rollbackFor = Exception.class) + default boolean removeByIds(Collection ids) { + if (CollectionUtil.isEmpty(ids)) { + return false; + } + return retBool(getBaseMapper().deleteBatchByIds(ids)); + } + + /** + * 根据 {@link Map} 构建查询条件删除数据。 + * + * @param query 查询条件 + * @return {@code true} 删除成功,{@code false} 删除失败。 + */ + default boolean removeByMap(Map query) { + return retBool(getBaseMapper().deleteByMap(query)); + } + + // ===== 更新(改)操作 ===== + + /** + * 根据查询条件更新数据。 + * + * @param entity 实体类对象 + * @param query 查询条件 + * @return {@code true} 更新成功,{@code false} 更新失败。 + */ + default boolean update(T entity, QueryWrapper query) { + return retBool(getBaseMapper().updateByQuery(entity, query)); + } + + /** + * 根据查询条件更新数据。 + * + * @param entity 实体类对象 + * @param query 查询条件 + * @return {@code true} 更新成功,{@code false} 更新失败。 + */ + default boolean update(T entity, QueryCondition query) { + return retBool(getBaseMapper().updateByCondition(entity, query)); + } + + /** + * 根据数据主键更新数据。 + * + * @param entity 实体类对象 + * @return {@code true} 更新成功,{@code false} 更新失败。 + */ + default boolean updateById(T entity) { + return retBool(getBaseMapper().update(entity)); + } + + /** + * 根据 {@link Map} 构建查询条件更新数据。 + * + * @param entity 实体类对象 + * @param query 查询条件 + * @return {@code true} 更新成功,{@code false} 更新失败。 + */ + default boolean updateByMap(T entity, Map query) { + return retBool(getBaseMapper().updateByMap(entity, query)); + } + + // ===== 查询(查)操作 ===== + + /** + * 根据数据主键查询一条数据。 + * + * @param id 数据主键 + * @return 查询结果数据 + */ + default T getById(Serializable id) { + return getBaseMapper().selectOneById(id); + } + + /** + * 根据数据主键查询一条数据。 + * + * @param id 数据主键 + * @return 查询结果数据 + * @apiNote 该方法会将查询结果封装为 {@link Optional} 类进行返回,方便链式操作。 + */ + default Optional getByIdOpt(Serializable id) { + return Optional.ofNullable(getById(id)); + } + + /** + * 根据查询条件查询一条数据。 + * + * @param query 查询条件 + * @return 查询结果数据 + */ + default T getOne(QueryWrapper query) { + return getBaseMapper().selectOneByQuery(query); + } + + /** + * 根据查询条件查询一条数据。 + * + * @param query 查询条件 + * @return 查询结果数据 + * @apiNote 该方法会将查询结果封装为 {@link Optional} 类进行返回,方便链式操作。 + */ + default Optional getOneOpt(QueryWrapper query) { + return Optional.ofNullable(getOne(query)); + } + + /** + * 根据查询条件查询一条数据。 + * + * @param query 查询条件 + * @return 查询结果数据 + */ + default T getOne(QueryCondition query) { + return getBaseMapper().selectOneByCondition(query); + } + + /** + * 根据查询条件查询一条数据。 + * + * @param query 查询条件 + * @return 查询结果数据 + * @apiNote 该方法会将查询结果封装为 {@link Optional} 类进行返回,方便链式操作。 + */ + default Optional getOneOpt(QueryCondition query) { + return Optional.ofNullable(getOne(query)); + } + + /** + * 查询所有数据。 + * + * @return 所有数据 + */ + default List list() { + return getBaseMapper().selectAll(); + } + + /** + * 根据查询条件查询数据集合。 + * + * @param query 查询条件 + * @return 数据集合 + */ + default List list(QueryWrapper query) { + return getBaseMapper().selectListByQuery(query); + } + + /** + * 根据查询条件查询数据集合。 + * + * @param query 查询条件 + * @return 数据集合 + */ + default List list(QueryCondition query) { + return getBaseMapper().selectListByCondition(query); + } + + /** + * 根据数据主键查询数据集合。 + * + * @param ids 数据主键 + * @return 数据集合 + */ + default List listByIds(Collection ids) { + return getBaseMapper().selectListByIds(ids); + } + + /** + * 根据 {@link Map} 构建查询条件查询数据集合。 + * + * @param query 查询条件 + * @return 数据集合 + */ + default List listByMap(Map query) { + return getBaseMapper().selectListByMap(query); + } + + // ===== 数量查询操作 ===== + + /** + * 查询所有数据数量。 + * + * @return 所有数据数量 + */ + default Long count() { + return getBaseMapper().selectCountByQuery(query()); + } + + /** + * 根据查询条件查询数据数量。 + * + * @param query 查询条件 + * @return 数据数量 + */ + default Long count(QueryWrapper query) { + return getBaseMapper().selectCountByQuery(query); + } + + /** + * 根据查询条件查询数据数量。 + * + * @param query 查询条件 + * @return 数据数量 + */ + default Long count(QueryCondition query) { + return getBaseMapper().selectCountByCondition(query); + } + + // ===== 分页查询操作 ===== + + /** + * 分页查询所有数据。 + * + * @param page 分页对象 + * @return 分页对象 + */ + default Page page(Page page) { + return getBaseMapper().paginate(page, query()); + } + + /** + * 根据查询条件分页查询数据。 + * + * @param page 分页对象 + * @param query 查询条件 + * @return 分页对象 + */ + default Page page(Page page, QueryWrapper query) { + return getBaseMapper().paginate(page, query); + } + + /** + * 根据查询条件分页查询数据。 + * + * @param page 分页对象 + * @param query 查询条件 + * @return 分页对象 + */ + default Page page(Page page, QueryCondition query) { + return getBaseMapper().paginate(page, query().where(query)); + } + + /** + * 查询包装器。 + * + * @return 查询包装器 + */ + default QueryWrapper query() { + return new QueryWrapper(); + } + +} \ No newline at end of file