Sfoglia il codice sorgente

优化一下代码/注释/变量名

miemie 6 anni fa
parent
commit
3a112503bc

+ 26 - 53
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/service/IService.java

@@ -39,7 +39,6 @@ public interface IService<T> {
      * </p>
      *
      * @param entity 实体对象
-     * @return boolean
      */
     boolean save(T entity);
 
@@ -49,7 +48,6 @@ public interface IService<T> {
      * </p>
      *
      * @param entityList 实体对象集合
-     * @return boolean
      */
     boolean saveBatch(Collection<T> entityList);
 
@@ -60,7 +58,6 @@ public interface IService<T> {
      *
      * @param entityList 实体对象集合
      * @param batchSize  插入批次数量
-     * @return boolean
      */
     boolean saveBatch(Collection<T> entityList, int batchSize);
 
@@ -70,7 +67,6 @@ public interface IService<T> {
      * </p>
      *
      * @param entityList 实体对象集合
-     * @return boolean
      */
     boolean saveOrUpdateBatch(Collection<T> entityList);
 
@@ -80,8 +76,7 @@ public interface IService<T> {
      * </p>
      *
      * @param entityList 实体对象集合
-     * @param batchSize
-     * @return boolean
+     * @param batchSize  每次的数量
      */
     boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
 
@@ -91,7 +86,6 @@ public interface IService<T> {
      * </p>
      *
      * @param id 主键ID
-     * @return boolean
      */
     boolean removeById(Serializable id);
 
@@ -101,7 +95,6 @@ public interface IService<T> {
      * </p>
      *
      * @param columnMap 表字段 map 对象
-     * @return boolean
      */
     boolean removeByMap(Map<String, Object> columnMap);
 
@@ -110,10 +103,9 @@ public interface IService<T> {
      * 根据 entity 条件,删除记录
      * </p>
      *
-     * @param wrapper 实体包装类 {@link Wrapper}
-     * @return boolean
+     * @param queryWrapper 实体包装类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
      */
-    boolean remove(Wrapper<T> wrapper);
+    boolean remove(Wrapper<T> queryWrapper);
 
     /**
      * <p>
@@ -121,7 +113,6 @@ public interface IService<T> {
      * </p>
      *
      * @param idList 主键ID列表
-     * @return boolean
      */
     boolean removeByIds(Collection<? extends Serializable> idList);
 
@@ -131,7 +122,6 @@ public interface IService<T> {
      * </p>
      *
      * @param entity 实体对象
-     * @return boolean
      */
     boolean updateById(T entity);
 
@@ -140,11 +130,10 @@ public interface IService<T> {
      * 根据 whereEntity 条件,更新记录
      * </p>
      *
-     * @param entity  实体对象
-     * @param wrapper 实体包装类 {@link Wrapper}
-     * @return boolean
+     * @param entity        实体对象
+     * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
      */
-    boolean update(T entity, Wrapper<T> wrapper);
+    boolean update(T entity, Wrapper<T> updateWrapper);
 
     /**
      * <p>
@@ -152,7 +141,6 @@ public interface IService<T> {
      * </p>
      *
      * @param entityList 实体对象集合
-     * @return boolean
      */
     boolean updateBatchById(Collection<T> entityList);
 
@@ -163,7 +151,6 @@ public interface IService<T> {
      *
      * @param entityList 实体对象集合
      * @param batchSize  更新批次数量
-     * @return boolean
      */
     boolean updateBatchById(Collection<T> entityList, int batchSize);
 
@@ -173,7 +160,6 @@ public interface IService<T> {
      * </p>
      *
      * @param entity 实体对象
-     * @return boolean
      */
     boolean saveOrUpdate(T entity);
 
@@ -183,7 +169,6 @@ public interface IService<T> {
      * </p>
      *
      * @param id 主键ID
-     * @return T
      */
     T getById(Serializable id);
 
@@ -193,7 +178,6 @@ public interface IService<T> {
      * </p>
      *
      * @param idList 主键ID列表
-     * @return Collection<T>
      */
     Collection<T> listByIds(Collection<? extends Serializable> idList);
 
@@ -203,7 +187,6 @@ public interface IService<T> {
      * </p>
      *
      * @param columnMap 表字段 map 对象
-     * @return Collection<T>
      */
     Collection<T> listByMap(Map<String, Object> columnMap);
 
@@ -212,91 +195,81 @@ public interface IService<T> {
      * 根据 Wrapper,查询一条记录
      * </p>
      *
-     * @param wrapper 实体对象
-     * @return T
+     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
      */
-    T getOne(Wrapper<T> wrapper);
+    T getOne(Wrapper<T> queryWrapper);
 
     /**
      * <p>
      * 根据 Wrapper,查询一条记录
      * </p>
      *
-     * @param wrapper {@link Wrapper}
-     * @return
+     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
      */
-    Map<String, Object> getMap(Wrapper<T> wrapper);
+    Map<String, Object> getMap(Wrapper<T> queryWrapper);
 
     /**
      * <p>
      * 根据 Wrapper,查询一条记录
      * </p>
      *
-     * @param wrapper {@link Wrapper}
-     * @return Object
+     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
      */
-    Object getObj(Wrapper<T> wrapper);
+    Object getObj(Wrapper<T> queryWrapper);
 
     /**
      * <p>
      * 根据 Wrapper 条件,查询总记录数
      * </p>
      *
-     * @param wrapper 实体对象
-     * @return int
+     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
      */
-    int count(Wrapper<T> wrapper);
+    int count(Wrapper<T> queryWrapper);
 
     /**
      * <p>
      * 查询列表
      * </p>
      *
-     * @param wrapper 实体包装类 {@link Wrapper}
-     * @return
+     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
      */
-    List<T> list(Wrapper<T> wrapper);
+    List<T> list(Wrapper<T> queryWrapper);
 
     /**
      * <p>
      * 翻页查询
      * </p>
      *
-     * @param page    翻页对象
-     * @param wrapper 实体包装类 {@link Wrapper}
-     * @return
+     * @param page         翻页对象
+     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
      */
-    IPage<T> page(IPage<T> page, Wrapper<T> wrapper);
+    IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);
 
     /**
      * <p>
      * 查询列表
      * </p>
      *
-     * @param wrapper {@link Wrapper}
-     * @return
+     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
      */
-    List<Map<String, Object>> listMaps(Wrapper<T> wrapper);
+    List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);
 
     /**
      * <p>
      * 根据 Wrapper 条件,查询全部记录
      * </p>
      *
-     * @param wrapper 实体对象封装操作类(可以为 null)
-     * @return List<Object>
+     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
      */
-    List<Object> listObjs(Wrapper<T> wrapper);
+    List<Object> listObjs(Wrapper<T> queryWrapper);
 
     /**
      * <p>
      * 翻页查询
      * </p>
      *
-     * @param page    翻页对象
-     * @param wrapper {@link Wrapper}
-     * @return
+     * @param page         翻页对象
+     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
      */
-    IPage<Map<String, Object>> pageMaps(IPage page, Wrapper<T> wrapper);
-
+    IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);
 }

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

@@ -17,22 +17,23 @@ package com.baomidou.mybatisplus.extension.service.impl;
 
 import java.io.Serializable;
 import java.util.Collection;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
+import javax.annotation.Resource;
+
 import org.apache.ibatis.binding.MapperMethod;
 import org.apache.ibatis.session.SqlSession;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 
 import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.enums.SqlMethod;
-import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
 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.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.core.toolkit.Constants;
+import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
 import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
 import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
@@ -48,9 +49,10 @@ import com.baomidou.mybatisplus.extension.service.IService;
  * @author hubin
  * @since 2018-06-23
  */
+@SuppressWarnings("unchecked")
 public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
 
-    @Autowired
+    @Resource
     protected M baseMapper;
 
     /**
@@ -118,9 +120,8 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         try (SqlSession batchSqlSession = sqlSessionBatch()) {
             int i = 0;
             String sqlStatement = sqlStatement(SqlMethod.INSERT_ONE);
-            Iterator<T> iterator = entityList.iterator();
-            while (iterator.hasNext()) {
-                batchSqlSession.insert(sqlStatement, iterator.next());
+            for (T anEntityList : entityList) {
+                batchSqlSession.insert(sqlStatement, anEntityList);
                 if (i >= 1 && i % batchSize == 0) {
                     batchSqlSession.flushStatements();
                 }
@@ -128,7 +129,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
             }
             batchSqlSession.flushStatements();
         } catch (Throwable e) {
-            throw new MybatisPlusException("Error: Cannot execute saveBatch Method. Cause", e);
+            throw ExceptionUtils.mpe("Error: Cannot execute saveBatch Method. Cause", e);
         }
         return true;
     }
@@ -157,7 +158,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
                     return updateById(entity) || save(entity);
                 }
             } else {
-                throw new MybatisPlusException("Error:  Can not execute. Could not find @TableId.");
+                throw ExceptionUtils.mpe("Error:  Can not execute. Could not find @TableId.");
             }
         }
         return false;
@@ -176,13 +177,12 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
             throw new IllegalArgumentException("Error: entityList must not be empty");
         }
         try (SqlSession batchSqlSession = sqlSessionBatch()) {
-            Iterator<T> iterator = entityList.iterator();
-            while (iterator.hasNext()) {
-                saveOrUpdate(iterator.next());
+            for (T anEntityList : entityList) {
+                saveOrUpdate(anEntityList);
             }
             batchSqlSession.flushStatements();
         } catch (Throwable e) {
-            throw new MybatisPlusException("Error: Cannot execute saveOrUpdateBatch Method. Cause", e);
+            throw ExceptionUtils.mpe("Error: Cannot execute saveOrUpdateBatch Method. Cause", e);
         }
         return true;
     }
@@ -195,7 +195,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
     @Override
     public boolean removeByMap(Map<String, Object> columnMap) {
         if (ObjectUtils.isEmpty(columnMap)) {
-            throw new MybatisPlusException("removeByMap columnMap is empty.");
+            throw ExceptionUtils.mpe("removeByMap columnMap is empty.");
         }
         return SqlHelper.delBool(baseMapper.deleteByMap(columnMap));
     }
@@ -216,8 +216,8 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
     }
 
     @Override
-    public boolean update(T entity, Wrapper<T> wrapper) {
-        return ServiceImpl.retBool(baseMapper.update(entity, wrapper));
+    public boolean update(T entity, Wrapper<T> updateWrapper) {
+        return ServiceImpl.retBool(baseMapper.update(entity, updateWrapper));
     }
 
     @Transactional(rollbackFor = Exception.class)
@@ -235,10 +235,9 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         try (SqlSession batchSqlSession = sqlSessionBatch()) {
             int i = 0;
             String sqlStatement = sqlStatement(SqlMethod.UPDATE_BY_ID);
-            Iterator<T> iterator = entityList.iterator();
-            while (iterator.hasNext()) {
+            for (T anEntityList : entityList) {
                 MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
-                param.put("et", iterator.next());
+                param.put(Constants.ENTITY, anEntityList);
                 batchSqlSession.update(sqlStatement, param);
                 if (i >= 1 && i % batchSize == 0) {
                     batchSqlSession.flushStatements();
@@ -247,7 +246,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
             }
             batchSqlSession.flushStatements();
         } catch (Throwable e) {
-            throw new MybatisPlusException("Error: Cannot execute updateBatchById Method. Cause", e);
+            throw ExceptionUtils.mpe("Error: Cannot execute updateBatchById Method. Cause", e);
         }
         return true;
     }
@@ -268,49 +267,49 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
     }
 
     @Override
-    public T getOne(Wrapper<T> wrapper) {
-        return SqlHelper.getObject(baseMapper.selectList(wrapper));
+    public T getOne(Wrapper<T> queryWrapper) {
+        return SqlHelper.getObject(baseMapper.selectList(queryWrapper));
     }
 
     @Override
-    public Map<String, Object> getMap(Wrapper<T> wrapper) {
-        return SqlHelper.getObject(baseMapper.selectMaps(wrapper));
+    public Map<String, Object> getMap(Wrapper<T> queryWrapper) {
+        return SqlHelper.getObject(baseMapper.selectMaps(queryWrapper));
     }
 
     @Override
-    public Object getObj(Wrapper<T> wrapper) {
-        return SqlHelper.getObject(baseMapper.selectObjs(wrapper));
+    public Object getObj(Wrapper<T> queryWrapper) {
+        return SqlHelper.getObject(baseMapper.selectObjs(queryWrapper));
     }
 
     @Override
-    public int count(Wrapper<T> wrapper) {
-        return SqlHelper.retCount(baseMapper.selectCount(wrapper));
+    public int count(Wrapper<T> queryWrapper) {
+        return SqlHelper.retCount(baseMapper.selectCount(queryWrapper));
     }
 
     @Override
-    public List<T> list(Wrapper<T> wrapper) {
-        return baseMapper.selectList(wrapper);
+    public List<T> list(Wrapper<T> queryWrapper) {
+        return baseMapper.selectList(queryWrapper);
     }
 
     @Override
-    public IPage<T> page(IPage<T> page, Wrapper<T> wrapper) {
-        wrapper = (Wrapper<T>) SqlHelper.fillWrapper(page, wrapper);
-        return baseMapper.selectPage(page, wrapper);
+    public IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper) {
+        queryWrapper = (Wrapper<T>) SqlHelper.fillWrapper(page, queryWrapper);
+        return baseMapper.selectPage(page, queryWrapper);
     }
 
     @Override
-    public List<Map<String, Object>> listMaps(Wrapper<T> wrapper) {
-        return baseMapper.selectMaps(wrapper);
+    public List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper) {
+        return baseMapper.selectMaps(queryWrapper);
     }
 
     @Override
-    public List<Object> listObjs(Wrapper<T> wrapper) {
-        return baseMapper.selectObjs(wrapper);
+    public List<Object> listObjs(Wrapper<T> queryWrapper) {
+        return baseMapper.selectObjs(queryWrapper);
     }
 
     @Override
-    public IPage<Map<String, Object>> pageMaps(IPage page, Wrapper<T> wrapper) {
-        wrapper = (Wrapper<T>) SqlHelper.fillWrapper(page, wrapper);
-        return baseMapper.selectMapsPage(page, wrapper);
+    public IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper) {
+        queryWrapper = (Wrapper<T>) SqlHelper.fillWrapper(page, queryWrapper);
+        return baseMapper.selectMapsPage(page, queryWrapper);
     }
 }