Browse Source

service使用泛型确定mapper命名空间.

https://github.com/baomidou/mybatis-plus/issues/2823
nieqiurong 4 years ago
parent
commit
198347b0e3

+ 2 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfo.java

@@ -182,7 +182,9 @@ public class TableInfo implements Constants {
      *
      * @param sqlMethod MybatisPlus 支持 SQL 方法
      * @return SQL Statement
+     * @deprecated 3.3.3 如果存在的多mapper共用一个实体的情况,这里可能会出现获取命名空间错误的情况
      */
+    @Deprecated
     public String getSqlStatement(String sqlMethod) {
         return currentNamespace + DOT + sqlMethod;
     }

+ 2 - 0
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/incrementer/ImadcnIdentifierGeneratorTest.java

@@ -2,12 +2,14 @@ package com.baomidou.mybatisplus.core.incrementer;
 
 import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 
 /**
  * @author miemie
  * @since 2020-08-11
  */
+@Disabled
 public class ImadcnIdentifierGeneratorTest {
 
     private static ImadcnIdentifierGenerator generator;

+ 1 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/activerecord/Model.java

@@ -263,6 +263,7 @@ public abstract class Model<T extends Model<?>> implements Serializable {
      * @param sqlMethod sqlMethod
      */
     protected String sqlStatement(String sqlMethod) {
+        //无法确定对应的mapper,只能用注入时候绑定的了。
         return SqlHelper.table(getClass()).getSqlStatement(sqlMethod);
     }
 

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

@@ -60,6 +60,8 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
 
     protected Class<?> entityClass = currentModelClass();
 
+    protected Class<?> mapperClass = currentMapperClass();
+
     /**
      * 判断数据库操作是否成功
      *
@@ -72,6 +74,10 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         return SqlHelper.retBool(result);
     }
 
+    protected Class<T> currentMapperClass() {
+        return (Class<T>) ReflectionKit.getSuperClassGenericType(getClass(), 0);
+    }
+
     protected Class<T> currentModelClass() {
         return (Class<T>) ReflectionKit.getSuperClassGenericType(getClass(), 1);
     }
@@ -102,7 +108,10 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
      *
      * @param sqlMethod ignore
      * @return ignore
+     * @see #getSqlStatement(SqlMethod)
+     * @deprecated 3.3.3
      */
+    @Deprecated
     protected String sqlStatement(SqlMethod sqlMethod) {
         return SqlHelper.table(entityClass).getSqlStatement(sqlMethod.getMethod());
     }
@@ -117,10 +126,21 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
     @Transactional(rollbackFor = Exception.class)
     @Override
     public boolean saveBatch(Collection<T> entityList, int batchSize) {
-        String sqlStatement = sqlStatement(SqlMethod.INSERT_ONE);
+        String sqlStatement = getSqlStatement(SqlMethod.INSERT_ONE);
         return executeBatch(entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity));
     }
 
+    /**
+     * 获取mapperStatementId
+     *
+     * @param sqlMethod 方法名
+     * @return 命名id
+     * @since 3.3.3
+     */
+    protected String getSqlStatement(SqlMethod sqlMethod) {
+        return SqlHelper.getSqlStatement(mapperClass, sqlMethod);
+    }
+
     /**
      * TableId 注解存在更新记录,否插入一条记录
      *
@@ -148,21 +168,21 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         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 SqlHelper.saveOrUpdateBatch(this.entityClass, this.log, entityList, batchSize, (sqlSession, entity) -> {
+        return SqlHelper.saveOrUpdateBatch(this.entityClass, this.mapperClass, this.log, entityList, batchSize, (sqlSession, entity) -> {
             Object idVal = ReflectionKit.getFieldValue(entity, keyProperty);
             return StringUtils.checkValNull(idVal)
-                || CollectionUtils.isEmpty(sqlSession.selectList(tableInfo.getSqlStatement(SqlMethod.SELECT_BY_ID.getMethod()), entity));
+                || CollectionUtils.isEmpty(sqlSession.selectList(getSqlStatement(SqlMethod.SELECT_BY_ID), entity));
         }, (sqlSession, entity) -> {
             MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
             param.put(Constants.ENTITY, entity);
-            sqlSession.update(tableInfo.getSqlStatement(SqlMethod.UPDATE_BY_ID.getMethod()), param);
+            sqlSession.update(getSqlStatement(SqlMethod.UPDATE_BY_ID), param);
         });
     }
 
     @Transactional(rollbackFor = Exception.class)
     @Override
     public boolean updateBatchById(Collection<T> entityList, int batchSize) {
-        String sqlStatement = sqlStatement(SqlMethod.UPDATE_BY_ID);
+        String sqlStatement = getSqlStatement(SqlMethod.UPDATE_BY_ID);
         return executeBatch(entityList, batchSize, (sqlSession, entity) -> {
             MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
             param.put(Constants.ENTITY, entity);
@@ -226,5 +246,5 @@ 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);
     }
-    
+
 }

+ 15 - 3
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SqlHelper.java

@@ -22,6 +22,7 @@ import com.baomidou.mybatisplus.core.toolkit.Assert;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
 import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import org.apache.ibatis.logging.Log;
 import org.apache.ibatis.reflection.ExceptionUtil;
 import org.apache.ibatis.session.ExecutorType;
@@ -233,15 +234,26 @@ public final class SqlHelper {
      * @return 操作结果
      * @since 3.4.0
      */
-    public static <E> boolean saveOrUpdateBatch(Class<?> entityClass, Log log, Collection<E> list, int batchSize, BiPredicate<SqlSession,E> predicate, BiConsumer<SqlSession, E> consumer) {
-        TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
+    public static <E> boolean saveOrUpdateBatch(Class<?> entityClass, Class<?> mapper, Log log, Collection<E> list, int batchSize, BiPredicate<SqlSession,E> predicate, BiConsumer<SqlSession, E> consumer) {
+        String sqlStatement = getSqlStatement(mapper, SqlMethod.INSERT_ONE);
         return executeBatch(entityClass, log, list, batchSize, (sqlSession, entity) -> {
             if (predicate.test(sqlSession, entity)) {
-                sqlSession.insert(tableInfo.getSqlStatement(SqlMethod.INSERT_ONE.getMethod()), entity);
+                sqlSession.insert(sqlStatement, entity);
             } else {
                 consumer.accept(sqlSession, entity);
             }
         });
     }
 
+    /**
+     * 获取mapperStatementId
+     *
+     * @param sqlMethod 方法名
+     * @return 命名id
+     * @since 3.3.3
+     */
+    public static String getSqlStatement(Class<?> mapper, SqlMethod sqlMethod) {
+        return mapper.getName() + StringPool.DOT + sqlMethod.getMethod();
+    }
+
 }