Procházet zdrojové kódy

减少泛型实体提取.

聂秋秋 před 5 roky
rodič
revize
c9523c8430

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

@@ -67,6 +67,8 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         return baseMapper;
     }
 
+    protected Class entityClass = currentModelClass();
+
     /**
      * 判断数据库操作是否成功
      *
@@ -88,7 +90,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
      */
     @Deprecated
     protected SqlSession sqlSessionBatch() {
-        return SqlHelper.sqlSessionBatch(currentModelClass());
+        return SqlHelper.sqlSessionBatch(entityClass);
     }
 
     /**
@@ -99,7 +101,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
      */
     @Deprecated
     protected void closeSqlSession(SqlSession sqlSession) {
-        SqlSessionUtils.closeSqlSession(sqlSession, GlobalConfigUtils.currentSessionFactory(currentModelClass()));
+        SqlSessionUtils.closeSqlSession(sqlSession, GlobalConfigUtils.currentSessionFactory(entityClass));
     }
 
     /**
@@ -109,7 +111,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
      * @return ignore
      */
     protected String sqlStatement(SqlMethod sqlMethod) {
-        return SqlHelper.table(currentModelClass()).getSqlStatement(sqlMethod.getMethod());
+        return SqlHelper.table(entityClass).getSqlStatement(sqlMethod.getMethod());
     }
 
     @Override
@@ -155,19 +157,18 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
     @Transactional(rollbackFor = Exception.class)
     @Override
     public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
-        Class<?> cls = currentModelClass();
-        TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
+        TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
         Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
         String keyProperty = tableInfo.getKeyProperty();
         Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
         return executeBatch(entityList, batchSize, (sqlSession, entity) -> {
-            Object idVal = ReflectionKit.getMethodValue(cls, entity, keyProperty);
+            Object idVal = ReflectionKit.getMethodValue(entityClass, entity, keyProperty);
             if (StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal))) {
-                sqlSession.insert(sqlStatement(SqlMethod.INSERT_ONE), entity);
+                sqlSession.insert(tableInfo.getSqlStatement(SqlMethod.INSERT_ONE.getMethod()), entity);
             } else {
                 MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
                 param.put(Constants.ENTITY, entity);
-                sqlSession.update(sqlStatement(SqlMethod.UPDATE_BY_ID), param);
+                sqlSession.update(tableInfo.getSqlStatement(SqlMethod.UPDATE_BY_ID.getMethod()), param);
             }
         });
     }
@@ -289,8 +290,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
      */
     @Deprecated
     protected boolean executeBatch(Consumer<SqlSession> consumer) {
-        Class<T> tClass = currentModelClass();
-        SqlSessionFactory sqlSessionFactory = SqlHelper.sqlSessionFactory(tClass);
+        SqlSessionFactory sqlSessionFactory = SqlHelper.sqlSessionFactory(entityClass);
         SqlSessionHolder sqlSessionHolder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sqlSessionFactory);
         boolean transaction = TransactionSynchronizationManager.isSynchronizationActive();
         if (sqlSessionHolder != null) {

+ 1 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/cache/service/impl/CacheServiceImpl.java

@@ -17,7 +17,7 @@ public class CacheServiceImpl extends ServiceImpl<CacheMapper, CacheModel> imple
     //手动撸一个批量删除.
     private void removeBatchById(Collection<Long> idList) {
         String sqlStatement = sqlStatement(SqlMethod.DELETE_BY_ID);
-        executeBatch(idList,idList.size(),(sqlSession, id) -> sqlSession.delete(sqlStatement,id));
+        executeBatch(idList, (sqlSession, id) -> sqlSession.delete(sqlStatement, id));
     }
 
     @Override