miemie 5 år sedan
förälder
incheckning
964cdaeb53

+ 2 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/handlers/MybatisEnumTypeHandler.java

@@ -43,7 +43,7 @@ import java.util.concurrent.ConcurrentHashMap;
  * @author hubin
  * @since 2017-10-11
  */
-public class MybatisEnumTypeHandler<E extends Enum<?>> extends BaseTypeHandler<Enum<?>> {
+public class MybatisEnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {
 
     private static final Map<String, String> TABLE_METHOD_OF_ENUM_TYPES = new ConcurrentHashMap<>();
     private static ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
@@ -109,7 +109,7 @@ public class MybatisEnumTypeHandler<E extends Enum<?>> extends BaseTypeHandler<E
 
     @SuppressWarnings("Duplicates")
     @Override
-    public void setNonNullParameter(PreparedStatement ps, int i, Enum<?> parameter, JdbcType jdbcType)
+    public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType)
         throws SQLException {
         if (jdbcType == null) {
             ps.setObject(i, this.getValue(parameter));

+ 10 - 2
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/tenant/TenantHandler.java

@@ -40,16 +40,24 @@ public interface TenantHandler {
 
     /**
      * 获取租户字段名
+     * <p>
+     * 默认字段名叫: tenant_id
      *
      * @return 租户字段名
      */
-    String getTenantIdColumn();
+    default String getTenantIdColumn() {
+        return "tenant_id";
+    }
 
     /**
      * 根据表名判断是否进行过滤
+     * <p>
+     * 默认都要进行解析
      *
      * @param tableName 表名
      * @return 是否进行过滤, true:表示忽略,false:需要解析多租户字段
      */
-    boolean doTableFilter(String tableName);
+    default boolean doTableFilter(String tableName) {
+        return false;
+    }
 }

+ 22 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/tenant/Entity.java

@@ -0,0 +1,22 @@
+package com.baomidou.mybatisplus.test.tenant;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+import java.io.Serializable;
+
+/**
+ * @author miemie
+ * @since 2020-06-24
+ */
+@Data
+@Accessors(chain = true)
+public class Entity implements Serializable {
+    private static final long serialVersionUID = 6389385437936113455L;
+
+    private Long id;
+
+    private String name;
+
+    private Integer tenantId;
+}

+ 12 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/tenant/EntityMapper.java

@@ -0,0 +1,12 @@
+package com.baomidou.mybatisplus.test.tenant;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.CacheNamespace;
+
+/**
+ * @author miemie
+ * @since 2020-06-24
+ */
+@CacheNamespace
+public interface EntityMapper extends BaseMapper<Entity> {
+}

+ 69 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/tenant/TenantTest.java

@@ -0,0 +1,69 @@
+package com.baomidou.mybatisplus.test.tenant;
+
+import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
+import com.baomidou.mybatisplus.extension.plugins.inner.TenantInnerInterceptor;
+import com.baomidou.mybatisplus.test.BaseDbTest;
+import net.sf.jsqlparser.expression.LongValue;
+import org.apache.ibatis.cache.Cache;
+import org.apache.ibatis.plugin.Interceptor;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * @author miemie
+ * @since 2020-06-24
+ */
+public class TenantTest extends BaseDbTest<EntityMapper> {
+
+    @Test
+    void test() {
+        Cache cache = sqlSessionFactory.getConfiguration().getCache(EntityMapper.class.getName());
+        assertThat(cache).as("使用 @CacheNamespace 指定了使用缓存").isNotNull();
+
+
+        Long id = 1L;
+        doTestAutoCommit(m -> {
+            int insert = m.insert(new Entity().setId(id));
+            assertThat(insert).as("插入成功").isEqualTo(1);
+        });
+
+
+        doTest(m -> {
+            Entity entity = m.selectById(id);
+            assertThat(entity).as("插入成功").isNotNull();
+            assertThat(entity.getTenantId()).as("有租户信息").isEqualTo(1);
+        });
+        assertThat(cache.getSize()).as("有一条缓存").isEqualTo(1);
+
+
+        doTest(m -> {
+            Entity entity = m.selectById(id);
+            assertThat(entity).as("插入成功").isNotNull();
+            assertThat(entity.getTenantId()).as("有租户信息").isEqualTo(1);
+        });
+        assertThat(cache.getSize()).as("依然只有一条缓存,命中了缓存").isEqualTo(1);
+    }
+
+    @Override
+    protected List<Interceptor> interceptors() {
+        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
+        interceptor.addInnerInterceptor(new TenantInnerInterceptor(select -> new LongValue(1)));
+        return Collections.singletonList(interceptor);
+    }
+
+    @Override
+    protected List<String> tableSql() {
+        return Arrays.asList("drop table if exists entity",
+            "CREATE TABLE IF NOT EXISTS entity (\n" +
+                "id BIGINT(20) NOT NULL,\n" +
+                "name VARCHAR(30) NULL DEFAULT NULL,\n" +
+                "tenant_id integer not NULL,\n" +
+                "PRIMARY KEY (id)" +
+                ")");
+    }
+}