public class SqlBuilder extends Object implements Builder<String>
Modifier and Type | Class and Description |
---|---|
static class |
SqlBuilder.Join
SQL中多表关联用的关键字
|
Constructor and Description |
---|
SqlBuilder() |
SqlBuilder(Wrapper wrapper) |
Modifier and Type | Method and Description |
---|---|
SqlBuilder |
addParams(Object... params)
手动增加参数,调用此方法前需确认SQL中有对应占位符,主要用于自定义SQL片段中有占位符的情况,例如:
SqlBuilder builder = SqlBuilder.of("select * from user where id=?")
|
SqlBuilder |
append(Object sqlFragment)
追加SQL其它部分片段,此方法只是简单的追加SQL字符串,空格需手动加入,例如:
SqlBuilder builder = SqlBuilder.of("select *");
builder.append(" from ").append("user");
如果需要追加带占位符的片段,需调用
addParams(Object...) |
String |
build()
构建,默认打印SQL日志
|
static SqlBuilder |
create()
创建SQL构建器
|
static SqlBuilder |
create(Wrapper wrapper)
创建SQL构建器
|
SqlBuilder |
delete(String tableName)
删除
|
SqlBuilder |
format()
格式化SQL语句
|
SqlBuilder |
from(String... tableNames)
添加 from语句
|
Object[] |
getParamValueArray()
获得占位符对应的值列表
|
List<Object> |
getParamValues()
获得占位符对应的值列表
|
SqlBuilder |
groupBy(String... fields)
分组
|
SqlBuilder |
having(Condition... conditions)
添加Having语句,所有逻辑之间关系使用
Condition.setLinkOperator(LogicalOperator) 定义 |
SqlBuilder |
having(String having)
添加Having语句
|
<T> SqlBuilder |
in(String field,
T... values)
多值选择
|
SqlBuilder |
insert(Entity entity)
插入,使用默认的ANSI方言
|
SqlBuilder |
insert(Entity entity,
DialectName dialectName)
插入
插入会忽略空的字段名及其对应值,但是对于有字段名对应值为 null 的情况不忽略 |
SqlBuilder |
insert(Entity entity,
String dialectName)
插入
插入会忽略空的字段名及其对应值,但是对于有字段名对应值为 null 的情况不忽略 |
SqlBuilder |
insertPreFragment(Object sqlFragment)
在SQL的开头补充SQL片段
|
SqlBuilder |
join(String tableName,
SqlBuilder.Join join)
多表关联
|
static SqlBuilder |
of(CharSequence sql)
从已有的SQL中构建一个SqlBuilder
|
SqlBuilder |
on(Condition... conditions)
配合JOIN的 ON语句,多表关联的条件语句,所有逻辑之间关系使用
Condition.setLinkOperator(LogicalOperator) 定义 |
SqlBuilder |
on(String on)
配合JOIN的 ON语句,多表关联的条件语句
只支持单一的逻辑运算符(例如多个条件之间) |
SqlBuilder |
orderBy(Order... orders)
排序
|
SqlBuilder |
query(Query query)
构建查询SQL
|
SqlBuilder |
select(boolean isDistinct,
Collection<String> fields)
查询
|
SqlBuilder |
select(boolean isDistinct,
String... fields)
查询
|
SqlBuilder |
select(Collection<String> fields)
查询(非Distinct)
|
SqlBuilder |
select(String... fields)
查询(非Distinct)
|
String |
toString() |
SqlBuilder |
update(Entity entity)
更新
|
static void |
validateEntity(Entity entity)
验证实体类对象的有效性
|
SqlBuilder |
where(Condition... conditions)
添加Where语句,所有逻辑之间关系使用
Condition.setLinkOperator(LogicalOperator) 定义 |
SqlBuilder |
where(String where)
添加Where语句
|
public SqlBuilder()
public SqlBuilder(Wrapper wrapper)
public static SqlBuilder create()
public static SqlBuilder create(Wrapper wrapper)
wrapper
- 包装器public static SqlBuilder of(CharSequence sql)
sql
- SQL语句public static void validateEntity(Entity entity) throws DbRuntimeException
entity
- 实体类对象DbRuntimeException
- SQL异常包装,获取元数据信息失败public SqlBuilder insert(Entity entity)
entity
- 实体public SqlBuilder insert(Entity entity, DialectName dialectName)
null
的情况不忽略entity
- 实体dialectName
- 方言名,用于对特殊数据库特殊处理public SqlBuilder insert(Entity entity, String dialectName)
null
的情况不忽略entity
- 实体dialectName
- 方言名,用于对特殊数据库特殊处理public SqlBuilder delete(String tableName)
tableName
- 表名public SqlBuilder update(Entity entity)
entity
- 要更新的实体public SqlBuilder select(boolean isDistinct, String... fields)
isDistinct
- 是否添加DISTINCT关键字(查询唯一结果)fields
- 查询的字段public SqlBuilder select(boolean isDistinct, Collection<String> fields)
isDistinct
- 是否添加DISTINCT关键字(查询唯一结果)fields
- 查询的字段public SqlBuilder select(String... fields)
fields
- 查询的字段public SqlBuilder select(Collection<String> fields)
fields
- 查询的字段public SqlBuilder from(String... tableNames)
tableNames
- 表名列表(多个表名用于多表查询)public SqlBuilder where(Condition... conditions)
Condition.setLinkOperator(LogicalOperator)
定义conditions
- 条件,当条件为空时,只添加WHERE关键字public SqlBuilder where(String where)
where
- WHERE语句之后跟的条件语句字符串public <T> SqlBuilder in(String field, T... values)
T
- 值类型field
- 字段名values
- 值列表public SqlBuilder groupBy(String... fields)
fields
- 字段public SqlBuilder having(Condition... conditions)
Condition.setLinkOperator(LogicalOperator)
定义conditions
- 条件public SqlBuilder having(String having)
having
- 条件语句public SqlBuilder orderBy(Order... orders)
orders
- 排序对象public SqlBuilder join(String tableName, SqlBuilder.Join join)
tableName
- 被关联的表名join
- 内联方式public SqlBuilder on(Condition... conditions)
Condition.setLinkOperator(LogicalOperator)
定义conditions
- 条件public SqlBuilder on(String on)
on
- 条件public SqlBuilder insertPreFragment(Object sqlFragment)
sqlFragment
- SQL片段public SqlBuilder append(Object sqlFragment)
SqlBuilder builder = SqlBuilder.of("select *"); builder.append(" from ").append("user");
如果需要追加带占位符的片段,需调用addParams(Object...)
方法加入对应参数值。
sqlFragment
- SQL其它部分片段public SqlBuilder addParams(Object... params)
SqlBuilder builder = SqlBuilder.of("select * from user where id=?"); builder.append(" and name=?") builder.addParams(1, "looly");
params
- 参数列表public SqlBuilder query(Query query)
query
- Query
public Object[] getParamValueArray()
public String build()
public SqlBuilder format()
Copyright © 2024. All rights reserved.