Browse Source

优化调整

hubin 4 years ago
parent
commit
7e0b1e2912

+ 15 - 11
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java

@@ -105,8 +105,7 @@ public abstract class AbstractMethod implements Constants {
         }
         if (ew) {
             sqlScript += NEWLINE;
-            sqlScript += SqlScriptUtils.convertIf(SqlScriptUtils.unSafeParam(U_WRAPPER_SQL_SET),
-                String.format("%s != null and %s != null", WRAPPER, U_WRAPPER_SQL_SET), false);
+            sqlScript += convertIfEwParam(U_WRAPPER_SQL_SET, false);
         }
         sqlScript = SqlScriptUtils.convertSet(sqlScript);
         return sqlScript;
@@ -118,8 +117,7 @@ public abstract class AbstractMethod implements Constants {
      * @return sql
      */
     protected String sqlComment() {
-        return SqlScriptUtils.convertIf(String.format("%s != null and %s != null", WRAPPER, Q_WRAPPER_SQL_COMMENT),
-            SqlScriptUtils.unSafeParam(Q_WRAPPER_SQL_COMMENT), true);
+        return convertIfEwParam(Q_WRAPPER_SQL_COMMENT, true);
     }
 
     /**
@@ -128,8 +126,12 @@ public abstract class AbstractMethod implements Constants {
      * @return sql
      */
     protected String sqlFirst() {
-        return SqlScriptUtils.convertIf(String.format("%s != null and %s != null", WRAPPER, Q_WRAPPER_SQL_FIRST),
-            SqlScriptUtils.unSafeParam(Q_WRAPPER_SQL_FIRST), true);
+        return convertIfEwParam(Q_WRAPPER_SQL_FIRST, true);
+    }
+
+    protected String convertIfEwParam(final String param, final boolean newLine) {
+        return SqlScriptUtils.convertIf(SqlScriptUtils.unSafeParam(param),
+            String.format("%s != null and %s != null", WRAPPER, param), newLine);
     }
 
     /**
@@ -149,8 +151,7 @@ public abstract class AbstractMethod implements Constants {
         if (!queryWrapper) {
             return selectColumns;
         }
-        return SqlScriptUtils.convertChoose(String.format("%s != null and %s != null", WRAPPER, Q_WRAPPER_SQL_SELECT),
-            SqlScriptUtils.unSafeParam(Q_WRAPPER_SQL_SELECT), selectColumns);
+        return convertChooseEwSelect(selectColumns);
     }
 
     /**
@@ -159,8 +160,7 @@ public abstract class AbstractMethod implements Constants {
      * @return count sql 脚本
      */
     protected String sqlCount() {
-        return SqlScriptUtils.convertChoose(String.format("%s != null and %s != null", WRAPPER, Q_WRAPPER_SQL_SELECT),
-            SqlScriptUtils.unSafeParam(Q_WRAPPER_SQL_SELECT), ASTERISK);
+        return convertChooseEwSelect(ASTERISK);
     }
 
     /**
@@ -169,8 +169,12 @@ public abstract class AbstractMethod implements Constants {
      * @param table 表信息
      */
     protected String sqlSelectObjsColumns(TableInfo table) {
+        return convertChooseEwSelect(table.getAllSqlSelect());
+    }
+
+    protected String convertChooseEwSelect(final String otherwise) {
         return SqlScriptUtils.convertChoose(String.format("%s != null and %s != null", WRAPPER, Q_WRAPPER_SQL_SELECT),
-            SqlScriptUtils.unSafeParam(Q_WRAPPER_SQL_SELECT), table.getAllSqlSelect());
+            SqlScriptUtils.unSafeParam(Q_WRAPPER_SQL_SELECT), otherwise);
     }
 
     /**

+ 3 - 3
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/ActiveRecordTest.java

@@ -56,13 +56,13 @@ class ActiveRecordTest {
     @Transactional
     @Order(1)
     void testInsert() {
-        H2Student student = new H2Student(null, "测试学生", 2);
+        H2Student student = new H2Student(3L, "测试学生", 2);
         assertThat(student.insert()).isTrue();
         System.out.println(student.getId());
-//        assertThat(student.getId()).isEqualTo(1);
+        assertThat(student.getId()).isEqualTo(3);
+        student.setId(null);
         assertThat(student.insert()).isTrue();
         System.out.println(student.getId());
-//        assertThat(student.getId()).isEqualTo(2);
     }
 
     @Test

+ 1 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2Delete1Eq1Test.java

@@ -59,7 +59,7 @@ class H2Delete1Eq1Test extends BaseTest {
         h2User.setName("2");
         log(logicDeleteMapper.selectList(new QueryWrapper<>(h2User).orderByAsc("name")));
 
-        for (long i = 0; i < 10L; i++) {
+        for (long i = 30; i < 50L; i++) {
             defaultMapper.insert(new H2Student(i, "Tom长大了", 1));
         }
         log(logicDeleteMapper.selectList(new QueryWrapper<>(h2User).eq("name", "2").orderByAsc("name")));