123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683 |
- /*
- * Copyright (c) 2011-2023, baomidou (jobob@qq.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.baomidou.mybatisplus.extension.service;
- import com.baomidou.mybatisplus.core.conditions.Wrapper;
- import com.baomidou.mybatisplus.core.mapper.BaseMapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.core.toolkit.Assert;
- import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.baomidou.mybatisplus.extension.conditions.query.ChainQuery;
- import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
- import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
- import com.baomidou.mybatisplus.extension.conditions.update.ChainUpdate;
- import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
- import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
- import com.baomidou.mybatisplus.extension.kotlin.KtQueryChainWrapper;
- import com.baomidou.mybatisplus.extension.kotlin.KtUpdateChainWrapper;
- import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
- import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
- import org.springframework.transaction.annotation.Transactional;
- import java.io.Serializable;
- import java.util.*;
- import java.util.function.Function;
- import java.util.stream.Collectors;
- /**
- * 顶级 Service
- *
- * @author hubin
- * @since 2018-06-23
- */
- public interface IService<T> {
- /**
- * 默认批次提交数量
- */
- int DEFAULT_BATCH_SIZE = 1000;
- /**
- * 插入一条记录(选择字段,策略插入)
- *
- * @param entity 实体对象
- */
- default boolean save(T entity) {
- return SqlHelper.retBool(getBaseMapper().insert(entity));
- }
- /**
- * 插入(批量)
- *
- * @param entityList 实体对象集合
- */
- @Transactional(rollbackFor = Exception.class)
- default boolean saveBatch(Collection<T> entityList) {
- return saveBatch(entityList, DEFAULT_BATCH_SIZE);
- }
- /**
- * 插入(批量)
- *
- * @param entityList 实体对象集合
- * @param batchSize 插入批次数量
- */
- boolean saveBatch(Collection<T> entityList, int batchSize);
- /**
- * 批量修改插入
- *
- * @param entityList 实体对象集合
- */
- @Transactional(rollbackFor = Exception.class)
- default boolean saveOrUpdateBatch(Collection<T> entityList) {
- return saveOrUpdateBatch(entityList, DEFAULT_BATCH_SIZE);
- }
- /**
- * 批量修改插入
- *
- * @param entityList 实体对象集合
- * @param batchSize 每次的数量
- */
- boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
- /**
- * 根据 ID 删除
- *
- * @param id 主键ID
- */
- default boolean removeById(Serializable id) {
- return SqlHelper.retBool(getBaseMapper().deleteById(id));
- }
- /**
- * 根据 ID 删除
- *
- * @param id 主键(类型必须与实体类型字段保持一致)
- * @param useFill 是否启用填充(为true的情况,会将入参转换实体进行delete删除)
- * @return 删除结果
- * @since 3.5.0
- */
- default boolean removeById(Serializable id, boolean useFill) {
- throw new UnsupportedOperationException("不支持的方法!");
- }
- /**
- * 根据实体(ID)删除
- *
- * @param entity 实体
- * @since 3.4.4
- */
- default boolean removeById(T entity) {
- return SqlHelper.retBool(getBaseMapper().deleteById(entity));
- }
- /**
- * 根据 columnMap 条件,删除记录
- *
- * @param columnMap 表字段 map 对象
- */
- default boolean removeByMap(Map<String, Object> columnMap) {
- Assert.notEmpty(columnMap, "error: columnMap must not be empty");
- return SqlHelper.retBool(getBaseMapper().deleteByMap(columnMap));
- }
- /**
- * 根据 entity 条件,删除记录
- *
- * @param queryWrapper 实体包装类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- default boolean remove(Wrapper<T> queryWrapper) {
- return SqlHelper.retBool(getBaseMapper().delete(queryWrapper));
- }
- /**
- * 删除(根据ID 批量删除)
- *
- * @param list 主键ID或实体列表
- */
- default boolean removeByIds(Collection<?> list) {
- if (CollectionUtils.isEmpty(list)) {
- return false;
- }
- return SqlHelper.retBool(getBaseMapper().deleteByIds(list));
- }
- /**
- * 批量删除
- *
- * @param list 主键ID或实体列表
- * @param useFill 是否填充(为true的情况,会将入参转换实体进行delete删除)
- * @return 删除结果
- * @since 3.5.0
- */
- @Transactional(rollbackFor = Exception.class)
- default boolean removeByIds(Collection<?> list, boolean useFill) {
- if (CollectionUtils.isEmpty(list)) {
- return false;
- }
- if (useFill) {
- return removeBatchByIds(list, true);
- }
- return SqlHelper.retBool(getBaseMapper().deleteByIds(list));
- }
- /**
- * 批量删除(jdbc批量提交)
- *
- * @param list 主键ID或实体列表(主键ID类型必须与实体类型字段保持一致)
- * @return 删除结果
- * @since 3.5.0
- */
- @Transactional(rollbackFor = Exception.class)
- default boolean removeBatchByIds(Collection<?> list) {
- return removeBatchByIds(list, DEFAULT_BATCH_SIZE);
- }
- /**
- * 批量删除(jdbc批量提交)
- *
- * @param list 主键ID或实体列表(主键ID类型必须与实体类型字段保持一致)
- * @param useFill 是否启用填充(为true的情况,会将入参转换实体进行delete删除)
- * @return 删除结果
- * @since 3.5.0
- */
- @Transactional(rollbackFor = Exception.class)
- default boolean removeBatchByIds(Collection<?> list, boolean useFill) {
- return removeBatchByIds(list, DEFAULT_BATCH_SIZE, useFill);
- }
- /**
- * 批量删除(jdbc批量提交)
- *
- * @param list 主键ID或实体列表
- * @param batchSize 批次大小
- * @return 删除结果
- * @see #removeBatchByIds(Collection, boolean)
- * @since 3.5.0
- * @deprecated 3.5.7
- */
- @Deprecated
- default boolean removeBatchByIds(Collection<?> list, int batchSize) {
- throw new UnsupportedOperationException("不支持的方法!");
- }
- /**
- * 批量删除(jdbc批量提交)
- *
- * @param list 主键ID或实体列表
- * @param batchSize 批次大小
- * @param useFill 是否启用填充(为true的情况,会将入参转换实体进行delete删除)
- * @return 删除结果
- * @see #removeBatchByIds(Collection, boolean)
- * @since 3.5.0
- * @deprecated 3.5.7
- */
- @Deprecated
- default boolean removeBatchByIds(Collection<?> list, int batchSize, boolean useFill) {
- throw new UnsupportedOperationException("不支持的方法!");
- }
- /**
- * 根据 ID 选择修改
- *
- * @param entity 实体对象
- */
- default boolean updateById(T entity) {
- return SqlHelper.retBool(getBaseMapper().updateById(entity));
- }
- /**
- * 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
- * <p>此方法无法进行自动填充,如需自动填充请使用{@link #update(Object, Wrapper)}</p>
- *
- * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
- */
- default boolean update(Wrapper<T> updateWrapper) {
- return update(null, updateWrapper);
- }
- /**
- * 根据 whereEntity 条件,更新记录
- *
- * @param entity 实体对象(当entity为空时无法进行自动填充)
- * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
- */
- default boolean update(T entity, Wrapper<T> updateWrapper) {
- return SqlHelper.retBool(getBaseMapper().update(entity, updateWrapper));
- }
- /**
- * 根据ID 批量更新
- *
- * @param entityList 实体对象集合
- */
- @Transactional(rollbackFor = Exception.class)
- default boolean updateBatchById(Collection<T> entityList) {
- return updateBatchById(entityList, DEFAULT_BATCH_SIZE);
- }
- /**
- * 根据ID 批量更新
- *
- * @param entityList 实体对象集合
- * @param batchSize 更新批次数量
- */
- boolean updateBatchById(Collection<T> entityList, int batchSize);
- /**
- * TableId 注解存在更新记录,否插入一条记录
- *
- * @param entity 实体对象
- */
- boolean saveOrUpdate(T entity);
- /**
- * 根据 ID 查询
- *
- * @param id 主键ID
- */
- default T getById(Serializable id) {
- return getBaseMapper().selectById(id);
- }
- /**
- * 根据 ID 查询,返回一个Option对象
- *
- * @param id 主键ID
- * @return {@link Optional}
- */
- default Optional<T> getOptById(Serializable id) {
- return Optional.ofNullable(getBaseMapper().selectById(id));
- }
- /**
- * 查询(根据ID 批量查询)
- *
- * @param idList 主键ID列表
- */
- default List<T> listByIds(Collection<? extends Serializable> idList) {
- return getBaseMapper().selectBatchIds(idList);
- }
- /**
- * 查询(根据 columnMap 条件)
- *
- * @param columnMap 表字段 map 对象
- */
- default List<T> listByMap(Map<String, Object> columnMap) {
- return getBaseMapper().selectByMap(columnMap);
- }
- /**
- * 根据 Wrapper,查询一条记录 <br/>
- * <p>结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")</p>
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- default T getOne(Wrapper<T> queryWrapper) {
- return getOne(queryWrapper, true);
- }
- /**
- * 根据 Wrapper,查询一条记录 <br/>
- * <p>结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")</p>
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- * @return {@link Optional} 返回一个Optional对象
- */
- default Optional<T> getOneOpt(Wrapper<T> queryWrapper) {
- return getOneOpt(queryWrapper, true);
- }
- /**
- * 根据 Wrapper,查询一条记录
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- * @param throwEx 有多个 result 是否抛出异常
- */
- T getOne(Wrapper<T> queryWrapper, boolean throwEx);
- /**
- * 根据 Wrapper,查询一条记录
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- * @param throwEx 有多个 result 是否抛出异常
- * @return {@link Optional} 返回一个Optional对象
- */
- Optional<T> getOneOpt(Wrapper<T> queryWrapper, boolean throwEx);
- /**
- * 根据 Wrapper,查询一条记录
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- Map<String, Object> getMap(Wrapper<T> queryWrapper);
- /**
- * 根据 Wrapper,查询一条记录
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- * @param mapper 转换函数
- */
- <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
- /**
- * 查询指定条件是否存在数据
- *
- * @see Wrappers#emptyWrapper()
- */
- default boolean exists(Wrapper<T> queryWrapper) {
- return getBaseMapper().exists(queryWrapper);
- }
- /**
- * 查询总记录数
- *
- * @see Wrappers#emptyWrapper()
- */
- default long count() {
- return count(Wrappers.emptyWrapper());
- }
- /**
- * 根据 Wrapper 条件,查询总记录数
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- default long count(Wrapper<T> queryWrapper) {
- return SqlHelper.retCount(getBaseMapper().selectCount(queryWrapper));
- }
- /**
- * 查询列表
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- default List<T> list(Wrapper<T> queryWrapper) {
- return getBaseMapper().selectList(queryWrapper);
- }
- /**
- * 查询列表
- *
- * @param page 分页条件
- * @param queryWrapper queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- * @return 列表数据
- * @since 3.5.3.2
- */
- default List<T> list(IPage<T> page, Wrapper<T> queryWrapper) {
- return getBaseMapper().selectList(page, queryWrapper);
- }
- /**
- * 查询所有
- *
- * @see Wrappers#emptyWrapper()
- */
- default List<T> list() {
- return list(Wrappers.emptyWrapper());
- }
- /**
- * 分页查询单表数据
- *
- * @param page 分页条件
- * @return 列表数据
- * @since 3.5.3.2
- */
- default List<T> list(IPage<T> page) {
- return list(page, Wrappers.emptyWrapper());
- }
- /**
- * 翻页查询
- *
- * @param page 翻页对象
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- default <E extends IPage<T>> E page(E page, Wrapper<T> queryWrapper) {
- return getBaseMapper().selectPage(page, queryWrapper);
- }
- /**
- * 无条件翻页查询
- *
- * @param page 翻页对象
- * @see Wrappers#emptyWrapper()
- */
- default <E extends IPage<T>> E page(E page) {
- return page(page, Wrappers.emptyWrapper());
- }
- /**
- * 查询列表
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- default List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper) {
- return getBaseMapper().selectMaps(queryWrapper);
- }
- /**
- * 查询列表
- *
- * @param page 分页条件
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- * @return 列表数据
- * @since 3.5.3.2
- */
- default List<Map<String, Object>> listMaps(IPage<? extends Map<String, Object>> page, Wrapper<T> queryWrapper) {
- return getBaseMapper().selectMaps(page, queryWrapper);
- }
- /**
- * 查询所有列表
- *
- * @see Wrappers#emptyWrapper()
- */
- default List<Map<String, Object>> listMaps() {
- return listMaps(Wrappers.emptyWrapper());
- }
- /**
- * 查询列表
- *
- * @param page 分页条件
- * @see Wrappers#emptyWrapper()
- */
- default List<Map<String, Object>> listMaps(IPage<? extends Map<String, Object>> page) {
- return listMaps(page, Wrappers.emptyWrapper());
- }
- /**
- * 查询全部记录
- */
- default <E> List<E> listObjs() {
- return getBaseMapper().selectObjs(null);
- }
- /**
- * 查询全部记录
- *
- * @param mapper 转换函数
- */
- default <V> List<V> listObjs(Function<? super Object, V> mapper) {
- return listObjs(Wrappers.emptyWrapper(), mapper);
- }
- /**
- * 根据 Wrapper 条件,查询全部记录
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- default <E> List<E> listObjs(Wrapper<T> queryWrapper) {
- return getBaseMapper().selectObjs(queryWrapper);
- }
- /**
- * 根据 Wrapper 条件,查询全部记录
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- * @param mapper 转换函数
- */
- default <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper) {
- return getBaseMapper().selectObjs(queryWrapper).stream().filter(Objects::nonNull).map(mapper).collect(Collectors.toList());
- }
- /**
- * 翻页查询
- *
- * @param page 翻页对象
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- default <E extends IPage<Map<String, Object>>> E pageMaps(E page, Wrapper<T> queryWrapper) {
- return getBaseMapper().selectMapsPage(page, queryWrapper);
- }
- /**
- * 无条件翻页查询
- *
- * @param page 翻页对象
- * @see Wrappers#emptyWrapper()
- */
- default <E extends IPage<Map<String, Object>>> E pageMaps(E page) {
- return pageMaps(page, Wrappers.emptyWrapper());
- }
- /**
- * 获取对应 entity 的 BaseMapper
- *
- * @return BaseMapper
- */
- BaseMapper<T> getBaseMapper();
- /**
- * 获取 entity 的 class
- *
- * @return {@link Class<T>}
- */
- Class<T> getEntityClass();
- /**
- * 以下的方法使用介绍:
- *
- * 一. 名称介绍
- * 1. 方法名带有 query 的为对数据的查询操作, 方法名带有 update 的为对数据的修改操作
- * 2. 方法名带有 lambda 的为内部方法入参 column 支持函数式的
- * 二. 支持介绍
- *
- * 1. 方法名带有 query 的支持以 {@link ChainQuery} 内部的方法名结尾进行数据查询操作
- * 2. 方法名带有 update 的支持以 {@link ChainUpdate} 内部的方法名为结尾进行数据修改操作
- *
- * 三. 使用示例,只用不带 lambda 的方法各展示一个例子,其他类推
- * 1. 根据条件获取一条数据: `query().eq("column", value).one()`
- * 2. 根据条件删除一条数据: `update().eq("column", value).remove()`
- *
- */
- /**
- * 链式查询 普通
- *
- * @return QueryWrapper 的包装类
- */
- default QueryChainWrapper<T> query() {
- return ChainWrappers.queryChain(getBaseMapper());
- }
- /**
- * 链式查询 lambda 式
- * <p>注意:不支持 Kotlin </p>
- *
- * @return LambdaQueryWrapper 的包装类
- */
- default LambdaQueryChainWrapper<T> lambdaQuery() {
- return ChainWrappers.lambdaQueryChain(getBaseMapper(), getEntityClass());
- }
- /**
- * 链式查询 lambda 式
- * <p>注意:不支持 Kotlin </p>
- *
- * @param entity 实体对象
- * @return LambdaQueryWrapper 的包装类
- */
- default LambdaQueryChainWrapper<T> lambdaQuery(T entity) {
- return ChainWrappers.lambdaQueryChain(getBaseMapper(), entity);
- }
- /**
- * 链式查询 lambda 式
- * kotlin 使用
- *
- * @return KtQueryWrapper 的包装类
- */
- default KtQueryChainWrapper<T> ktQuery() {
- return ChainWrappers.ktQueryChain(getBaseMapper(), getEntityClass());
- }
- /**
- * 链式查询 lambda 式
- * kotlin 使用
- *
- * @return KtQueryWrapper 的包装类
- */
- default KtUpdateChainWrapper<T> ktUpdate() {
- return ChainWrappers.ktUpdateChain(getBaseMapper(), getEntityClass());
- }
- /**
- * 链式更改 普通
- *
- * @return UpdateWrapper 的包装类
- */
- default UpdateChainWrapper<T> update() {
- return ChainWrappers.updateChain(getBaseMapper());
- }
- /**
- * 链式更改 lambda 式
- * <p>注意:不支持 Kotlin </p>
- *
- * @return LambdaUpdateWrapper 的包装类
- */
- default LambdaUpdateChainWrapper<T> lambdaUpdate() {
- return ChainWrappers.lambdaUpdateChain(getBaseMapper());
- }
- /**
- * <p>
- * 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
- * 此次修改主要是减少了此项业务代码的代码量(存在性验证之后的saveOrUpdate操作)
- * </p>
- * <p>
- * 该方法不推荐在多线程并发下使用,并发可能存在间隙锁的问题,可以采用先查询后判断是否更新或保存。
- * </p>
- * <p>
- * 该方法存在安全隐患将在后续大版本删除
- * </p>
- *
- * @param entity 实体对象
- */
- @Deprecated
- default boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper) {
- return update(entity, updateWrapper) || saveOrUpdate(entity);
- }
- }
|