123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- /*
- * Copyright (c) 2011-2019, hubin (jobob@qq.com).
- * <p>
- * 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
- * <p>
- * https://www.apache.org/licenses/LICENSE-2.0
- * <p>
- * 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.Wrappers;
- import com.baomidou.mybatisplus.extension.service.additional.query.ChainQuery;
- import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
- import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
- import com.baomidou.mybatisplus.extension.service.additional.update.ChainUpdate;
- import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
- import com.baomidou.mybatisplus.extension.service.additional.update.impl.UpdateChainWrapper;
- import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
- import org.springframework.transaction.annotation.Transactional;
- import java.io.Serializable;
- import java.util.Collection;
- import java.util.List;
- import java.util.Map;
- import java.util.function.Function;
- /**
- * 顶级 Service
- *
- * @author hubin
- * @since 2018-06-23
- */
- public interface IService<T> {
- /**
- * 插入一条记录(选择字段,策略插入)
- *
- * @param entity 实体对象
- */
- boolean save(T entity);
- /**
- * 插入(批量)
- *
- * @param entityList 实体对象集合
- */
- @Transactional(rollbackFor = Exception.class)
- default boolean saveBatch(Collection<T> entityList) {
- return saveBatch(entityList, 1000);
- }
- /**
- * 插入(批量)
- *
- * @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, 1000);
- }
- /**
- * 批量修改插入
- *
- * @param entityList 实体对象集合
- * @param batchSize 每次的数量
- */
- boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
- /**
- * 根据 ID 删除
- *
- * @param id 主键ID
- */
- boolean removeById(Serializable id);
- /**
- * 根据 columnMap 条件,删除记录
- *
- * @param columnMap 表字段 map 对象
- */
- boolean removeByMap(Map<String, Object> columnMap);
- /**
- * 根据 entity 条件,删除记录
- *
- * @param queryWrapper 实体包装类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- boolean remove(Wrapper<T> queryWrapper);
- /**
- * 删除(根据ID 批量删除)
- *
- * @param idList 主键ID列表
- */
- boolean removeByIds(Collection<? extends Serializable> idList);
- /**
- * 根据 ID 选择修改
- *
- * @param entity 实体对象
- */
- boolean updateById(T entity);
- /**
- * 根据 whereEntity 条件,更新记录
- *
- * @param entity 实体对象
- * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
- */
- boolean update(T entity, Wrapper<T> updateWrapper);
- /**
- * 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
- *
- * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
- */
- default boolean update(Wrapper<T> updateWrapper) {
- return update(null, updateWrapper);
- }
- /**
- * 根据ID 批量更新
- *
- * @param entityList 实体对象集合
- */
- @Transactional(rollbackFor = Exception.class)
- default boolean updateBatchById(Collection<T> entityList) {
- return updateBatchById(entityList, 1000);
- }
- /**
- * 根据ID 批量更新
- *
- * @param entityList 实体对象集合
- * @param batchSize 更新批次数量
- */
- boolean updateBatchById(Collection<T> entityList, int batchSize);
- /**
- * TableId 注解存在更新记录,否插入一条记录
- *
- * @param entity 实体对象
- */
- boolean saveOrUpdate(T entity);
- /**
- * 根据 ID 查询
- *
- * @param id 主键ID
- */
- T getById(Serializable id);
- /**
- * 查询(根据ID 批量查询)
- *
- * @param idList 主键ID列表
- */
- Collection<T> listByIds(Collection<? extends Serializable> idList);
- /**
- * 查询(根据 columnMap 条件)
- *
- * @param columnMap 表字段 map 对象
- */
- Collection<T> listByMap(Map<String, Object> 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,查询一条记录
- *
- * @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}
- */
- Map<String, Object> getMap(Wrapper<T> queryWrapper);
- /**
- * 根据 Wrapper,查询一条记录
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- * @param mapper 转换函数
- */
- default <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper) {
- return SqlHelper.getObject(listObjs(queryWrapper, mapper));
- }
- /**
- * 根据 Wrapper 条件,查询总记录数
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- int count(Wrapper<T> queryWrapper);
- /**
- * 查询总记录数
- *
- * @see Wrappers#emptyWrapper()
- */
- default int count() {
- return count(Wrappers.emptyWrapper());
- }
- /**
- * 查询列表
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- List<T> list(Wrapper<T> queryWrapper);
- /**
- * 查询所有
- *
- * @see Wrappers#emptyWrapper()
- */
- default List<T> list() {
- return list(Wrappers.emptyWrapper());
- }
- /**
- * 翻页查询
- *
- * @param page 翻页对象
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);
- /**
- * 无条件翻页查询
- *
- * @param page 翻页对象
- * @see Wrappers#emptyWrapper()
- */
- default IPage<T> page(IPage<T> page) {
- return page(page, Wrappers.emptyWrapper());
- }
- /**
- * 查询列表
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);
- /**
- * 查询所有列表
- *
- * @see Wrappers#emptyWrapper()
- */
- default List<Map<String, Object>> listMaps() {
- return listMaps(Wrappers.emptyWrapper());
- }
- /**
- * 查询全部记录
- */
- default List<Object> listObjs() {
- return listObjs(Function.identity());
- }
- /**
- * 查询全部记录
- *
- * @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 List<Object> listObjs(Wrapper<T> queryWrapper) {
- return listObjs(queryWrapper, Function.identity());
- }
- /**
- * 根据 Wrapper 条件,查询全部记录
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- * @param mapper 转换函数
- */
- <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
- /**
- * 翻页查询
- *
- * @param page 翻页对象
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);
- /**
- * 无条件翻页查询
- *
- * @param page 翻页对象
- * @see Wrappers#emptyWrapper()
- */
- default IPage<Map<String, Object>> pageMaps(IPage<T> page) {
- return pageMaps(page, Wrappers.emptyWrapper());
- }
- /**
- * 获取对应 entity 的 BaseMapper
- *
- * @return BaseMapper
- */
- BaseMapper<T> getBaseMapper();
- /**
- * 链式查询 普通
- * 支持 {@link ChainQuery} 内的方法为结尾进行查询
- *
- * @return QueryWrapper 的包装类
- */
- default QueryChainWrapper<T> query() {
- return new QueryChainWrapper<>(getBaseMapper());
- }
- /**
- * 链式查询 lambda 式
- * <p>注意:不支持 Kotlin </p>
- *
- * @return LambdaQueryWrapper 的包装类
- */
- default LambdaQueryChainWrapper<T> lambdaQuery() {
- return new LambdaQueryChainWrapper<>(getBaseMapper());
- }
- /**
- * 链式更改 普通
- * 支持 {@link ChainUpdate} 内的方法为结尾进行数据修改
- *
- * @return UpdateWrapper 的包装类
- */
- default UpdateChainWrapper<T> update() {
- return new UpdateChainWrapper<>(getBaseMapper());
- }
- /**
- * 链式更改 lambda 式
- * <p>注意:不支持 Kotlin </p>
- *
- * @return LambdaUpdateWrapper 的包装类
- */
- default LambdaUpdateChainWrapper<T> lambdaUpdate() {
- return new LambdaUpdateChainWrapper<>(getBaseMapper());
- }
- }
|