浏览代码

再优化 as 的逻辑

miemie 6 年之前
父节点
当前提交
a073c85ffc

+ 5 - 9
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/StringUtils.java

@@ -15,18 +15,14 @@
  */
 package com.baomidou.mybatisplus.core.toolkit;
 
-import static java.util.stream.Collectors.joining;
+import com.baomidou.mybatisplus.core.toolkit.sql.StringEscape;
 
 import java.nio.charset.StandardCharsets;
 import java.sql.Blob;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
+import java.util.*;
 import java.util.regex.Pattern;
 
-import com.baomidou.mybatisplus.core.toolkit.sql.StringEscape;
+import static java.util.stream.Collectors.joining;
 
 /**
  * <p>
@@ -124,8 +120,8 @@ public class StringUtils {
      * @param str 字符串
      * @return 判断结果
      */
-    public static boolean isColumnName(String str) {
-        return P_IS_CLOMUN.matcher(str).matches();
+    public static boolean isNotColumnName(String str) {
+        return !P_IS_CLOMUN.matcher(str).matches();
     }
 
     /**

+ 8 - 10
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/TableInfoHelper.java

@@ -425,22 +425,20 @@ public class TableInfoHelper {
      */
     public static boolean checkRelated(boolean underCamel, String property, String column) {
         boolean existEscape = false;
-        if (!StringUtils.isColumnName(column)) {
+        if (StringUtils.isNotColumnName(column)) {
             // 首尾有转义符,手动在注解里设置了转义符
             column = column.substring(1, column.length() - 1);
             existEscape = true;
         }
-        if (underCamel && !existEscape) {
-            /**
-             * 开启了驼峰并且没使用注解里弄上转义符,判断 property 下划线后是否与 column 相同 (全部转为小写)
-             * 相同则不需要 as ,则 related 为 false
-             */
+        if (underCamel) {
+            if (existEscape && property.toLowerCase().equals(property)) {
+                // 注解里手动加了转义符,而且字段全是小写,判断 property 与 column 转小写后是否相同
+                return !property.equals(column.toLowerCase());
+            }
+            // 开启了驼峰并且没有注解里手动加转义符,判断 property 下划线后是否与 column 相同 (全部转为小写)
             return !StringUtils.camelToUnderline(property).equals(column.toLowerCase());
         } else {
-            /**
-             * 未开启驼峰或者手动设置了转义符,判断 property 是否与 column 相同
-             * 相同则不需要 as ,则 related 为 false
-             */
+            // 未开启驼峰或者手动设置了转义符,直接判断 property 是否与 column 相同
             return !property.equals(column);
         }
     }

+ 5 - 6
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/sql/SqlUtils.java

@@ -88,12 +88,11 @@ public class SqlUtils {
      */
     public static String sqlWordConvert(DbType dbType, String val, boolean isColumn) {
         if (dbType == DbType.POSTGRE_SQL) {
-            if (isColumn && val.toLowerCase().equals(val)) {
-                // 是数据库字段,并且全小写之后和原值一样,直接返回
-                return val;
-            }
-            if (isColumn && !StringUtils.isColumnName(val)) {
-                // 是数据库字段,并且手动加了转义符,直接返回
+            if (isColumn && (StringUtils.isNotColumnName(val) || val.toLowerCase().equals(val))) {
+                // 都是数据库字段的情况下
+                // 1.手动加了转义符
+                // 2.全小写之后和原值一样
+                // 都直接返回
                 return val;
             }
             return String.format("\"%s\"", val);

+ 1 - 1
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/EncryptTest.java

@@ -26,7 +26,7 @@ public class EncryptTest {
     public void other() {
         System.out.println(TableInfoHelper.checkRelated(true, "order", "'order'"));
         System.out.println(TableInfoHelper.checkRelated(true, "order", "order"));
-        System.out.println(TableInfoHelper.checkRelated(true, "orderFile", "'order_file'"));
+        System.out.println(TableInfoHelper.checkRelated(true, "orderFile", "'ORDER_FILE'"));
     }
 
     @Test

+ 16 - 16
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MysqlTestDataMapperTest.java

@@ -66,14 +66,14 @@ public class MysqlTestDataMapperTest {
     }
 
     @Test
-    public void b_deleteById() {
+    public void b1_deleteById() {
         Assert.assertEquals(1, commonMapper.deleteById(1L));
         Assert.assertEquals(1, commonLogicMapper.deleteById(1L));
         Assert.assertEquals(1, mysqlMapper.deleteById(1L));
     }
 
     @Test
-    public void c_deleteByMap() {
+    public void b2_deleteByMap() {
         Map<String, Object> map = new HashMap<>();
         map.put("id", 2L);
         map.put("test_int", 5);
@@ -86,7 +86,7 @@ public class MysqlTestDataMapperTest {
     }
 
     @Test
-    public void d_delete() {
+    public void b3_delete() {
         Assert.assertEquals(1, commonMapper.delete(new QueryWrapper<CommonData>().lambda()
             .eq(CommonData::getId, 2L)
             .eq(CommonData::getTestInt, 2)));
@@ -99,7 +99,7 @@ public class MysqlTestDataMapperTest {
     }
 
     @Test
-    public void e_deleteBatchIds() {
+    public void b4_deleteBatchIds() {
         List<Long> ids = Arrays.asList(3L, 4L);
         Assert.assertEquals(2, commonMapper.deleteBatchIds(ids));
         Assert.assertEquals(2, commonLogicMapper.deleteBatchIds(ids));
@@ -107,20 +107,20 @@ public class MysqlTestDataMapperTest {
     }
 
     @Test
-    public void f_updateById() {
-        Assert.assertEquals(1, commonMapper.updateById(new CommonData().setId(5L).setTestInt(555)));
+    public void c1_updateById() {
+        Assert.assertEquals(1, commonMapper.updateById(new CommonData().setId(5L).setTestInt(555).setVersion(0)));
         Assert.assertEquals(1, commonLogicMapper.updateById(new CommonLogicData().setId(5L).setTestInt(555)));
         Assert.assertEquals(1, mysqlMapper.updateById(new MysqlData().setId(5L).setOrder(555)));
     }
 
     @Test
-    public void g_optimisticUpdateById() {
+    public void c2_optimisticUpdateById() {
         Assert.assertEquals(1, commonMapper.updateById(new CommonData().setId(5L).setTestInt(556)
             .setVersion(0)));
     }
 
     @Test
-    public void h_update() {
+    public void c3_update() {
         Assert.assertEquals(1, commonMapper.update(
             new CommonData().setTestInt(666),
             new UpdateWrapper<CommonData>().lambda().eq(CommonData::getId, 6L)
@@ -136,12 +136,12 @@ public class MysqlTestDataMapperTest {
     }
 
     @Test
-    public void i_getAllNoTenant() {
+    public void d1_getAllNoTenant() {
         commonMapper.getAllNoTenant();
     }
 
     @Test
-    public void j_selectById() {
+    public void d2_selectById() {
         long id = 6L;
         Assert.assertNotNull(commonMapper.selectById(id));
         Assert.assertNotNull(commonLogicMapper.selectById(id));
@@ -149,7 +149,7 @@ public class MysqlTestDataMapperTest {
     }
 
     @Test
-    public void k_selectBatchIds() {
+    public void d3_selectBatchIds() {
         List<Long> ids = Arrays.asList(7L, 8L);
         Assert.assertTrue(CollectionUtils.isNotEmpty(commonMapper.selectBatchIds(ids)));
         Assert.assertTrue(CollectionUtils.isNotEmpty(commonLogicMapper.selectBatchIds(ids)));
@@ -157,7 +157,7 @@ public class MysqlTestDataMapperTest {
     }
 
     @Test
-    public void l_selectByMap() {
+    public void d4_selectByMap() {
         Map<String, Object> map = new HashMap<>();
         map.put("id", 9L);
         map.put("test_int", 9);
@@ -170,7 +170,7 @@ public class MysqlTestDataMapperTest {
     }
 
     @Test
-    public void m_selectOne() {
+    public void d5_selectOne() {
         Assert.assertNotNull(commonMapper.selectOne(new QueryWrapper<CommonData>().lambda()
             .eq(CommonData::getId, 10L).eq(CommonData::getTestInt, 10)));
         Assert.assertNotNull(commonLogicMapper.selectOne(new QueryWrapper<CommonLogicData>().lambda()
@@ -180,7 +180,7 @@ public class MysqlTestDataMapperTest {
     }
 
     @Test
-    public void n_selectList() {
+    public void d6_selectList() {
         Assert.assertTrue(CollectionUtils.isNotEmpty(commonMapper.selectList(new QueryWrapper<CommonData>()
             .lambda().eq(CommonData::getTestInt, 10))));
         Assert.assertTrue(CollectionUtils.isNotEmpty(commonLogicMapper.selectList(new QueryWrapper<CommonLogicData>()
@@ -190,7 +190,7 @@ public class MysqlTestDataMapperTest {
     }
 
     @Test
-    public void o_selectPage() {
+    public void d7_selectPage() {
         IPage<CommonData> page = new Page<>(1, 5);
         IPage<CommonData> dataPage = commonMapper.selectPage(page, null);
         Assert.assertSame(dataPage, page);
@@ -217,7 +217,7 @@ public class MysqlTestDataMapperTest {
     }
 
     @Test
-    public void p_testApply() {
+    public void d8_testApply() {
         Assert.assertTrue(CollectionUtils.isNotEmpty(commonMapper.selectList(new QueryWrapper<CommonData>()
             .apply("test_int = 12"))));
         Assert.assertTrue(CollectionUtils.isNotEmpty(commonLogicMapper.selectList(new QueryWrapper<CommonLogicData>()

+ 15 - 15
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/postgres/PostgresTestDataMapperTest.java

@@ -65,14 +65,14 @@ public class PostgresTestDataMapperTest {
     }
 
     @Test
-    public void b_deleteById() {
+    public void b1_deleteById() {
         Assert.assertEquals(1, commonMapper.deleteById(1L));
         Assert.assertEquals(1, commonLogicMapper.deleteById(1L));
         Assert.assertEquals(1, pgMapper.deleteById(1L));
     }
 
     @Test
-    public void c_deleteByMap() {
+    public void b2_deleteByMap() {
         Map<String, Object> map = new HashMap<>();
         map.put("id", 2L);
         map.put("test_int", 5);
@@ -86,7 +86,7 @@ public class PostgresTestDataMapperTest {
     }
 
     @Test
-    public void d_delete() {
+    public void b3_delete() {
         Assert.assertEquals(1, commonMapper.delete(new QueryWrapper<CommonData>().lambda()
             .eq(CommonData::getId, 2L)
             .eq(CommonData::getTestInt, 2)));
@@ -99,7 +99,7 @@ public class PostgresTestDataMapperTest {
     }
 
     @Test
-    public void e_deleteBatchIds() {
+    public void b4_deleteBatchIds() {
         List<Long> ids = Arrays.asList(3L, 4L);
         Assert.assertEquals(2, commonMapper.deleteBatchIds(ids));
         Assert.assertEquals(2, commonLogicMapper.deleteBatchIds(ids));
@@ -107,20 +107,20 @@ public class PostgresTestDataMapperTest {
     }
 
     @Test
-    public void f_updateById() {
+    public void c1_updateById() {
         Assert.assertEquals(1, commonMapper.updateById(new CommonData().setId(5L).setTestInt(555)));
         Assert.assertEquals(1, commonLogicMapper.updateById(new CommonLogicData().setId(5L).setTestInt(555)));
         Assert.assertEquals(1, pgMapper.updateById(new PgData().setId(5L).setGroup(555).setPgInt(555)));
     }
 
     @Test
-    public void g_optimisticUpdateById() {
+    public void c2_optimisticUpdateById() {
         Assert.assertEquals(1, commonMapper.updateById(new CommonData().setId(5L).setTestInt(556)
             .setVersion(0)));
     }
 
     @Test
-    public void h_update() {
+    public void c3_update() {
         Assert.assertEquals(1, commonMapper.update(
             new CommonData().setTestInt(666),
             new UpdateWrapper<CommonData>().lambda().eq(CommonData::getId, 6L)
@@ -136,12 +136,12 @@ public class PostgresTestDataMapperTest {
     }
 
     @Test
-    public void i_getAllNoTenant() {
+    public void d1_getAllNoTenant() {
         commonMapper.getAllNoTenant();
     }
 
     @Test
-    public void j_selectById() {
+    public void d2_selectById() {
         long id = 6L;
         Assert.assertNotNull(commonMapper.selectById(id));
         Assert.assertNotNull(commonLogicMapper.selectById(id));
@@ -149,7 +149,7 @@ public class PostgresTestDataMapperTest {
     }
 
     @Test
-    public void k_selectBatchIds() {
+    public void d3_selectBatchIds() {
         List<Long> ids = Arrays.asList(7L, 8L);
         Assert.assertTrue(CollectionUtils.isNotEmpty(commonMapper.selectBatchIds(ids)));
         Assert.assertTrue(CollectionUtils.isNotEmpty(commonLogicMapper.selectBatchIds(ids)));
@@ -157,7 +157,7 @@ public class PostgresTestDataMapperTest {
     }
 
     @Test
-    public void l_selectByMap() {
+    public void d4_selectByMap() {
         Map<String, Object> map = new HashMap<>();
         map.put("id", 9L);
         map.put("test_int", 9);
@@ -171,7 +171,7 @@ public class PostgresTestDataMapperTest {
     }
 
     @Test
-    public void m_selectOne() {
+    public void d5_selectOne() {
         Assert.assertNotNull(commonMapper.selectOne(new QueryWrapper<CommonData>().lambda()
             .eq(CommonData::getId, 10L).eq(CommonData::getTestInt, 10)));
         Assert.assertNotNull(commonLogicMapper.selectOne(new QueryWrapper<CommonLogicData>().lambda()
@@ -181,7 +181,7 @@ public class PostgresTestDataMapperTest {
     }
 
     @Test
-    public void n_selectList() {
+    public void d6_selectList() {
         Assert.assertTrue(CollectionUtils.isNotEmpty(commonMapper.selectList(new QueryWrapper<CommonData>()
             .lambda().eq(CommonData::getTestInt, 10))));
         Assert.assertTrue(CollectionUtils.isNotEmpty(commonLogicMapper.selectList(new QueryWrapper<CommonLogicData>()
@@ -191,7 +191,7 @@ public class PostgresTestDataMapperTest {
     }
 
     @Test
-    public void o_selectPage() {
+    public void d7_selectPage() {
         IPage<CommonData> page = new Page<>(1, 5);
         IPage<CommonData> dataPage = commonMapper.selectPage(page, null);
         Assert.assertSame(dataPage, page);
@@ -219,7 +219,7 @@ public class PostgresTestDataMapperTest {
     }
 
     @Test
-    public void p_testApply() {
+    public void d8_testApply() {
         Assert.assertTrue(CollectionUtils.isNotEmpty(commonMapper.selectList(new QueryWrapper<CommonData>()
             .apply("test_int = 12"))));
         Assert.assertTrue(CollectionUtils.isNotEmpty(commonLogicMapper.selectList(new QueryWrapper<CommonLogicData>()