Class SelectSqlBuilder

java.lang.Object
cn.dinodev.spring.data.sql.builder.WhereSql<SelectSqlBuilder>
cn.dinodev.spring.data.sql.builder.SelectSqlBuilder
All Implemented Interfaces:
SqlBuilder

public class SelectSqlBuilder extends WhereSql<SelectSqlBuilder>
Author:
Cody Lu
  • Field Details

    • distinct

      protected boolean distinct
    • columns

      protected List<String> columns
    • joins

      protected List<cn.dinodev.spring.data.sql.builder.SelectSqlBuilder.JoinEntity<String>> joins
    • groupBys

      protected List<String> groupBys
    • havings

      protected List<String> havings
    • unions

      protected List<cn.dinodev.spring.data.sql.builder.SelectSqlBuilder.JoinEntity<SelectSqlBuilder>> unions
    • orderBys

      protected List<String> orderBys
    • havingParams

      protected List<Object> havingParams
    • joinParams

      protected List<Object> joinParams
  • Constructor Details

    • SelectSqlBuilder

      public SelectSqlBuilder(Dialect dialect)
    • SelectSqlBuilder

      public SelectSqlBuilder(Dialect dialect, String table)
      根据表名构造,表名,如下写法都是合法的:

      - "table1"

      - "table1, table2"

      - "table1 as t1"

      - "table1 as t1 join table2 as t2 on t1.id=t2.id"

      Parameters:
      table -
    • SelectSqlBuilder

      public SelectSqlBuilder(Dialect dialect, String table, String alias)
      设置表名,更设置表的别名

      - 生成的sql片段为:table AS alias

      Parameters:
      table -
      alias -
    • SelectSqlBuilder

      public SelectSqlBuilder(SelectSqlBuilder subQuery, String alias)
      带子查询的SqlBuilder
      Parameters:
      subQuery - 子查询
      alias - 子查询的别名
  • Method Details

    • column

      public SelectSqlBuilder column(String name)
      添加查询列信息,可以逐个添加,也可以添加多个,用逗号隔开,如下用法都是合法的

      - column("col1, col2, 'abc' as col3");

      - column("col1").column("col2").("col3, col4");

      Parameters:
      name -
      Returns:
    • columns

      public SelectSqlBuilder columns(String... names)
      添加多个查询列,每个参数可以是一个col,可以是多个,如下用法都是合法的:

      - columns("col1", "col2", "'abc' as col3");

      - columns("col1, col2", "col3"); - columns("col1").clumns("col2", "col3")

      Parameters:
      names -
      Returns:
    • groupBy

      public SelectSqlBuilder groupBy(String... expr)
      添加分组表达式,如下用法都是合法的

      - groupBy("col1", "col2");

      - groupBy("col1, col2");

      - groupBy("col1").groupBy("col2");

      Parameters:
      expr -
      Returns:
    • orderBy

      public SelectSqlBuilder orderBy(String... expr)
      添加 ORDER BY 排序表达式,如下用法都是合法的:

      - orderBy("col1 desc", "col2");

      - orderBy("col1, col2");

      - orderBy("col1").orderBy("col2 desc");

      Parameters:
      expr -
      Returns:
    • orderBy

      public SelectSqlBuilder orderBy(String name, boolean ascending)
      添加 ORDER BY 排序表达式,并指明是否 ASC 升序,如下用法都是合法的:

      - orderBy("col1", true); 则为 ORDER BY col1 ASC

      - orderBy("col2", false); 则为 ORDER BY col2 DESC

      Parameters:
      name - Name of the column by which to sort.
      ascending - If true, specifies the direction "asc", otherwise, specifies

      the direction "desc".

    • having

      public SelectSqlBuilder having(String expr)
      having条件表达式

      - having("cnt > 10");

      多个条件用 AND 连接

      Parameters:
      expr -
      Returns:
    • having

      public SelectSqlBuilder having(String expr, Object... values)
      having条件表达式,带参数.

      - having("cnt > ? or type = ?", n, type);

      多个条件用 AND 连接

      Parameters:
      expr -
      values -
      Returns:
    • union

      public SelectSqlBuilder union(SelectSqlBuilder selectSql)
      与另一个查询,做 UNION 连接

      若使用 UNION ALL 请使用 unionAll(SelectSqlBuilder)

      Parameters:
      selectSql -
      Returns:
    • unionAll

      public SelectSqlBuilder unionAll(SelectSqlBuilder selectSql)
      与另一个查询,做 UNION ALL 连接

      若使用 UNION 请使用 union(SelectSqlBuilder)

      Parameters:
      selectSql -
      Returns:
    • join

      public SelectSqlBuilder join(String joinExpr)
      JOIN 内连接表,表达式,如下写法都是合法的:

      - join("table2")

      - join("table2 AS t2")

      - join("table2 AS t2 ON t1.id = t2.classId")

      Parameters:
      joinExpr -
      Returns:
    • join

      public SelectSqlBuilder join(String table, String alias)
      JOIN 内连接表,并给表指定别名:

      - join("table2", "t2")

      生成的sql为:JOIN table2 AS t2

      Parameters:
      table -
      alias -
      Returns:
    • join

      public SelectSqlBuilder join(String table, String alias, String onExpr, Object... values)
      JOIN 内连接表,并给表指定别名和连接条件表达式:

      - join("table2", "t2", "t1.id=t2.classId AND t2.status=2")

      生成的sql为:JOIN table2 AS t2 ON AND t2.status=2

      Parameters:
      table -
      alias -
      onExpr -
      values - 参数
      Returns:
    • leftJoin

      public SelectSqlBuilder leftJoin(String joinExpr)
      LEFT JOIN 左连接表,表达式,如下写法都是合法的:

      - leftJoin("table2")

      - leftJoin("table2 AS t2")

      - leftJoin("table2 AS t2 ON t1.id = t2.classId")

      Parameters:
      joinExpr -
      Returns:
    • leftJoin

      public SelectSqlBuilder leftJoin(String table, String alias)
      LEFT JOIN 左连接表,并给表指定别名:

      - leftJoin("table2", "t2")

      生成的sql为:LEFT JOIN table2 AS t2

      Parameters:
      table -
      alias -
      Returns:
    • leftJoin

      public SelectSqlBuilder leftJoin(String table, String alias, String onExpr, Object... values)
      LEFT JOIN 左连接表,并给表指定别名和连接条件表达式:

      - leftJoin("table2", "t2", "t1.id=t2.classId AND t2.status=2")

      生成的sql为:LEFT JOIN table2 AS t2 ON AND t2.status=2

      Parameters:
      table -
      alias -
      onExpr -
      values - 参数
      Returns:
    • rightJoin

      public SelectSqlBuilder rightJoin(String joinExpr)
      RIGHT JOIN 右连接表,表达式,如下写法都是合法的:

      - rightJoin("table2")

      - rightJoin("table2 AS t2")

      - rightJoin("table2 AS t2 ON t1.id = t2.classId")

      Parameters:
      joinExpr -
      Returns:
    • rightJoin

      public SelectSqlBuilder rightJoin(String table, String alias)
      RIGHT JOIN 左连接表,并给表指定别名:

      - rightJoin("table2", "t2")

      生成的sql为:RIGHT JOIN table2 AS t2

      Parameters:
      table -
      alias -
      Returns:
    • rightJoin

      public SelectSqlBuilder rightJoin(String table, String alias, String onExpr, Object... values)
      RIGHT JOIN 右连接表,并给表指定别名和连接条件表达式:

      - rightJoin("table2", "t2", "t1.id=t2.classId AND t2.status=2")

      生成的sql为:RIGHT JOIN table2 AS t2 ON AND t2.status=2

      Parameters:
      table -
      alias -
      onExpr -
      values - 参数
      Returns:
    • crossJoin

      public SelectSqlBuilder crossJoin(String joinExpr)
      CROSS JOIN 交叉连接,cross join不可以加on

      - crossJoin("table2")

      生成的sql为:CROSS JOIN table2 OR

      - crossJoin("jsonb_array_elements(knowledge)")

      生成的sql为:CROSS JOIN jsonb_array_elements(knowledge)

      Parameters:
      joinExpr -
      Returns:
    • crossJoin

      public SelectSqlBuilder crossJoin(String joinExpr, String alias)
      CROSS JOIN 交叉连接,cross join不可以加on

      - crossJoin("table2", "t2")

      生成的sql为:CROSS JOIN table2 AS t2 OR

      - crossJoin("jsonb_array_elements(knowledge)", "value")

      生成的sql为:CROSS JOIN jsonb_array_elements(knowledge) AS value

      Parameters:
      joinExpr -
      alias -
      Returns:
    • distinct

      public SelectSqlBuilder distinct()
      声明为distinct查询
      Returns:
    • limit

      public SelectSqlBuilder limit(int limit)
      使用 LIMIT 限制查询条数,生成的SQL语句如下:

      LIMIT [limit]

      Parameters:
      limit -
      Returns:
    • limit

      public SelectSqlBuilder limit(int limit, long offset)
      使用 LIMIT 限制查询条数,生成的SQL语句如下:

      LIMIT [offset], [limit]

      使用 OFFSET 关键字,请用 #limitOffset(int, long)

      Parameters:
      limit -
      offset -
      Returns:
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getSql

      public String getSql()
      Description copied from interface: SqlBuilder
      获取生成的sql语句
      Returns:
    • getCountSql

      public String getCountSql()
    • getParams

      public Object[] getParams()
      Description copied from interface: SqlBuilder
      获取sql语句需要的参数数组
      Returns: