瀏覽代碼

test 以及租户插件解析sql遇到多表必须给表起别名

miemie 2 年之前
父節點
當前提交
d3cfdb368b

+ 2 - 0
build.gradle

@@ -175,6 +175,8 @@ subprojects {
         dependsOn("cleanTest", "generatePomFileForMavenJavaPublication")
         useJUnitPlatform()
         exclude("**/phoenix/**")
+        exclude("**/postgresql/**")
+        exclude("**/generator/**")
     }
 
     task javadocJar(type: Jar) {

+ 3 - 6
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/TenantLineInnerInterceptor.java

@@ -209,14 +209,11 @@ public class TenantLineInnerInterceptor extends BaseMultiTableInnerInterceptor i
      */
     protected Column getAliasColumn(Table table) {
         StringBuilder column = new StringBuilder();
-        // 禁止 `为了兼容隐式内连接,没有别名时条件就需要加上表名`
-        // 该起别名就要起别名
+        // todo 该起别名就要起别名,禁止修改此处逻辑
         if (table.getAlias() != null) {
-            column.append(table.getAlias().getName());
-        } else {
-            column.append(table.getName());
+            column.append(table.getAlias().getName()).append(StringPool.DOT);
         }
-        column.append(StringPool.DOT).append(tenantLineHandler.getTenantIdColumn());
+        column.append(tenantLineHandler.getTenantIdColumn());
         return new Column(column.toString());
     }
 

+ 8 - 8
mybatis-plus-extension/src/test/java/com/baomidou/mybatisplus/extension/plugins/inner/TenantLineInnerInterceptorTest.java

@@ -51,7 +51,7 @@ class TenantLineInnerInterceptorTest {
             "INSERT INTO entity (id, name, tenant_id) VALUES (?, ?, ?)");
         // insert into select
         assertSql("insert into entity (id,name) select id,name from entity2",
-            "INSERT INTO entity (id, name, tenant_id) SELECT id, name, tenant_id FROM entity2 WHERE entity2.tenant_id = 1");
+            "INSERT INTO entity (id, name, tenant_id) SELECT id, name, tenant_id FROM entity2 WHERE tenant_id = 1");
 
         assertSql("insert into entity (id,name) select * from entity2 e2",
             "INSERT INTO entity (id, name, tenant_id) SELECT * FROM entity2 e2 WHERE e2.tenant_id = 1");
@@ -69,30 +69,30 @@ class TenantLineInnerInterceptorTest {
     @Test
     void delete() {
         assertSql("delete from entity where id = ?",
-            "DELETE FROM entity WHERE entity.tenant_id = 1 AND id = ?");
+            "DELETE FROM entity WHERE id = ? AND tenant_id = 1");
     }
 
     @Test
     void update() {
         assertSql("update entity set name = ? where id = ?",
-            "UPDATE entity SET name = ? WHERE entity.tenant_id = 1 AND id = ?");
+            "UPDATE entity SET name = ? WHERE id = ? AND tenant_id = 1");
     }
 
     @Test
     void selectSingle() {
         // 单表
         assertSql("select * from entity where id = ?",
-            "SELECT * FROM entity WHERE id = ? AND entity.tenant_id = 1");
+            "SELECT * FROM entity WHERE id = ? AND tenant_id = 1");
 
         assertSql("select * from entity where id = ? or name = ?",
-            "SELECT * FROM entity WHERE (id = ? OR name = ?) AND entity.tenant_id = 1");
+            "SELECT * FROM entity WHERE (id = ? OR name = ?) AND tenant_id = 1");
 
         assertSql("SELECT * FROM entity WHERE (id = ? OR name = ?)",
-            "SELECT * FROM entity WHERE (id = ? OR name = ?) AND entity.tenant_id = 1");
+            "SELECT * FROM entity WHERE (id = ? OR name = ?) AND tenant_id = 1");
 
         /* not */
         assertSql("SELECT * FROM entity WHERE not (id = ? OR name = ?)",
-            "SELECT * FROM entity WHERE NOT (id = ? OR name = ?) AND entity.tenant_id = 1");
+            "SELECT * FROM entity WHERE NOT (id = ? OR name = ?) AND tenant_id = 1");
 
         assertSql("SELECT * FROM entity u WHERE not (u.id = ? OR u.name = ?)",
             "SELECT * FROM entity u WHERE NOT (u.id = ? OR u.name = ?) AND u.tenant_id = 1");
@@ -430,7 +430,7 @@ class TenantLineInnerInterceptorTest {
     @Test
     void selectWithAs() {
         assertSql("with with_as_A as (select * from entity) select * from with_as_A",
-            "WITH with_as_A AS (SELECT * FROM entity WHERE entity.tenant_id = 1) SELECT * FROM with_as_A");
+            "WITH with_as_A AS (SELECT * FROM entity WHERE tenant_id = 1) SELECT * FROM with_as_A");
     }
 
 

+ 1 - 2
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/BaseDbTest.java

@@ -20,7 +20,6 @@ import org.apache.ibatis.session.SqlSessionFactory;
 import org.apache.ibatis.transaction.managed.ManagedTransactionFactory;
 import org.apache.ibatis.type.TypeReference;
 import org.h2.Driver;
-import org.jetbrains.annotations.Nullable;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.jdbc.datasource.SimpleDriverDataSource;
 
@@ -101,7 +100,7 @@ public abstract class BaseDbTest<T> extends TypeReference<T> {
         return dataSource;
     }
 
-    protected SqlSession sqlSession(@Nullable ExecutorType type) {
+    protected SqlSession sqlSession(ExecutorType type) {
         return sqlSessionFactory.openSession(type);
     }
 

+ 7 - 12
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/toolkit/DbTest.java

@@ -1,18 +1,7 @@
 package com.baomidou.mybatisplus.test.toolkit;
 
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.ibatis.plugin.Interceptor;
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Test;
-
 import com.baomidou.mybatisplus.annotation.DbType;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
-import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
@@ -26,6 +15,12 @@ import com.baomidou.mybatisplus.extension.toolkit.Db;
 import com.baomidou.mybatisplus.test.BaseDbTest;
 import com.baomidou.mybatisplus.test.sqlrunner.Entity;
 import com.baomidou.mybatisplus.test.sqlrunner.EntityMapper;
+import org.apache.ibatis.exceptions.TooManyResultsException;
+import org.apache.ibatis.plugin.Interceptor;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.*;
 
 /**
  * 以静态方式调用Service中的函数
@@ -153,7 +148,7 @@ class DbTest extends BaseDbTest<EntityMapper> {
     @Test
     void testGetOne() {
         LambdaQueryWrapper<Entity> wrapper = Wrappers.lambdaQuery(Entity.class);
-        Assertions.assertThrows(MybatisPlusException.class, () -> Db.getOne(wrapper));
+        Assertions.assertThrows(TooManyResultsException.class, () -> Db.getOne(wrapper));
         Entity one = Db.getOne(wrapper, false);
         Assertions.assertNotNull(one);
     }