ソースを参照

Merge branch 'feature_add_first_sql' of https://gitee.com/lennon-coder/mybatis-plus into 3.0

# Conflicts:
#	mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectBatchByIds.java
#	mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectById.java
miemie 5 年 前
コミット
a3729f9196
18 ファイル変更165 行追加44 行削除
  1. 21 0
      mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractWrapper.java
  2. 2 0
      mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/Wrapper.java
  3. 17 0
      mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/interfaces/Join.java
  4. 12 12
      mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/enums/SqlMethod.java
  5. 15 0
      mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java
  6. 16 5
      mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectBatchByIds.java
  7. 12 5
      mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectById.java
  8. 5 2
      mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectByMap.java
  9. 10 1
      mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectCount.java
  10. 7 3
      mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectList.java
  11. 7 3
      mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectMaps.java
  12. 7 3
      mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectMapsPage.java
  13. 7 3
      mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectObjs.java
  14. 8 3
      mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectOne.java
  15. 7 3
      mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectPage.java
  16. 4 0
      mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/Constants.java
  17. 4 0
      mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/conditions/AbstractChainWrapper.java
  18. 4 1
      mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/SelectCountDistinctTest.java

+ 21 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractWrapper.java

@@ -63,6 +63,10 @@ public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T,
      */
     protected SharedString sqlComment;
     /**
+     * SQL起始语句
+     */
+    protected SharedString sqlFirst = SharedString.emptyString();
+    /**ß
      * 数据库表映射实体类
      */
     private T entity;
@@ -232,6 +236,14 @@ public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T,
         return typedThis;
     }
 
+    @Override
+    public Children first(boolean condition, String firstSql) {
+        if(condition) {
+            this.sqlFirst.setStringValue(firstSql);
+        }
+        return typedThis;
+    }
+
     @Override
     public Children exists(boolean condition, String existsSql) {
         return doIt(condition, EXISTS, () -> String.format("(%s)", existsSql));
@@ -422,6 +434,7 @@ public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T,
         expression = new MergeSegments();
         lastSql = SharedString.emptyString();
         sqlComment = SharedString.emptyString();
+        sqlFirst = SharedString.emptyString();
     }
 
     /**
@@ -458,6 +471,14 @@ public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T,
         return null;
     }
 
+    @Override
+    public String getSqlFirst() {
+        if (StringUtils.isNotEmpty(sqlFirst.getStringValue())) {
+            return StringEscape.escapeRawString(sqlFirst.getStringValue());
+        }
+        return null;
+    }
+
     @Override
     public MergeSegments getExpression() {
         return expression;

+ 2 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/Wrapper.java

@@ -52,6 +52,8 @@ public abstract class Wrapper<T> implements ISqlSegment {
         return null;
     }
 
+    public String getSqlFirst() { return null; }
+
     /**
      * 获取 MergeSegments
      */

+ 17 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/interfaces/Join.java

