类 SqlHelper


  • public class SqlHelper
    extends Object
    拼常用SQL的工具类
    从以下版本开始:
    2015-11-03 22:40
    作者:
    liuzh
    • 构造器详细资料

      • SqlHelper

        public SqlHelper()
    • 方法详细资料

      • getDynamicTableName

        public static String getDynamicTableName​(Class<?> entityClass,
                                                 String tableName)
        获取表名 - 支持动态表名
        参数:
        entityClass -
        tableName -
        返回:
      • getDynamicTableName

        public static String getDynamicTableName​(Class<?> entityClass,
                                                 String tableName,
                                                 String parameterName)
        获取表名 - 支持动态表名,该方法用于多个入参时,通过parameterName指定入参中实体类的@Param的注解值
        参数:
        entityClass -
        tableName -
        parameterName -
        返回:
      • getBindCache

        public static String getBindCache​(EntityColumn column)
        参数:
        column -
        返回:
      • getIfCacheNotNull

        public static String getIfCacheNotNull​(EntityColumn column,
                                               String contents)
        参数:
        column -
        返回:
      • getIfCacheIsNull

        public static String getIfCacheIsNull​(EntityColumn column,
                                              String contents)
        如果_cache == null
        参数:
        column -
        返回:
      • getIfNotNull

        public static String getIfNotNull​(EntityColumn column,
                                          String contents,
                                          boolean empty)
        判断自动!=null的条件结构
        参数:
        column -
        contents -
        empty -
        返回:
      • getIfIsNull

        public static String getIfIsNull​(EntityColumn column,
                                         String contents,
                                         boolean empty)
        判断自动==null的条件结构
        参数:
        column -
        contents -
        empty -
        返回:
      • getIfNotNull

        public static String getIfNotNull​(String entityName,
                                          EntityColumn column,
                                          String contents,
                                          boolean empty)
        判断自动!=null的条件结构
        参数:
        entityName -
        column -
        contents -
        empty -
        返回:
      • getIfIsNull

        public static String getIfIsNull​(String entityName,
                                         EntityColumn column,
                                         String contents,
                                         boolean empty)
        判断自动==null的条件结构
        参数:
        entityName -
        column -
        contents -
        empty -
        返回:
      • getAllColumns

        public static String getAllColumns​(Class<?> entityClass)
        获取所有查询列,如id,name,code...
        参数:
        entityClass -
        返回:
      • selectAllColumns

        public static String selectAllColumns​(Class<?> entityClass)
        select xxx,xxx...
        参数:
        entityClass -
        返回:
      • selectCount

        public static String selectCount​(Class<?> entityClass)
        select count(x)
        参数:
        entityClass -
        返回:
      • selectCountExists

        public static String selectCountExists​(Class<?> entityClass)
        select case when count(x) > 0 then 1 else 0 end
        参数:
        entityClass -
        返回:
      • fromTable

        public static String fromTable​(Class<?> entityClass,
                                       String defaultTableName)
        from tableName - 动态表名
        参数:
        entityClass -
        defaultTableName -
        返回:
      • updateTable

        public static String updateTable​(Class<?> entityClass,
                                         String defaultTableName)
        update tableName - 动态表名
        参数:
        entityClass -
        defaultTableName -
        返回:
      • updateTable

        public static String updateTable​(Class<?> entityClass,
                                         String defaultTableName,
                                         String entityName)
        update tableName - 动态表名
        参数:
        entityClass -
        defaultTableName - 默认表名
        entityName - 别名
        返回:
      • deleteFromTable

        public static String deleteFromTable​(Class<?> entityClass,
                                             String defaultTableName)
        delete tableName - 动态表名
        参数:
        entityClass -
        defaultTableName -
        返回:
      • insertIntoTable

        public static String insertIntoTable​(Class<?> entityClass,
                                             String defaultTableName)
        insert into tableName - 动态表名
        参数:
        entityClass -
        defaultTableName -
        返回:
      • insertIntoTable

        public static String insertIntoTable​(Class<?> entityClass,
                                             String defaultTableName,
                                             String parameterName)
        insert into tableName - 动态表名
        参数:
        entityClass -
        defaultTableName -
        parameterName - 动态表名的参数名
        返回:
      • insertColumns

        public static String insertColumns​(Class<?> entityClass,
                                           boolean skipId,
                                           boolean notNull,
                                           boolean notEmpty)
        insert table()列
        参数:
        entityClass -
        skipId - 是否从列中忽略id类型
        notNull - 是否判断!=null
        notEmpty - 是否判断String类型!=''
        返回:
      • insertValuesColumns

        public static String insertValuesColumns​(Class<?> entityClass,
                                                 boolean skipId,
                                                 boolean notNull,
                                                 boolean notEmpty)
        insert-values()列
        参数:
        entityClass -
        skipId - 是否从列中忽略id类型
        notNull - 是否判断!=null
        notEmpty - 是否判断String类型!=''
        返回:
      • updateSetColumns

        public static String updateSetColumns​(Class<?> entityClass,
                                              String entityName,
                                              boolean notNull,
                                              boolean notEmpty)
        update set列
        参数:
        entityClass -
        entityName - 实体映射名
        notNull - 是否判断!=null
        notEmpty - 是否判断String类型!=''
        返回:
      • updateSetColumnsIgnoreVersion

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

        public static String notAllNullParameterCheck​(String parameterName,
                                                      Set<EntityColumn> columnSet)
        不是所有参数都是 null 的检查
        参数:
        parameterName - 参数名
        columnSet - 需要检查的列
        返回:
      • exampleHasAtLeastOneCriteriaCheck

        public static String exampleHasAtLeastOneCriteriaCheck​(String parameterName)
        Example 中包含至少 1 个查询条件
        参数:
        parameterName - 参数名
        返回:
      • wherePKColumns

        public static String wherePKColumns​(Class<?> entityClass)
        where主键条件
        参数:
        entityClass -
        返回:
      • wherePKColumns

        public static String wherePKColumns​(Class<?> entityClass,
                                            boolean useVersion)
        where主键条件
        参数:
        entityClass -
        useVersion -
        返回:
      • wherePKColumns

        public static String wherePKColumns​(Class<?> entityClass,
                                            String entityName,
                                            boolean useVersion)
        where主键条件
        参数:
        entityClass -
        entityName -
        useVersion -
        返回:
      • whereAllIfColumns

        public static String whereAllIfColumns​(Class<?> entityClass,
                                               boolean empty)
        where所有列的条件,会判断是否!=null
        参数:
        entityClass -
        empty -
        返回:
      • whereAllIfColumns

        public static String whereAllIfColumns​(Class<?> entityClass,
                                               boolean empty,
                                               boolean useVersion)
        where所有列的条件,会判断是否!=null
        参数:
        entityClass -
        empty -
        useVersion -
        返回:
      • whereVersion

        public static String whereVersion​(Class<?> entityClass)
        乐观锁字段条件
        参数:
        entityClass -
        返回:
      • whereLogicDelete

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

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

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

        public static int getLogicDeletedValue​(EntityColumn column,
                                               boolean isDeleted)
        获取逻辑删除注解的参数值
        参数:
        column -
        isDeleted - true:逻辑删除的值,false:未逻辑删除的值
        返回:
      • hasLogicDeleteColumn

        public static boolean hasLogicDeleteColumn​(Class<?> entityClass)
        是否有逻辑删除的注解
        参数:
        entityClass -
        返回:
      • getLogicDeleteColumn

        public static EntityColumn getLogicDeleteColumn​(Class<?> entityClass)
        获取逻辑删除注解的列,若没有返回null
        参数:
        entityClass -
        返回:
      • orderByDefault

        public static String orderByDefault​(Class<?> entityClass)
        获取默认的orderBy,通过注解设置的
        参数:
        entityClass -
        返回:
      • exampleSelectColumns

        public static String exampleSelectColumns​(Class<?> entityClass)
        example支持查询指定列时
        返回:
      • exampleCountColumn

        public static String exampleCountColumn​(Class<?> entityClass)
        example支持查询指定列时
        返回:
      • exampleOrderBy

        public static String exampleOrderBy​(Class<?> entityClass)
        example查询中的orderBy条件,会判断默认orderBy
        返回:
      • exampleOrderBy

        public static String exampleOrderBy​(String entityName,
                                            Class<?> entityClass)
        example查询中的orderBy条件,会判断默认orderBy
        返回:
      • exampleForUpdate

        public static String exampleForUpdate()
        example 支持 for update
        返回:
      • exampleCheck

        public static String exampleCheck​(Class<?> entityClass)
        example 支持 for update
        返回:
      • exampleWhereClause

        public static String exampleWhereClause()
        Example查询中的where结构,用于只有一个Example参数时
        返回:
      • updateByExampleWhereClause

        public static String updateByExampleWhereClause()
        Example-Update中的where结构,用于多个参数时,Example带@Param("example")注解时
        返回: