public class DefaultSqlContext extends Object implements SqlContext
| 限定符 | 构造器和说明 |
|---|---|
protected |
DefaultSqlContext() |
| 限定符和类型 | 方法和说明 |
|---|---|
<T> int |
delete(Class<T> entityClass,
Consumer<GenericWhereCondition> condition)
根据指定的条件删除数据库中的记录。
|
<T> int |
deleteByPrimaryKey(Class<T> entityClass,
Collection<?> pkValues)
根据主键值集合删除数据库中的多条记录。
|
<T> int |
deleteByPrimaryKey(Class<T> entityClass,
Object pkValue)
根据主键值删除数据库中的单条记录。
|
<T> T |
execute(String dataSourceName,
String sql,
ParameterBinder parameterBinder,
Class<T> returnType)
执行指定数据源上的 SQL 语句,并将结果映射为指定的返回类型。
|
<T,L extends List<T>> |
execute(String dataSourceName,
String sql,
ParameterBinder parameterBinder,
Class<T> returnType,
Supplier<L> listSupplier)
执行指定数据源上的 SQL 语句,并将结果映射为指定集合类型。
|
List<ColumnMetaData> |
getAllColumnMetaData(String dataSourceName,
String catalog,
String schemaPattern,
String tableNamePattern,
String columnNamePattern)
获取指定数据源、目录、模式、表名称模式和列名称模式下所有匹配的列元数据。
|
List<TableMetaData> |
getAllTableMetaData(String dataSourceName,
String catalog,
String schemaPattern,
String tableNamePattern,
String[] tableTypes)
获取指定数据源、目录、模式和名称模式下所有匹配的表元数据。
|
<T> int |
insert(T entity)
插入一个实体到数据库,插入所有字段。
|
<T> int |
insertBatch(Collection<T> entities)
批量插入一组实体到数据库。
|
<T> int |
insertMultiple(Collection<T> entities)
通过追加模式插入多条记录到数据库。
|
<T> int |
insertSelective(T entity)
插入一个实体到数据库,选择性插入非空字段。
|
<T> int |
insertSelective(T entity,
Collection<Fn<T,?>> forcedFields)
插入一个实体到数据库,选择性插入非空字段,并强制插入指定字段。
|
AbstractColumnReference |
select()
创建并返回一个
AbstractColumnReference 对象,用于构建 SQL 查询操作
,并选择所需的列进行检索。 |
<T> List<T> |
selectByPrimaryKey(Class<T> entityClass,
Collection<?> pkValues)
根据多个主键查询实体对象集合。
|
<T> T |
selectByPrimaryKey(Class<T> entityClass,
Object pkValue)
根据主键查询实体对象。
|
<T> List<T> |
selectList(String sql,
Class<T> returnType)
执行指定的 SQL 查询,返回映射为指定类型的结果列表。
|
<T> List<T> |
selectList(String sql,
Class<T> returnType,
ParameterBinder parameterBinder)
使用预编译的方式执行指定的 SQL 查询,返回映射为指定类型的结果列表。
|
<T> List<T> |
selectList(String dataSourceName,
String sql,
Class<T> returnType)
执行指定数据源上的 SQL 查询,返回映射为指定类型的结果列表。
|
<T> List<T> |
selectList(String dataSourceName,
String sql,
Class<T> returnType,
ParameterBinder parameterBinder)
使用预编译的方式在指定数据源上执行 SQL 查询,返回映射为指定类型的结果列表。
|
<T> T |
selectOne(String sql,
Class<T> returnType)
执行指定的 SQL 查询,返回映射为指定类型的单个结果。
|
<T> T |
selectOne(String sql,
Class<T> returnType,
ParameterBinder parameterBinder)
使用预编译的方式执行指定的 SQL 查询,返回映射为指定类型的单个结果。
|
<T> T |
selectOne(String dataSourceName,
String sql,
Class<T> returnType)
执行指定数据源上的 SQL 查询,返回映射为指定类型的单个结果。
|
<T> T |
selectOne(String dataSourceName,
String sql,
Class<T> returnType,
ParameterBinder parameterBinder)
使用预编译的方式在指定数据源上执行 SQL 查询,返回映射为指定类型的单个结果。
|
<T> int |
update(T entity,
Consumer<GenericWhereCondition> condition)
根据指定条件更新实体对象的所有字段。
|
<T> int |
updateByPrimaryKey(T entity)
根据主键更新整个实体对象的所有字段。
|
<T> int |
updateSelective(T entity,
Collection<Fn<T,?>> forcedFields,
Consumer<GenericWhereCondition> condition)
根据指定条件更新实体对象的非空字段,并强制更新指定的字段。
|
<T> int |
updateSelective(T entity,
Consumer<GenericWhereCondition> condition)
根据指定条件更新实体对象的非空字段。
|
<T> int |
updateSelectiveByPrimaryKey(T entity)
根据主键更新实体对象中非空字段。
|
<T> int |
updateSelectiveByPrimaryKey(T entity,
Collection<Fn<T,?>> forcedFields)
根据主键更新实体对象中非空字段,并强制更新指定的字段。
|
<T> int |
upsert(T entity)
插入或更新实体对象的所有字段。
|
<T> int |
upsertMultiple(Collection<T> entities)
批量插入或更新实体对象的所有字段。
|
<T> int |
upsertSelective(T entity)
插入或更新实体(仅更新非空字段)。
|
<T> int |
upsertSelective(T entity,
Collection<Fn<T,?>> forcedFields)
插入或更新实体(仅更新非空字段),并强制更新指定字段。
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitexecute, execute, execute, execute, execute, execute, getAllColumnMetaData, getAllTableMetaDatapublic AbstractColumnReference select()
SqlContextAbstractColumnReference 对象,用于构建 SQL 查询操作
,并选择所需的列进行检索。select 在接口中 SqlContextAbstractColumnReference 对象public <T> T selectByPrimaryKey(Class<T> entityClass, Object pkValue)
SqlContextselectByPrimaryKey 在接口中 SqlContextT - 实体类的类型。entityClass - 实体类的 Class 对象,用于指定查询结果的映射类型。pkValue - 主键值,用于定位唯一的数据库记录。null。public <T> List<T> selectByPrimaryKey(Class<T> entityClass, Collection<?> pkValues)
SqlContextselectByPrimaryKey 在接口中 SqlContextT - 实体类的类型。entityClass - 实体类的 Class 对象,用于指定查询结果的映射类型。pkValues - 主键值集合public <T> T selectOne(String sql, Class<T> returnType)
SqlContextselectOne 在接口中 SqlContextT - 返回值的泛型类型。sql - 要执行的 SQL 查询语句。returnType - 结果映射的目标类型。null。public <T> T selectOne(String sql, Class<T> returnType, ParameterBinder parameterBinder)
SqlContextselectOne 在接口中 SqlContextT - 返回值的泛型类型。sql - 要执行的 SQL 查询语句。returnType - 结果映射的目标类型。parameterBinder - 参数绑定器,用于绑定查询中的参数。null。public <T> T selectOne(String dataSourceName, String sql, Class<T> returnType)
SqlContextselectOne 在接口中 SqlContextT - 返回值的泛型类型。dataSourceName - 数据源名称,未指定默认按照优先级匹配最佳数据源sql - 要执行的 SQL 查询语句。returnType - 结果映射的目标类型。null。public <T> T selectOne(String dataSourceName, String sql, Class<T> returnType, ParameterBinder parameterBinder)
SqlContext使用示例:
ParameterBinder parameterBinder = new ParameterBinder();
String value = SqlUtils.registerValueWithKey(parameterBinder, 1);
String sql = "SELECT x.user_id FROM users x where x.user_id = %s";
sqlContext.selectOne("dataSource", String.format(sql, value), Integer.class, parameterBinder);
selectOne 在接口中 SqlContextT - 返回值的泛型类型。dataSourceName - 数据源名称,未指定默认按照优先级匹配最佳数据源sql - 要执行的 SQL 查询语句。returnType - 结果映射的目标类型。parameterBinder - 参数绑定器,用于绑定查询中的参数。null。public <T> List<T> selectList(String sql, Class<T> returnType)
SqlContextselectList 在接口中 SqlContextT - 返回值的泛型类型。sql - 要执行的 SQL 查询语句。returnType - 结果映射的目标类型。public <T> List<T> selectList(String sql, Class<T> returnType, ParameterBinder parameterBinder)
SqlContextselectList 在接口中 SqlContextT - 返回值的泛型类型。sql - 要执行的 SQL 查询语句。returnType - 结果映射的目标类型。parameterBinder - 参数绑定器,用于绑定查询中的参数。public <T> List<T> selectList(String dataSourceName, String sql, Class<T> returnType)
SqlContextselectList 在接口中 SqlContextT - 返回值的泛型类型。dataSourceName - 数据源名称,未指定默认按照优先级匹配最佳数据源sql - 要执行的 SQL 查询语句。returnType - 结果映射的目标类型。public <T> List<T> selectList(String dataSourceName, String sql, Class<T> returnType, ParameterBinder parameterBinder)
SqlContext使用示例:
ParameterBinder parameterBinder = new ParameterBinder();
String value = SqlUtils.registerValueWithKey(parameterBinder, 1);
String sql = "SELECT x.user_id FROM users x where x.user_id >= %s";
sqlContext.selectList("dataSource", String.format(sql, value), Integer.class, parameterBinder);
selectList 在接口中 SqlContextT - 返回值的泛型类型。dataSourceName - 数据源名称,未指定默认按照优先级匹配最佳数据源sql - 要执行的 SQL 查询语句。returnType - 结果映射的目标类型。parameterBinder - 参数绑定器,用于绑定查询中的参数。public <T> int insertSelective(T entity)
SqlContextinsertSelective 在接口中 SqlContextT - 实体的类型,通常是一个数据模型类。entity - 要插入的实体对象,不能为空。public <T> int insertSelective(T entity,
Collection<Fn<T,?>> forcedFields)
SqlContextinsertSelective 在接口中 SqlContextT - 实体的类型,通常是一个数据模型类。entity - 要插入的实体对象,不能为空。forcedFields - 强制插入的字段集合,可以为 null 或空集合。public <T> int insert(T entity)
SqlContextinsert 在接口中 SqlContextT - 实体的类型,通常是一个数据模型类。entity - 要插入的实体对象,不能为空。public <T> int insertBatch(Collection<T> entities)
SqlContextinsertBatch 在接口中 SqlContextT - 实体的类型,一般为数据模型类。entities - 要插入的实体集合,不能为空。public <T> int insertMultiple(Collection<T> entities)
SqlContextinsertMultiple 在接口中 SqlContextT - 实体的类型,一般为数据模型类。entities - 要插入的实体集合,不能为空。public <T> int deleteByPrimaryKey(Class<T> entityClass, Object pkValue)
SqlContextdeleteByPrimaryKey 在接口中 SqlContextT - 实体类的类型。entityClass - 实体类,表示需要操作的表。pkValue - 主键值,指定要删除的记录。public <T> int deleteByPrimaryKey(Class<T> entityClass, Collection<?> pkValues)
SqlContextdeleteByPrimaryKey 在接口中 SqlContextT - 实体类的类型。entityClass - 实体类,表示需要操作的表。pkValues - 主键值集合,指定要删除的多条记录。public <T> int delete(Class<T> entityClass, Consumer<GenericWhereCondition> condition)
SqlContextdelete 在接口中 SqlContextT - 实体类的类型。entityClass - 实体类,表示需要操作的表。condition - 条件构造器,使用 Consumer 定义删除条件。
如果传入 null,则删除整张表的所有数据。public <T> int updateByPrimaryKey(T entity)
SqlContextupdateByPrimaryKey 在接口中 SqlContextT - 实体类的类型。entity - 包含主键和其他字段的实体对象,所有字段将被更新。public <T> int updateSelectiveByPrimaryKey(T entity)
SqlContextupdateSelectiveByPrimaryKey 在接口中 SqlContextT - 实体类的类型。entity - 包含主键的实体对象,仅更新非空字段。public <T> int updateSelectiveByPrimaryKey(T entity,
Collection<Fn<T,?>> forcedFields)
SqlContextupdateSelectiveByPrimaryKey 在接口中 SqlContextT - 实体类的类型。entity - 包含主键的实体对象,仅更新非空字段。forcedFields - 需要强制更新的字段,即使字段值为空也会更新。public <T> int update(T entity,
Consumer<GenericWhereCondition> condition)
SqlContextupdate 在接口中 SqlContextT - 实体类的类型。entity - 实体对象,表示需要更新的数据。condition - 条件构造器,使用 Consumer 定义更新条件,为null更新所有。public <T> int updateSelective(T entity,
Consumer<GenericWhereCondition> condition)
SqlContextupdateSelective 在接口中 SqlContextT - 实体类的类型。entity - 实体对象,仅更新非空字段。condition - 条件构造器,使用 Consumer 定义更新条件,为null更新所有。public <T> int updateSelective(T entity,
Collection<Fn<T,?>> forcedFields,
Consumer<GenericWhereCondition> condition)
SqlContextupdateSelective 在接口中 SqlContextT - 实体类的类型。entity - 实体对象,仅更新非空字段。forcedFields - 需要强制更新的字段,即使字段值为空也会更新。condition - 条件构造器,使用 Consumer 定义更新条件,为null更新所有。public <T> int upsert(T entity)
SqlContext如果记录不存在,则执行插入操作。 如果记录已存在,则更新所有字段。
upsert 在接口中 SqlContextT - 实体类的类型。entity - 要插入或更新的实体对象。public <T> int upsertSelective(T entity)
SqlContext如果记录不存在,则执行插入操作。 如果记录已存在,则仅更新非空字段。
upsertSelective 在接口中 SqlContextT - 实体类的类型。entity - 要插入或更新的实体对象。public <T> int upsertSelective(T entity,
Collection<Fn<T,?>> forcedFields)
SqlContext如果记录不存在,则执行插入操作。 如果记录已存在,则仅更新非空字段,并强制更新指定字段。
upsertSelective 在接口中 SqlContextT - 实体类的类型。entity - 要插入或更新的实体对象。forcedFields - 需要强制更新的字段,即使字段值为空也会更新。public <T> int upsertMultiple(Collection<T> entities)
SqlContext对于每个实体: 如果记录不存在,则执行插入操作。 如果记录已存在,则更新所有字段。
upsertMultiple 在接口中 SqlContextT - 实体类的类型。entities - 包含需要插入或更新的实体对象集合。public <T> T execute(String dataSourceName, String sql, ParameterBinder parameterBinder, Class<T> returnType)
SqlContext此方法适用于需要在多数据源环境下执行查询,并将结果转换为单对象的场景。
execute 在接口中 SqlContextT - 返回对象的类型参数。dataSourceName - 数据源名称,用于标识要执行 SQL 的目标数据源。sql - 要执行的 SQL 语句字符串,其中可以包含占位符。parameterBinder - 参数绑定器,用于注册和绑定 SQL 参数。returnType - 结果的目标类型,例如 User.class 或 Integer.class。returnType。public <T,L extends List<T>> L execute(String dataSourceName, String sql, ParameterBinder parameterBinder, Class<T> returnType, Supplier<L> listSupplier)
SqlContext
此方法适用于需要返回集合结果的场景,例如返回 ArrayList<User> 或 LinkedList<User>。
调用者可通过 listSupplier 指定具体集合实现。
execute 在接口中 SqlContextT - 集合元素的类型参数。L - 返回集合的具体类型,必须是 List 的子类型。dataSourceName - 数据源名称,用于标识要执行 SQL 的目标数据源。sql - 要执行的 SQL 语句字符串,其中可以包含占位符。parameterBinder - 参数绑定器,用于注册和绑定 SQL 参数。returnType - 集合元素的目标类型,例如 User.class。listSupplier - 集合供应器,用于创建具体的集合实例(如 ArrayList::new)。L。public List<TableMetaData> getAllTableMetaData(String dataSourceName, String catalog, String schemaPattern, String tableNamePattern, String[] tableTypes)
SqlContextgetAllTableMetaData 在接口中 SqlContextdataSourceName - 数据源名称,用于指定执行查询的数据库连接。如果系统支持多个数据源,则通过此参数选择目标数据源。catalog - 目录名称,可为 null(如果数据库不支持目录或无需指定,在MySQL中等价于database)。schemaPattern - 模式名称或模式模式,可为 null(如果数据库不支持模式或无需指定,比如在MySQL中通常为null)。tableNamePattern - 表名称模式,支持通配符(如 "%" 匹配所有表,"user%" 匹配以 "user" 开头的表)。
不能为空或空字符串。tableTypes - 表类型的数组(如 "TABLE", "VIEW", "SYSTEM TABLE"),可为 null(默认只返回 "TABLE" 类型)。TableMetaData 对象,包含表的详细信息(如名称、类型、备注等)。
如果没有匹配的表,返回空列表(非 null)。获取表的元数据。public List<ColumnMetaData> getAllColumnMetaData(String dataSourceName, String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern)
SqlContextgetAllColumnMetaData 在接口中 SqlContextdataSourceName - 数据源名称,用于指定执行查询的数据库连接。catalog - 目录名称,可为 null(如果数据库不支持目录或无需指定)。schemaPattern - 模式名称或模式模式,可为 null(如果数据库不支持模式或无需指定)。tableNamePattern - 表名称模式,支持通配符(如 "%" 匹配所有表,"user%" 匹配以 "user" 开头的表)。
不能为空或空字符串。columnNamePattern - 列名称模式,支持通配符(如 "%" 匹配所有列,"id%" 匹配以 "id" 开头的列)。
可为 null,表示匹配所有列。ColumnMetaData 对象,包含列的详细信息(如名称、类型、注释等)。
如果没有匹配的列,返回空列表(非 null)。获取列的元数据。Copyright © 2024–2025 Dynamic-SQL. All rights reserved.