Procházet zdrojové kódy

部分method改为default方法

miemie před 5 roky
rodič
revize
2a3e14b172

+ 68 - 27
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/service/IService.java

@@ -18,19 +18,24 @@ 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.LambdaQueryChainWrapper;
 import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
 import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
 import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
 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.Collection;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.function.Function;
+import java.util.stream.Collectors;
 
 /**
  * 顶级 Service
@@ -50,7 +55,9 @@ public interface IService<T> {
      *
      * @param entity 实体对象
      */
-    boolean save(T entity);
+    default boolean save(T entity) {
+        return SqlHelper.retBool(getBaseMapper().insert(entity));
+    }
 
     /**
      * 插入(批量)
@@ -93,51 +100,67 @@ public interface IService<T> {
      *
      * @param id 主键ID
      */
-    boolean removeById(Serializable id);
+    default boolean removeById(Serializable id) {
+        return SqlHelper.retBool(getBaseMapper().deleteById(id));
+    }
 
     /**
      * 根据 columnMap 条件,删除记录
      *
      * @param columnMap 表字段 map 对象
      */
-    boolean removeByMap(Map<String, Object> columnMap);
+    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}
      */
-    boolean remove(Wrapper<T> queryWrapper);
+    default boolean remove(Wrapper<T> queryWrapper) {
+        return SqlHelper.retBool(getBaseMapper().delete(queryWrapper));
+    }
 
     /**
      * 删除(根据ID 批量删除)
      *
      * @param idList 主键ID列表
      */
-    boolean removeByIds(Collection<? extends Serializable> idList);
+    default boolean removeByIds(Collection<? extends Serializable> idList) {
+        if (CollectionUtils.isEmpty(idList)) {
+            return false;
+        }
+        return SqlHelper.retBool(getBaseMapper().deleteBatchIds(idList));
+    }
 
     /**
      * 根据 ID 选择修改
      *
      * @param entity 实体对象
      */
-    boolean updateById(T entity);
+    default boolean updateById(T entity) {
+        return SqlHelper.retBool(getBaseMapper().updateById(entity));
+    }
 
     /**
-     * 根据 whereEntity 条件,更新记录
+     * 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
      *
-     * @param entity        实体对象
      * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
      */
-    boolean update(T entity, Wrapper<T> updateWrapper);
+    default boolean update(Wrapper<T> updateWrapper) {
+        return update(null, updateWrapper);
+    }
 
     /**
-     * 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
+     * 根据 whereEntity 条件,更新记录
      *
+     * @param entity        实体对象
      * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
      */
-    default boolean update(Wrapper<T> updateWrapper) {
-        return update(null, updateWrapper);
+    default boolean update(T entity, Wrapper<T> updateWrapper) {
+        return SqlHelper.retBool(getBaseMapper().update(entity, updateWrapper));
     }
 
     /**
@@ -170,21 +193,27 @@ public interface IService<T> {
      *
      * @param id 主键ID
      */
-    T getById(Serializable id);
+    default T getById(Serializable id) {
+        return getBaseMapper().selectById(id);
+    }
 
     /**
      * 查询(根据ID 批量查询)
      *
      * @param idList 主键ID列表
      */
-    List<T> listByIds(Collection<? extends Serializable> idList);
+    default List<T> listByIds(Collection<? extends Serializable> idList) {
+        return getBaseMapper().selectBatchIds(idList);
+    }
 
     /**
      * 查询(根据 columnMap 条件)
      *
      * @param columnMap 表字段 map 对象
      */
-    List<T> listByMap(Map<String, Object> columnMap);
+    default List<T> listByMap(Map<String, Object> columnMap) {
+        return getBaseMapper().selectByMap(columnMap);
+    }
 
     /**
      * 根据 Wrapper,查询一条记录 <br/>
@@ -219,13 +248,6 @@ public interface IService<T> {
      */
     <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
 
-    /**
-     * 根据 Wrapper 条件,查询总记录数
-     *
-     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
-     */
-    int count(Wrapper<T> queryWrapper);
-
     /**
      * 查询总记录数
      *
@@ -235,12 +257,23 @@ public interface IService<T> {
         return count(Wrappers.emptyWrapper());
     }
 
+    /**
+     * 根据 Wrapper 条件,查询总记录数
+     *
+     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
+     */
+    default int count(Wrapper<T> queryWrapper) {
+        return SqlHelper.retCount(getBaseMapper().selectCount(queryWrapper));
+    }
+
     /**
      * 查询列表
      *
      * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
      */
-    List<T> list(Wrapper<T> queryWrapper);
+    default List<T> list(Wrapper<T> queryWrapper) {
+        return getBaseMapper().selectList(queryWrapper);
+    }
 
     /**
      * 查询所有
@@ -257,7 +290,9 @@ public interface IService<T> {
      * @param page         翻页对象
      * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
      */
-    <E extends IPage<T>> E page(E page, Wrapper<T> queryWrapper);
+    default <E extends IPage<T>> E page(E page, Wrapper<T> queryWrapper) {
+        return getBaseMapper().selectPage(page, queryWrapper);
+    }
 
     /**
      * 无条件翻页查询
@@ -274,7 +309,9 @@ public interface IService<T> {
      *
      * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
      */
-    List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);
+    default List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper) {
+        return getBaseMapper().selectMaps(queryWrapper);
+    }
 
     /**
      * 查询所有列表
@@ -316,7 +353,9 @@ public interface IService<T> {
      * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
      * @param mapper       转换函数
      */
-    <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> 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());
+    }
 
     /**
      * 翻页查询
@@ -324,7 +363,9 @@ public interface IService<T> {
      * @param page         翻页对象
      * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
      */
-    <E extends IPage<Map<String, Object>>> E pageMaps(E page, Wrapper<T> queryWrapper);
+    default <E extends IPage<Map<String, Object>>> E pageMaps(E page, Wrapper<T> queryWrapper) {
+        return getBaseMapper().selectMapsPage(page, queryWrapper);
+    }
 
     /**
      * 无条件翻页查询

+ 3 - 89
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/service/impl/ServiceImpl.java

@@ -18,7 +18,6 @@ package com.baomidou.mybatisplus.extension.service.impl;
 import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.enums.SqlMethod;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
 import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
 import com.baomidou.mybatisplus.core.toolkit.*;
@@ -40,13 +39,11 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
 
 import java.io.Serializable;
 import java.util.Collection;
-import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.function.BiConsumer;
 import java.util.function.Consumer;
 import java.util.function.Function;
-import java.util.stream.Collectors;
 
 /**
  * IService 实现类( 泛型:M 是 mapper 对象,T 是实体 )
@@ -67,14 +64,16 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         return baseMapper;
     }
 
-    protected Class entityClass = currentModelClass();
+    protected Class<?> entityClass = currentModelClass();
 
     /**
      * 判断数据库操作是否成功
      *
      * @param result 数据库操作返回影响条数
      * @return boolean
+     * @deprecated 3.3.1
      */
+    @Deprecated
     protected boolean retBool(Integer result) {
         return SqlHelper.retBool(result);
     }
@@ -114,11 +113,6 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         return SqlHelper.table(entityClass).getSqlStatement(sqlMethod.getMethod());
     }
 
-    @Override
-    public boolean save(T entity) {
-        return retBool(baseMapper.insert(entity));
-    }
-
     /**
      * 批量插入
      *
@@ -173,40 +167,6 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         });
     }
 
-    @Override
-    public boolean removeById(Serializable id) {
-        return SqlHelper.retBool(baseMapper.deleteById(id));
-    }
-
-    @Override
-    public boolean removeByMap(Map<String, Object> columnMap) {
-        Assert.notEmpty(columnMap, "error: columnMap must not be empty");
-        return SqlHelper.retBool(baseMapper.deleteByMap(columnMap));
-    }
-
-    @Override
-    public boolean remove(Wrapper<T> wrapper) {
-        return SqlHelper.retBool(baseMapper.delete(wrapper));
-    }
-
-    @Override
-    public boolean removeByIds(Collection<? extends Serializable> idList) {
-        if (CollectionUtils.isEmpty(idList)) {
-            return false;
-        }
-        return SqlHelper.retBool(baseMapper.deleteBatchIds(idList));
-    }
-
-    @Override
-    public boolean updateById(T entity) {
-        return retBool(baseMapper.updateById(entity));
-    }
-
-    @Override
-    public boolean update(T entity, Wrapper<T> updateWrapper) {
-        return retBool(baseMapper.update(entity, updateWrapper));
-    }
-
     @Transactional(rollbackFor = Exception.class)
     @Override
     public boolean updateBatchById(Collection<T> entityList, int batchSize) {
@@ -218,21 +178,6 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         });
     }
 
-    @Override
-    public T getById(Serializable id) {
-        return baseMapper.selectById(id);
-    }
-
-    @Override
-    public List<T> listByIds(Collection<? extends Serializable> idList) {
-        return baseMapper.selectBatchIds(idList);
-    }
-
-    @Override
-    public List<T> listByMap(Map<String, Object> columnMap) {
-        return baseMapper.selectByMap(columnMap);
-    }
-
     @Override
     public T getOne(Wrapper<T> queryWrapper, boolean throwEx) {
         if (throwEx) {
@@ -246,36 +191,6 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         return SqlHelper.getObject(log, baseMapper.selectMaps(queryWrapper));
     }
 
-    @Override
-    public int count(Wrapper<T> queryWrapper) {
-        return SqlHelper.retCount(baseMapper.selectCount(queryWrapper));
-    }
-
-    @Override
-    public List<T> list(Wrapper<T> queryWrapper) {
-        return baseMapper.selectList(queryWrapper);
-    }
-
-    @Override
-    public <E extends IPage<T>> E page(E page, Wrapper<T> queryWrapper) {
-        return baseMapper.selectPage(page, queryWrapper);
-    }
-
-    @Override
-    public List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper) {
-        return baseMapper.selectMaps(queryWrapper);
-    }
-
-    @Override
-    public <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper) {
-        return baseMapper.selectObjs(queryWrapper).stream().filter(Objects::nonNull).map(mapper).collect(Collectors.toList());
-    }
-
-    @Override
-    public <E extends IPage<Map<String, Object>>> E pageMaps(E page, Wrapper<T> queryWrapper) {
-        return baseMapper.selectMapsPage(page, queryWrapper);
-    }
-
     @Override
     public <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper) {
         return SqlHelper.getObject(log, listObjs(queryWrapper, mapper));
@@ -359,5 +274,4 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
     protected <E> boolean executeBatch(Collection<E> list, BiConsumer<SqlSession, E> consumer) {
         return executeBatch(list, DEFAULT_BATCH_SIZE, consumer);
     }
-
 }