Class SqlBuilder

java.lang.Object
org.miaixz.bus.mapper.builder.SqlBuilder

public class SqlBuilder extends Object
拼常用SQL的工具类
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • SqlBuilder

      public SqlBuilder()
  • Method Details

    • getDynamicTableName

      public static String getDynamicTableName(Class<?> entityClass, String tableName)
      获取表名 - 支持动态表名
      Parameters:
      entityClass - 实体Class对象
      tableName - 表名称
      Returns:
      the string
    • getDynamicTableName

      public static String getDynamicTableName(Class<?> entityClass, String tableName, String parameterName)
      获取表名 - 支持动态表名,该方法用于多个入参时,通过parameterName指定入参中实体类的@Param的注解值
      Parameters:
      entityClass - 实体Class对象
      tableName - 表名称
      parameterName - 属性名称
      Returns:
      the string
    • getBindCache

      public static String getBindCache(EntityColumn column)
      Parameters:
      column - 列信息
      Returns:
      the string
    • getBindValue

      public static String getBindValue(EntityColumn column, String value)
      Parameters:
      column - 列信息
      value - 值
      Returns:
      the string
    • getIfCacheNotNull

      public static String getIfCacheNotNull(EntityColumn column, String contents)
      Parameters:
      column - 列
      contents - 内容
      Returns:
      the string
    • getIfCacheIsNull

      public static String getIfCacheIsNull(EntityColumn column, String contents)
      如果_cache == null
      Parameters:
      column - 列信息
      contents - 内容
      Returns:
      the string
    • getIfNotNull

      public static String getIfNotNull(EntityColumn column, String contents, boolean empty)
      判断自动!=null的条件结构
      Parameters:
      column - 列信息
      contents - 内容
      empty - 是否为空
      Returns:
      the string
    • getIfIsNull

      public static String getIfIsNull(EntityColumn column, String contents, boolean empty)
      判断自动==null的条件结构
      Parameters:
      column - 列信息
      contents - 内容
      empty - 是否为空
      Returns:
      the string
    • getIfNotNull

      public static String getIfNotNull(String entityName, EntityColumn column, String contents, boolean empty)
      判断自动!=null的条件结构
      Parameters:
      entityName - 实体映射名
      column - 列信息
      contents - 内容
      empty - 是否为空
      Returns:
      the string
    • getIfIsNull

      public static String getIfIsNull(String entityName, EntityColumn column, String contents, boolean empty)
      判断自动==null的条件结构
      Parameters:
      entityName - 实体映射名
      column - 列信息
      contents - 内容
      empty - 是否为空
      Returns:
      the string
    • getAllColumns

      public static String getAllColumns(Class<?> entityClass)
      获取所有查询列,如id,name,code...
      Parameters:
      entityClass - 实体Class对象
      Returns:
      the string
    • selectAllColumns

      public static String selectAllColumns(Class<?> entityClass)
      select xxx,xxx...
      Parameters:
      entityClass - 实体Class对象
      Returns:
      the string
    • selectCount

      public static String selectCount(Class<?> entityClass)
      select count(x)
      Parameters:
      entityClass - 实体Class对象
      Returns:
      the string
    • selectCountExists

      public static String selectCountExists(Class<?> entityClass)
      select case when count(x) 大于 0 then 1 else 0 end
      Parameters:
      entityClass - 实体Class对象
      Returns:
      the string
    • fromTable

      public static String fromTable(Class<?> entityClass, String defaultTableName)
      from tableName - 动态表名
      Parameters:
      entityClass - 实体Class对象
      defaultTableName - 默认表名
      Returns:
      the string
    • updateTable

      public static String updateTable(Class<?> entityClass, String defaultTableName)
      update tableName - 动态表名
      Parameters:
      entityClass - 实体Class对象
      defaultTableName - 默认表名
      Returns:
      the string
    • updateTable

      public static String updateTable(Class<?> entityClass, String defaultTableName, String entityName)
      update tableName - 动态表名
      Parameters:
      entityClass - 实体Class对象
      defaultTableName - 默认表名
      entityName - 实体映射名
      Returns:
      the string
    • deleteFromTable

      public static String deleteFromTable(Class<?> entityClass, String defaultTableName)
      delete tableName - 动态表名
      Parameters:
      entityClass - 实体Class对象
      defaultTableName - 默认表名
      Returns:
      the string
    • insertIntoTable

      public static String insertIntoTable(Class<?> entityClass, String defaultTableName)
      insert into tableName - 动态表名
      Parameters:
      entityClass - 实体Class对象
      defaultTableName - 默认表名
      Returns:
      the string
    • insertIntoTable

      public static String insertIntoTable(Class<?> entityClass, String defaultTableName, String parameterName)
      insert into tableName - 动态表名
      Parameters:
      entityClass - 实体Class对象
      defaultTableName - 默认表名
      parameterName - 动态表名的参数名
      Returns:
      the string
    • insertColumns

      public static String insertColumns(Class<?> entityClass, boolean skipId, boolean notNull, boolean notEmpty)
      insert table()列
      Parameters:
      entityClass - 实体Class对象
      skipId - 是否从列中忽略id类型
      notNull - 是否判断!=null
      notEmpty - 是否判断String类型!=''
      Returns:
      the string
    • insertValuesColumns

      public static String insertValuesColumns(Class<?> entityClass, boolean skipId, boolean notNull, boolean notEmpty)
      insert-values()列
      Parameters:
      entityClass - 实体Class对象
      skipId - 是否从列中忽略id类型
      notNull - 是否判断!=null
      notEmpty - 是否判断String类型!=''
      Returns:
      the string
    • updateSetColumns

      public static String updateSetColumns(Class<?> entityClass, String entityName, boolean notNull, boolean notEmpty)
      update set列
      Parameters:
      entityClass - 实体Class对象
      entityName - 实体映射名
      notNull - 是否判断!=null
      notEmpty - 是否判断String类型!=''
      Returns:
      XML中的SET语句块
    • updateSetColumnsIgnoreVersion

      public static String updateSetColumnsIgnoreVersion(Class<?> entityClass, String entityName, boolean notNull, boolean notEmpty)
      update set列,不考虑乐观锁注解 @Version
      Parameters:
      entityClass - 实体Class对象
      entityName - 实体映射名
      notNull - 是否判断!=null
      notEmpty - 是否判断String类型!=''
      Returns:
      the string
    • notAllNullParameterCheck

      public static String notAllNullParameterCheck(String parameterName, Set<EntityColumn> columnSet)
      不是所有参数都是 null 的检查
      Parameters:
      parameterName - 参数名
      columnSet - 需要检查的列
      Returns:
      the string
    • conditionHasAtLeastOneCriteriaCheck

      public static String conditionHasAtLeastOneCriteriaCheck(String parameterName)
      Condition 中包含至少 1 个查询条件
      Parameters:
      parameterName - 参数名
      Returns:
      the string
    • wherePKColumns

      public static String wherePKColumns(Class<?> entityClass)
      where主键条件
      Parameters:
      entityClass - 实体Class对象
      Returns:
      the string
    • wherePKColumns

      public static String wherePKColumns(Class<?> entityClass, boolean useVersion)
      where主键条件
      Parameters:
      entityClass - 实体Class对象
      useVersion - 版本条件
      Returns:
      the string
    • wherePKColumns

      public static String wherePKColumns(Class<?> entityClass, String entityName, boolean useVersion)
      where主键条件
      Parameters:
      entityClass - 实体Class对象
      entityName - 实体映射名
      useVersion - 版本条件
      Returns:
      the string
    • whereAllIfColumns

      public static String whereAllIfColumns(Class<?> entityClass, boolean empty)
      where所有列的条件,会判断是否!=null
      Parameters:
      entityClass - 实体Class对象
      empty - 是否为空
      Returns:
      the string
    • whereAllIfColumns

      public static String whereAllIfColumns(Class<?> entityClass, boolean empty, boolean useVersion)
      where所有列的条件,会判断是否!=null
      Parameters:
      entityClass - 实体Class对象
      empty - 是否为空
      useVersion - 版本条件
      Returns:
      the string
    • whereVersion

      public static String whereVersion(Class<?> entityClass)
      乐观锁字段条件
      Parameters:
      entityClass - 实体Class对象
      Returns:
      the string
    • whereVersion

      public static String whereVersion(Class<?> entityClass, String entityName)
      乐观锁字段条件
      Parameters:
      entityClass -
      entityName - 实体名称
      Returns:
      the string
    • whereLogicDelete

      public static String whereLogicDelete(Class<?> entityClass, boolean isDeleted)
      逻辑删除的where条件,没有逻辑删除注解则返回空字符串 AND column = value
      Parameters:
      entityClass - 实体Class对象
      isDeleted - true:已经逻辑删除,false:未逻辑删除
      Returns:
      the string
    • logicDeleteColumnEqualsValue

      public static String logicDeleteColumnEqualsValue(Class<?> entityClass, boolean isDeleted)
      返回格式: column = value 默认isDeletedValue = 1 notDeletedValue = 0 则返回is_deleted = 1 或 is_deleted = 0 若没有逻辑删除注解,则返回空字符串
      Parameters:
      entityClass - 实体Class对象
      isDeleted - true 已经逻辑删除 false 未逻辑删除
      Returns:
      the string
    • logicDeleteColumnEqualsValue

      public static String logicDeleteColumnEqualsValue(EntityColumn column, boolean isDeleted)
      返回格式: column = value 默认isDeletedValue = 1 notDeletedValue = 0 则返回is_deleted = 1 或 is_deleted = 0 若没有逻辑删除注解,则返回空字符串
      Parameters:
      column - 列
      isDeleted - true 已经逻辑删除 false 未逻辑删除
      Returns:
      the string
    • getLogicDeletedValue

      public static Integer getLogicDeletedValue(EntityColumn column, boolean isDeleted)
      获取逻辑删除注解的参数值
      Parameters:
      column - 列
      isDeleted - true:逻辑删除的值,false:未逻辑删除的值
      Returns:
      the int
    • hasLogicDeleteColumn

      public static boolean hasLogicDeleteColumn(Class<?> entityClass)
      是否有逻辑删除的注解
      Parameters:
      entityClass - 实体Class对象
      Returns:
      the string
    • getLogicDeleteColumn

      public static EntityColumn getLogicDeleteColumn(Class<?> entityClass)
      获取逻辑删除注解的列,若没有返回null
      Parameters:
      entityClass - 实体Class对象
      Returns:
      the string
    • orderByDefault

      public static String orderByDefault(Class<?> entityClass)
      获取默认的orderBy,通过注解设置的
      Parameters:
      entityClass - 实体Class对象
      Returns:
      the string
    • conditionSelectColumns

      public static String conditionSelectColumns(Class<?> entityClass)
      condition支持查询指定列时
      Parameters:
      entityClass - 实体Class对象
      Returns:
      the string
    • conditionCountColumn

      public static String conditionCountColumn(Class<?> entityClass)
      condition支持查询指定列时
      Parameters:
      entityClass - 实体Class对象
      Returns:
      the string
    • conditionOrderBy

      public static String conditionOrderBy(Class<?> entityClass)
      condition查询中的orderBy条件,会判断默认orderBy
      Parameters:
      entityClass - 实体Class对象
      Returns:
      the string
    • conditionOrderBy

      public static String conditionOrderBy(String entityName, Class<?> entityClass)
      condition查询中的orderBy条件,会判断默认orderBy
      Parameters:
      entityName - 实体映射名
      entityClass - 实体Class对象
      Returns:
      the string
    • conditionForUpdate

      public static String conditionForUpdate()
      condition 支持 for update
      Returns:
      the string
    • conditionCheck

      public static String conditionCheck(Class<?> entityClass)
      condition 支持 for update
      Parameters:
      entityClass - 实体Class对象
      Returns:
      the string
    • conditionWhereClause

      public static String conditionWhereClause()
      Condition查询中的where结构,用于只有一个Condition参数时
      Returns:
      the string
    • updateByConditionWhereClause

      public static String updateByConditionWhereClause()
      Condition-Update中的where结构,用于多个参数时,Condition带@Param("condition")注解时
      Returns:
      the string