@@ -94,6 +94,23 @@ public interface Join<Children> extends Serializable {
      */
     Children comment(boolean condition, String comment);
 
+    /**
+     * ignore
+     * @param firstSql
+     * @return
+     */
+    default Children first(String firstSql) {
+        return first(true, firstSql);
+    }
+
+    /**
+     * sql 起始句(会拼接在SQL语句的起始处)
+     * @param condition 执行条件
+     * @param firstSql  起始语句
+     * @return
+     */
+    Children first(boolean condition, String firstSql);
+
     /**
      * ignore
      */

+ 12 - 12
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/enums/SqlMethod.java

@@ -57,22 +57,22 @@ public enum SqlMethod {
     /**
      * 查询
      */
-    SELECT_BY_ID("selectById", "根据ID 查询一条数据", "SELECT %s FROM %s WHERE %s=#{%s}"),
-    SELECT_BY_MAP("selectByMap", "根据columnMap 查询一条数据", "<script>\nSELECT %s FROM %s %s\n</script>"),
-    SELECT_BATCH_BY_IDS("selectBatchIds", "根据ID集合,批量查询数据", "<script>\nSELECT %s FROM %s WHERE %s IN (%s)\n</script>"),
-    SELECT_ONE("selectOne", "查询满足条件一条数据", "<script>\nSELECT %s FROM %s %s %s\n</script>"),
-    SELECT_COUNT("selectCount", "查询满足条件总记录数", "<script>\nSELECT COUNT(%s) FROM %s %s %s\n</script>"),
-    SELECT_LIST("selectList", "查询满足条件所有数据", "<script>\nSELECT %s FROM %s %s %s\n</script>"),
-    SELECT_PAGE("selectPage", "查询满足条件所有数据(并翻页)", "<script>\nSELECT %s FROM %s %s %s\n</script>"),
-    SELECT_MAPS("selectMaps", "查询满足条件所有数据", "<script>\nSELECT %s FROM %s %s %s\n</script>"),
-    SELECT_MAPS_PAGE("selectMapsPage", "查询满足条件所有数据(并翻页)", "<script>\nSELECT %s FROM %s %s %s\n</script>"),
-    SELECT_OBJS("selectObjs", "查询满足条件所有数据", "<script>\nSELECT %s FROM %s %s %s\n</script>"),
+    SELECT_BY_ID("selectById", "根据ID 查询一条数据", " %s SELECT %s FROM %s WHERE %s=#{%s}"),
+    SELECT_BY_MAP("selectByMap", "根据columnMap 查询一条数据", "<script>\n %s SELECT %s FROM %s %s\n</script>"),
+    SELECT_BATCH_BY_IDS("selectBatchIds", "根据ID集合,批量查询数据", "<script>\n %s SELECT %s FROM %s WHERE %s IN (%s)\n</script>"),
+    SELECT_ONE("selectOne", "查询满足条件一条数据", "<script>\n %s SELECT %s FROM %s %s %s\n</script>"),
+    SELECT_COUNT("selectCount", "查询满足条件总记录数", "<script>\n %s SELECT COUNT(%s) FROM %s %s %s\n</script>"),
+    SELECT_LIST("selectList", "查询满足条件所有数据", "<script>\n %s SELECT %s FROM %s %s %s\n</script>"),
+    SELECT_PAGE("selectPage", "查询满足条件所有数据(并翻页)", "<script>\n %s SELECT %s FROM %s %s %s\n</script>"),
+    SELECT_MAPS("selectMaps", "查询满足条件所有数据", "<script>\n %s SELECT %s FROM %s %s %s\n</script>"),
+    SELECT_MAPS_PAGE("selectMapsPage", "查询满足条件所有数据(并翻页)", "<script>\n %s SELECT %s FROM %s %s %s\n</script>"),
+    SELECT_OBJS("selectObjs", "查询满足条件所有数据", "<script>\n %s SELECT %s FROM %s %s %s\n</script>"),
 
     /**
      * 逻辑删除 -> 查询
      */
-    LOGIC_SELECT_BY_ID("selectById", "根据ID 查询一条数据", "SELECT %s FROM %s WHERE %s=#{%s} %s"),
-    LOGIC_SELECT_BATCH_BY_IDS("selectBatchIds", "根据ID集合,批量查询数据", "<script>\nSELECT %s FROM %s WHERE %s IN (%s) %s\n</script>");
+    LOGIC_SELECT_BY_ID("selectById", "根据ID 查询一条数据", " %s SELECT %s FROM %s WHERE %s=#{%s} %s"),
+    LOGIC_SELECT_BATCH_BY_IDS("selectBatchIds", "根据ID集合,批量查询数据", "<script>\n %s SELECT %s FROM %s WHERE %s IN (%s) %s\n</script>");
 
     private final String method;
     private final String desc;

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

@@ -118,6 +118,21 @@ public abstract class AbstractMethod implements Constants {
             SqlScriptUtils.unSafeParam(Q_WRAPPER_SQL_COMMENT), EMPTY);
     }
 
+    /**
+     * SQL 注释
+     *
+     * @return sql
+     */
+    protected String sqlFirst() {
+        return SqlScriptUtils.convertChoose(
+                String.format(
+                        "%s != null and %s != null",
+                        WRAPPER,
+                        Q_WRAPPER_SQL_FIRST
+                ),
+                SqlScriptUtils.unSafeParam(Q_WRAPPER_SQL_FIRST),
+                EMPTY);
+    }
     /**
      * SQL 查询所有表字段
      *

+ 16 - 5
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectBatchByIds.java

@@ -33,10 +33,21 @@ public class SelectBatchByIds extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.LOGIC_SELECT_BATCH_BY_IDS;
-        SqlSource sqlSource = languageDriver.createSqlSource(configuration, String.format(sqlMethod.getSql(),
-            sqlSelectColumns(tableInfo, false), tableInfo.getTableName(), tableInfo.getKeyColumn(),
-            SqlScriptUtils.convertForeach("#{item}", COLLECTION, null, "item", COMMA),
-            tableInfo.getLogicDeleteSql(true, true)), Object.class);
-        return addSelectMappedStatementForTable(mapperClass, getMethod(sqlMethod), sqlSource, tableInfo);
+        SqlSource sqlSource = languageDriver.createSqlSource(configuration,
+                String.format(
+                        sqlMethod.getSql(),
+                        sqlFirst(),
+                        sqlSelectColumns(tableInfo, false),
+                        tableInfo.getTableName(),
+                        tableInfo.getKeyColumn(),
+                        SqlScriptUtils.convertForeach(
+                                "#{item}",
+                                COLLECTION,
+                                null,
+                                "item",
+                                COMMA
+                        ),
+                        tableInfo.getLogicDeleteSql(true, false)), Object.class);
+        return addSelectMappedStatementForTable(mapperClass, sqlMethod.getMethod(), sqlSource, tableInfo);
     }
 }

+ 12 - 5
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectById.java

@@ -33,10 +33,17 @@ public class SelectById extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.LOGIC_SELECT_BY_ID;
-        SqlSource sqlSource = new RawSqlSource(configuration, String.format(sqlMethod.getSql(),
-            sqlSelectColumns(tableInfo, false),
-            tableInfo.getTableName(), tableInfo.getKeyColumn(), tableInfo.getKeyProperty(),
-            tableInfo.getLogicDeleteSql(true, true)), Object.class);
-        return this.addSelectMappedStatementForTable(mapperClass, getMethod(sqlMethod), sqlSource, tableInfo);
+        SqlSource sqlSource = new RawSqlSource(configuration,
+                String.format(
+                        sqlMethod.getSql(),
+                        sqlFirst(),
+                        sqlSelectColumns(tableInfo, false),
+                        tableInfo.getTableName(),
+                        tableInfo.getKeyColumn(),
+                        tableInfo.getKeyProperty(),
+                        tableInfo.getLogicDeleteSql(true, false)
+                ),
+                Object.class);
+        return this.addSelectMappedStatementForTable(mapperClass, sqlMethod.getMethod(), sqlSource, tableInfo);
     }
 }

+ 5 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectByMap.java

@@ -34,8 +34,11 @@ public class SelectByMap extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.SELECT_BY_MAP;
-        String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, false),
-            tableInfo.getTableName(), sqlWhereByMap(tableInfo));
+        String sql = String.format(
+                sqlMethod.getSql(),
+                sqlFirst(),
+                sqlSelectColumns(tableInfo, false),
+                tableInfo.getTableName(), sqlWhereByMap(tableInfo));
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Map.class);
         return this.addSelectMappedStatementForTable(mapperClass, getMethod(sqlMethod), sqlSource, tableInfo);
     }

+ 10 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectCount.java

@@ -32,7 +32,16 @@ public class SelectCount extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.SELECT_COUNT;
-        String sql = String.format(sqlMethod.getSql(), this.sqlCount(), tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
+        String sql = String.format(
+                sqlMethod.getSql(),
+                sqlFirst(),
+                this.sqlCount(),
+                tableInfo.getTableName(),
+                sqlWhereEntityWrapper(
+                        true,
+                        tableInfo
+                ),
+                sqlComment());
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return this.addSelectMappedStatementForOther(mapperClass, getMethod(sqlMethod), sqlSource, Integer.class);
     }

+ 7 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectList.java

@@ -32,9 +32,13 @@ public class SelectList extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.SELECT_LIST;
-        String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
-            tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo),
-            sqlComment());
+        String sql = String.format(
+                sqlMethod.getSql(),
+                sqlFirst(),
+                sqlSelectColumns(tableInfo, true),
+                tableInfo.getTableName(),
+                sqlWhereEntityWrapper(true, tableInfo),
+                sqlComment());
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return this.addSelectMappedStatementForTable(mapperClass, getMethod(sqlMethod), sqlSource, tableInfo);
     }

+ 7 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectMaps.java

@@ -34,9 +34,13 @@ public class SelectMaps extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.SELECT_MAPS;
-        String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
-            tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo),
-            sqlComment());
+        String sql = String.format(
+                sqlMethod.getSql(),
+                sqlFirst(),
+                sqlSelectColumns(tableInfo, true),
+                tableInfo.getTableName(),
+                sqlWhereEntityWrapper(true, tableInfo),
+                sqlComment());
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return this.addSelectMappedStatementForOther(mapperClass, getMethod(sqlMethod), sqlSource, Map.class);
     }

+ 7 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectMapsPage.java

@@ -34,9 +34,13 @@ public class SelectMapsPage extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.SELECT_MAPS_PAGE;
-        String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
-            tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo),
-            sqlComment());
+        String sql = String.format(
+                sqlMethod.getSql(),
+                sqlFirst(),
+                sqlSelectColumns(tableInfo, true),
+                tableInfo.getTableName(),
+                sqlWhereEntityWrapper(true, tableInfo),
+                sqlComment());
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return this.addSelectMappedStatementForOther(mapperClass, getMethod(sqlMethod), sqlSource, Map.class);
     }

+ 7 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectObjs.java

@@ -32,9 +32,13 @@ public class SelectObjs extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.SELECT_OBJS;
-        String sql = String.format(sqlMethod.getSql(), sqlSelectObjsColumns(tableInfo),
-            tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo),
-            sqlComment());
+        String sql = String.format(
+                sqlMethod.getSql(),
+                sqlFirst(),
+                sqlSelectObjsColumns(tableInfo),
+                tableInfo.getTableName(),
+                sqlWhereEntityWrapper(true, tableInfo),
+                sqlComment());
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return this.addSelectMappedStatementForOther(mapperClass, getMethod(sqlMethod), sqlSource, Object.class);
     }

+ 8 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectOne.java

@@ -32,9 +32,14 @@ public class SelectOne extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.SELECT_ONE;
-        SqlSource sqlSource = languageDriver.createSqlSource(configuration, String.format(sqlMethod.getSql(),
-            this.sqlSelectColumns(tableInfo, true), tableInfo.getTableName(),
-            this.sqlWhereEntityWrapper(true, tableInfo), sqlComment()),
+        SqlSource sqlSource = languageDriver.createSqlSource(configuration,
+                String.format(
+                        sqlMethod.getSql(),
+                        sqlFirst(),
+                        this.sqlSelectColumns(tableInfo, true),
+                        tableInfo.getTableName(),
+                        this.sqlWhereEntityWrapper(true, tableInfo),
+                        sqlComment()),
             modelClass);
         return this.addSelectMappedStatementForTable(mapperClass, getMethod(sqlMethod), sqlSource, tableInfo);
     }

+ 7 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectPage.java

@@ -32,9 +32,13 @@ public class SelectPage extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.SELECT_PAGE;
-        String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
-            tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo),
-            sqlComment());
+        String sql = String.format(
+                sqlMethod.getSql(),
+                sqlFirst(),
+                sqlSelectColumns(tableInfo, true),
+                tableInfo.getTableName(),
+                sqlWhereEntityWrapper(true, tableInfo),
+                sqlComment());
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return this.addSelectMappedStatementForTable(mapperClass, getMethod(sqlMethod), sqlSource, tableInfo);
     }

+ 4 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/Constants.java

@@ -92,6 +92,10 @@ public interface Constants extends StringPool {
      * wrapper 类的属性 sqlComment
      */
     String Q_WRAPPER_SQL_COMMENT = WRAPPER_DOT + "sqlComment";
+    /**
+     * wrapper 类的属性 sqlFirst
+     */
+    String Q_WRAPPER_SQL_FIRST = WRAPPER_DOT + "sqlFirst";
     /**
      * columnMap
      */

+ 4 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/conditions/AbstractChainWrapper.java

@@ -241,6 +241,10 @@ public abstract class AbstractChainWrapper<T, R, Children extends AbstractChainW
         return typedThis;
     }
 
+    public Children first(boolean condition, String firstSql) {
+        getWrapper().first(condition, firstSql);
+        return typedThis;
+    }
     @Override
     public Children exists(boolean condition, String existsSql) {
         getWrapper().exists(condition, existsSql);

+ 4 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/SelectCountDistinctTest.java

@@ -48,7 +48,10 @@ class SelectCountDistinctTest {
     void testCountDistinct() {
         QueryWrapper<CommonData> distinct = new QueryWrapper<>();
         distinct.select("distinct test_int");
-        distinct.eq("test_int", 25).or().eq("test_str", "test");
+        distinct.eq("test_int", 25)
+                .or()
+                .eq("test_str", "test")
+                .first("/*Force Master*/");
         int count = commonDataMapper.selectCount(distinct);
         Assertions.assertEquals(1, count);
     }